wye 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module Wye
5
5
  module Base
6
6
  module ClassMethods
7
7
  def on(alternate, &blk)
8
- connection_handler.switch.on(alternate, &blk)
8
+ connection_handler.switch.on(self, alternate, &blk)
9
9
  end
10
10
  end
11
11
  end
@@ -19,6 +19,10 @@ module Wye
19
19
  @class_to_alternate_pools.map { |(klass,atp)| atp.map(&:first) }.flatten.uniq
20
20
  end
21
21
 
22
+ def distributor(type)
23
+ Distributor.new(type, alternates << nil)
24
+ end
25
+
22
26
  def establish_connection(name, spec)
23
27
  super.tap do
24
28
  @class_to_alternate_pools[name] ||= {}
@@ -0,0 +1,15 @@
1
+ module Wye
2
+ module Distributor
3
+ class RoundRobin
4
+ def initialize(values)
5
+ @cycle = values.cycle
6
+ end
7
+
8
+ def next(*arguments)
9
+ @cycle.next
10
+ rescue StopIteration
11
+ nil
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'zlib'
2
+
3
+ module Wye
4
+ module Distributor
5
+ class Sticky
6
+ def initialize(values)
7
+ @values = values
8
+ end
9
+
10
+ def next(id)
11
+ (mod = @values.length) > 0 ? @values[Zlib.crc32(id.to_s) % mod] : nil
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ module Wye
2
+ module Distributor
3
+ autoload :RoundRobin, 'wye/distributor/round_robin'
4
+ autoload :Sticky, 'wye/distributor/sticky'
5
+
6
+ def self.new(type, values)
7
+ klass = type.to_s.classify
8
+ raise "unknown distributor type" unless const_defined?(klass, false)
9
+ const_get(klass).new(values)
10
+ end
11
+ end
12
+ end
data/lib/wye/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Wye
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
data/lib/wye.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Wye
2
2
  autoload :ActiveRecord, 'wye/active_record'
3
+ autoload :Distributor, 'wye/distributor'
3
4
  autoload :Switch, 'wye/switch'
4
5
  end
5
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wye
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -56,6 +56,9 @@ files:
56
56
  - lib/wye/active_record/base.rb
57
57
  - lib/wye/active_record/connection_handler.rb
58
58
  - lib/wye/active_record.rb
59
+ - lib/wye/distributor/round_robin.rb
60
+ - lib/wye/distributor/sticky.rb
61
+ - lib/wye/distributor.rb
59
62
  - lib/wye/railtie.rb
60
63
  - lib/wye/switch.rb
61
64
  - lib/wye/version.rb