Module: QueueKit::Clients::RoundRobinShuffler

Defined in:
lib/queue_kit/clients/round_robin_shuffler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_ivars(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 4

def self.with_ivars(klass)
  mod = self
  klass.class_eval do
    include mod
    attr_accessor :commands_per_client

    def command_clients_size
      @clients.size
    end
  end
end

Instance Method Details

#clientObject



31
32
33
34
35
36
37
38
39
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 31

def client
  @client_command_count += 1

  if @client_command_count > commands_per_client
    rotate_client
  end

  clients[@client_index]
end

#client_command_with_retries(retries = 10) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 16

def client_command_with_retries(retries = 10)
  attempts = 0

  while attempts < retries
    if data = (yield client, attempts)
      return data
    end

    rotate_client
    attempts += 1
  end

  nil
end

#clientsObject



61
62
63
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 61

def clients
  []
end

#commands_per_clientObject



57
58
59
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 57

def commands_per_client
  100
end

#rotate_clientObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 45

def rotate_client
  @client_index ||= -1
  @client_len ||= clients.size

  @client_command_count = 0
  @client_index += 1

  if @client_index >= @client_len
    @client_index = 0
  end
end

#round_robin_from(options) ⇒ Object



41
42
43
# File 'lib/queue_kit/clients/round_robin_shuffler.rb', line 41

def round_robin_from(options)
  @commands_per_client = options[:commands_per_client] || 100
end