Class: Fairway::RandomDistribution

Inherits:
Object
  • Object
show all
Defined in:
lib/fairway/config.rb

Defined Under Namespace

Classes: CannotConnect

Constant Summary collapse

EXCEPTIONS =
[
  Redis::CannotConnectError,
  Errno::ETIMEDOUT,
  Errno::EHOSTUNREACH
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pools) ⇒ RandomDistribution

Returns a new instance of RandomDistribution.



13
14
15
# File 'lib/fairway/config.rb', line 13

def initialize(pools)
  @pools = pools
end

Instance Attribute Details

#poolsObject (readonly)

Returns the value of attribute pools.



11
12
13
# File 'lib/fairway/config.rb', line 11

def pools
  @pools
end

Instance Method Details

#with(&block) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fairway/config.rb', line 17

def with(&block)
  valid_pools = @pools

  while valid_pools.any?
    pool = valid_pools.sample

    pool.with do |conn|
      begin
        return yield(conn)
      rescue *EXCEPTIONS => e
        puts "FAIRWAY WITH EXCEPTION: #{e}"
        valid_pools -= [pool]
      end
    end
  end

  raise CannotConnect.new
end

#with_each(&block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/fairway/config.rb', line 48

def with_each(&block)
  @pools.shuffle.each do |pool|
    pool.with do |conn|
      yield(conn)
    end
  end
end

#with_each_running(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fairway/config.rb', line 36

def with_each_running(&block)
  @pools.shuffle.each do |pool|
    pool.with do |conn|
      begin
        yield(conn)
      rescue *EXCEPTIONS => e
        puts "FAIRWAY WITH EACH RUNNING EXCEPTION: #{e}"
      end
    end
  end
end