fairway 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fairway/config.rb +45 -9
- data/lib/fairway/facet.rb +7 -5
- data/lib/fairway/queue.rb +16 -10
- data/lib/fairway/scripts.rb +48 -7
- data/lib/fairway/version.rb +1 -1
- data/spec/lib/fairway/config_spec.rb +13 -2
- metadata +1 -1
data/lib/fairway/config.rb
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
module Fairway
|
2
|
+
class RandomPool
|
3
|
+
attr_reader :pools
|
4
|
+
|
5
|
+
def initialize(pools)
|
6
|
+
@pools = pools
|
7
|
+
end
|
8
|
+
|
9
|
+
def with(&block)
|
10
|
+
@pools.sample.with do |conn|
|
11
|
+
yield(conn)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
2
16
|
class Config
|
3
17
|
attr_accessor :namespace
|
4
18
|
attr_reader :defined_queues, :redis_options
|
@@ -8,7 +22,7 @@ module Fairway
|
|
8
22
|
QueueDefinition = Struct.new(:name, :channel)
|
9
23
|
|
10
24
|
def initialize
|
11
|
-
@redis_options =
|
25
|
+
@redis_options = []
|
12
26
|
@namespace = nil
|
13
27
|
@facet = lambda { |message| DEFAULT_FACET }
|
14
28
|
@defined_queues = []
|
@@ -29,32 +43,54 @@ module Fairway
|
|
29
43
|
end
|
30
44
|
|
31
45
|
def redis=(options)
|
32
|
-
@redis_options = options
|
46
|
+
@redis_options = [options].flatten
|
33
47
|
end
|
34
48
|
|
35
49
|
def redis
|
36
|
-
@redis ||=
|
50
|
+
@redis ||= redises
|
37
51
|
end
|
38
52
|
|
39
53
|
def scripts
|
40
54
|
@scripts ||= begin
|
41
|
-
Scripts.new(
|
55
|
+
Scripts.new(raw_redises, @namespace)
|
42
56
|
end
|
43
57
|
end
|
44
58
|
|
45
59
|
private
|
46
60
|
|
47
|
-
def
|
48
|
-
|
49
|
-
|
61
|
+
def redises
|
62
|
+
@redises ||= begin
|
63
|
+
@redis_options << {} if @redis_options.empty?
|
64
|
+
pools = @redis_options.map do |options|
|
65
|
+
pool(options) { Redis::Namespace.new(@namespace, redis: raw_redis(options)) }
|
66
|
+
end
|
67
|
+
|
68
|
+
RandomPool.new(pools)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def raw_redises
|
73
|
+
@raw_redises ||= begin
|
74
|
+
@redis_options << {} if @redis_options.empty?
|
75
|
+
pools = @redis_options.map do |options|
|
76
|
+
pool(options) { raw_redis(options) }
|
77
|
+
end
|
78
|
+
|
79
|
+
RandomPool.new(pools)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def pool(options, &block)
|
84
|
+
pool_size = options[:pool] || 1
|
85
|
+
pool_timeout = options[:timeout] || 5
|
50
86
|
|
51
87
|
ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
|
52
88
|
yield
|
53
89
|
end
|
54
90
|
end
|
55
91
|
|
56
|
-
def raw_redis
|
57
|
-
Redis.new(
|
92
|
+
def raw_redis(options)
|
93
|
+
Redis.new(options)
|
58
94
|
end
|
59
95
|
end
|
60
96
|
end
|
data/lib/fairway/facet.rb
CHANGED
@@ -10,11 +10,13 @@ module Fairway
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def length
|
13
|
-
redis.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
redis.pools.map do |pool|
|
14
|
+
pool.with do |conn|
|
15
|
+
each_queue do |queue|
|
16
|
+
conn.llen(facet_key(queue))
|
17
|
+
end.sum
|
18
|
+
end
|
19
|
+
end.sum
|
18
20
|
end
|
19
21
|
|
20
22
|
def priority
|
data/lib/fairway/queue.rb
CHANGED
@@ -8,21 +8,27 @@ module Fairway
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def active_facets
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
facet_names = []
|
12
|
+
|
13
|
+
redis.pools.each do |pool|
|
14
|
+
pool.with do |conn|
|
15
|
+
facet_names += unique_queues.map do |queue|
|
16
|
+
conn.smembers("#{queue}:active_facets")
|
17
|
+
end.flatten
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
facet_names.uniq.map do |name|
|
22
|
+
Facet.new(self, name)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
26
|
def length
|
23
|
-
redis.
|
24
|
-
|
25
|
-
|
27
|
+
redis.pools.map do |pool|
|
28
|
+
pool.with do |conn|
|
29
|
+
conn.mget(unique_queues.map{|q| "#{q}:length" }).map(&:to_i).sum
|
30
|
+
end
|
31
|
+
end.sum
|
26
32
|
end
|
27
33
|
|
28
34
|
def peek
|
data/lib/fairway/scripts.rb
CHANGED
@@ -15,14 +15,18 @@ module Fairway
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def register_queue(name, channel)
|
18
|
-
redis.
|
19
|
-
|
18
|
+
redis.pools.each do |pool|
|
19
|
+
pool.with do |conn|
|
20
|
+
conn.hset(registered_queues_key, name, channel)
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
25
|
def unregister_queue(name)
|
24
|
-
redis.
|
25
|
-
|
26
|
+
redis.pools.each do |pool|
|
27
|
+
pool.with do |conn|
|
28
|
+
conn.hdel(registered_queues_key, name)
|
29
|
+
end
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
@@ -35,12 +39,22 @@ module Fairway
|
|
35
39
|
def method_missing(method_name, *args)
|
36
40
|
loaded = false
|
37
41
|
|
38
|
-
|
39
|
-
|
42
|
+
if multi?(method_name)
|
43
|
+
each_pool do |conn|
|
44
|
+
conn.evalsha(script_sha(method_name), [namespace], args)
|
45
|
+
end
|
46
|
+
elsif first?(method_name)
|
47
|
+
first_pool do |conn|
|
48
|
+
conn.evalsha(script_sha(method_name), [namespace], args)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
redis.with do |conn|
|
52
|
+
conn.evalsha(script_sha(method_name), [namespace], args)
|
53
|
+
end
|
40
54
|
end
|
41
55
|
rescue Redis::CommandError => ex
|
42
56
|
if ex.message.include?("NOSCRIPT") && !loaded
|
43
|
-
|
57
|
+
each_pool do |conn|
|
44
58
|
conn.script(:load, script_source(method_name))
|
45
59
|
end
|
46
60
|
|
@@ -53,6 +67,33 @@ module Fairway
|
|
53
67
|
|
54
68
|
private
|
55
69
|
|
70
|
+
def first?(script)
|
71
|
+
["fairway_pull", "fairway_peek"].include?(script.to_s)
|
72
|
+
end
|
73
|
+
|
74
|
+
def multi?(script)
|
75
|
+
["fairway_priority", "fairway_destroy"].include?(script.to_s)
|
76
|
+
end
|
77
|
+
|
78
|
+
def each_pool(&block)
|
79
|
+
redis.pools.each do |pool|
|
80
|
+
pool.with do |conn|
|
81
|
+
yield(conn)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def first_pool(&block)
|
87
|
+
redis.pools.each do |pool|
|
88
|
+
pool.with do |conn|
|
89
|
+
val = yield(conn)
|
90
|
+
return val if val
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
nil
|
95
|
+
end
|
96
|
+
|
56
97
|
def registered_queues_key
|
57
98
|
"#{namespace}registered_queues"
|
58
99
|
end
|
data/lib/fairway/version.rb
CHANGED
@@ -29,19 +29,30 @@ module Fairway
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
+
it "allows multiple redis config" do
|
33
|
+
Config.new do |config|
|
34
|
+
config.redis = [
|
35
|
+
{ host: "127.0.0.1", port: 6379 },
|
36
|
+
{ host: "127.0.0.1", port: 6380 }
|
37
|
+
]
|
38
|
+
|
39
|
+
config.redis.pools.length.should == 2
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
32
43
|
it "allows setting of connection pooling" do
|
33
44
|
config = Config.new do |config|
|
34
45
|
config.redis = { pool: 10 }
|
35
46
|
end
|
36
47
|
|
37
|
-
config.redis.instance_variable_get("@size").should == 10
|
48
|
+
config.redis.pools.first.instance_variable_get("@size").should == 10
|
38
49
|
end
|
39
50
|
|
40
51
|
it "defaults to pool of 1" do
|
41
52
|
config = Config.new do |config|
|
42
53
|
end
|
43
54
|
|
44
|
-
config.redis.instance_variable_get("@size").should == 1
|
55
|
+
config.redis.pools.first.instance_variable_get("@size").should == 1
|
45
56
|
end
|
46
57
|
|
47
58
|
it "allows setting of redis namespace" do
|