fairway 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/fairway/config.rb +37 -6
- data/lib/fairway/queue.rb +4 -6
- data/lib/fairway/scripts.rb +6 -18
- data/lib/fairway/version.rb +1 -1
- data/spec/lib/fairway/config_spec.rb +42 -2
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/lib/fairway/config.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
module Fairway
|
2
|
-
class
|
2
|
+
class RandomDistribution
|
3
|
+
class CannotConnect < RuntimeError; end
|
4
|
+
|
5
|
+
EXCEPTIONS = [
|
6
|
+
Redis::CannotConnectError,
|
7
|
+
Errno::ETIMEDOUT,
|
8
|
+
Errno::EHOSTUNREACH
|
9
|
+
]
|
10
|
+
|
3
11
|
attr_reader :pools
|
4
12
|
|
5
13
|
def initialize(pools)
|
@@ -7,15 +15,37 @@ module Fairway
|
|
7
15
|
end
|
8
16
|
|
9
17
|
def with(&block)
|
10
|
-
@pools
|
11
|
-
|
18
|
+
valid_pools = @pools
|
19
|
+
|
20
|
+
while valid_pools.any?
|
21
|
+
pool = valid_pools.sample
|
22
|
+
|
23
|
+
pool.with do |conn|
|
24
|
+
begin
|
25
|
+
return yield(conn)
|
26
|
+
rescue *EXCEPTIONS
|
27
|
+
valid_pools -= [pool]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
raise CannotConnect.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def with_each(&block)
|
36
|
+
@pools.each do |pool|
|
37
|
+
pool.with do |conn|
|
38
|
+
begin
|
39
|
+
yield(conn)
|
40
|
+
end
|
41
|
+
end
|
12
42
|
end
|
13
43
|
end
|
14
44
|
end
|
15
45
|
|
16
46
|
class Config
|
17
47
|
attr_accessor :namespace
|
18
|
-
attr_reader :defined_queues, :redis_options
|
48
|
+
attr_reader :defined_queues, :redis_options, :distribute
|
19
49
|
|
20
50
|
DEFAULT_FACET = "default"
|
21
51
|
|
@@ -24,6 +54,7 @@ module Fairway
|
|
24
54
|
def initialize
|
25
55
|
@redis_options = []
|
26
56
|
@namespace = nil
|
57
|
+
@distribute = RandomDistribution
|
27
58
|
@facet = lambda { |message| DEFAULT_FACET }
|
28
59
|
@defined_queues = []
|
29
60
|
|
@@ -65,7 +96,7 @@ module Fairway
|
|
65
96
|
pool(options) { Redis::Namespace.new(@namespace, redis: raw_redis(options)) }
|
66
97
|
end
|
67
98
|
|
68
|
-
|
99
|
+
@distribute.new(pools)
|
69
100
|
end
|
70
101
|
end
|
71
102
|
|
@@ -76,7 +107,7 @@ module Fairway
|
|
76
107
|
pool(options) { raw_redis(options) }
|
77
108
|
end
|
78
109
|
|
79
|
-
|
110
|
+
@distribute.new(pools)
|
80
111
|
end
|
81
112
|
end
|
82
113
|
|
data/lib/fairway/queue.rb
CHANGED
@@ -10,12 +10,10 @@ module Fairway
|
|
10
10
|
def active_facets
|
11
11
|
facet_names = []
|
12
12
|
|
13
|
-
redis.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end.flatten
|
18
|
-
end
|
13
|
+
redis.with_each do |conn|
|
14
|
+
facet_names += unique_queues.map do |queue|
|
15
|
+
conn.smembers("#{queue}:active_facets")
|
16
|
+
end.flatten
|
19
17
|
end
|
20
18
|
|
21
19
|
facet_names.uniq.map do |name|
|
data/lib/fairway/scripts.rb
CHANGED
@@ -15,18 +15,14 @@ module Fairway
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def register_queue(name, channel)
|
18
|
-
redis.
|
19
|
-
|
20
|
-
conn.hset(registered_queues_key, name, channel)
|
21
|
-
end
|
18
|
+
redis.with_each do |conn|
|
19
|
+
conn.hset(registered_queues_key, name, channel)
|
22
20
|
end
|
23
21
|
end
|
24
22
|
|
25
23
|
def unregister_queue(name)
|
26
|
-
redis.
|
27
|
-
|
28
|
-
conn.hdel(registered_queues_key, name)
|
29
|
-
end
|
24
|
+
redis.with_each do |conn|
|
25
|
+
conn.hdel(registered_queues_key, name)
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
@@ -40,7 +36,7 @@ module Fairway
|
|
40
36
|
loaded = false
|
41
37
|
|
42
38
|
if multi?(method_name)
|
43
|
-
|
39
|
+
redis.with_each do |conn|
|
44
40
|
conn.evalsha(script_sha(method_name), [namespace], args)
|
45
41
|
end
|
46
42
|
elsif first?(method_name)
|
@@ -54,7 +50,7 @@ module Fairway
|
|
54
50
|
end
|
55
51
|
rescue Redis::CommandError => ex
|
56
52
|
if ex.message.include?("NOSCRIPT") && !loaded
|
57
|
-
|
53
|
+
redis.with_each do |conn|
|
58
54
|
conn.script(:load, script_source(method_name))
|
59
55
|
end
|
60
56
|
|
@@ -75,14 +71,6 @@ module Fairway
|
|
75
71
|
["fairway_priority", "fairway_destroy"].include?(script.to_s)
|
76
72
|
end
|
77
73
|
|
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
74
|
def first_pool(&block)
|
87
75
|
redis.pools.each do |pool|
|
88
76
|
pool.with do |conn|
|
data/lib/fairway/version.rb
CHANGED
@@ -30,14 +30,54 @@ module Fairway
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it "allows multiple redis config" do
|
33
|
-
Config.new do |config|
|
33
|
+
config = Config.new do |config|
|
34
34
|
config.redis = [
|
35
35
|
{ host: "127.0.0.1", port: 6379 },
|
36
36
|
{ host: "127.0.0.1", port: 6380 }
|
37
37
|
]
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
+
config.redis.pools.length.should == 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it "distributes requests randomly between multiple redis's" do
|
44
|
+
config = Config.new do |config|
|
45
|
+
config.redis = [
|
46
|
+
{ host: "127.0.0.1", port: 6379 },
|
47
|
+
{ host: "127.0.0.1", port: 6380 }
|
48
|
+
]
|
49
|
+
end
|
50
|
+
|
51
|
+
ports = []
|
52
|
+
|
53
|
+
20.times do
|
54
|
+
config.redis.with do |conn|
|
55
|
+
ports << conn.info["tcp_port"]
|
56
|
+
end
|
40
57
|
end
|
58
|
+
|
59
|
+
ports.uniq.sort.should == ["6379", "6380"]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "doesn't lose requests if one redis is down" do
|
63
|
+
config = Config.new do |config|
|
64
|
+
config.redis = [
|
65
|
+
{ host: "127.0.0.1", port: 6379 },
|
66
|
+
{ host: "127.0.0.1", port: 6380 },
|
67
|
+
{ host: "127.0.0.1", port: 9999 }
|
68
|
+
]
|
69
|
+
end
|
70
|
+
|
71
|
+
ports = []
|
72
|
+
|
73
|
+
20.times do
|
74
|
+
config.redis.with do |conn|
|
75
|
+
ports << conn.info["tcp_port"]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
ports.length.should == 20
|
80
|
+
ports.uniq.sort.should == ["6379", "6380"]
|
41
81
|
end
|
42
82
|
|
43
83
|
it "allows setting of connection pooling" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fairway
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-10-
|
12
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|