fairway 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source :rubygems
4
4
  gemspec
5
5
 
6
6
  group :test do
7
+ gem "debugger"
7
8
  gem "sidekiq"
8
9
  gem "rspec"
9
10
  end
data/Gemfile.lock CHANGED
@@ -1,9 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fairway (0.1.2)
4
+ fairway (0.1.3)
5
5
  activesupport
6
- hiredis
7
6
  redis
8
7
  redis-namespace (>= 1.3.0)
9
8
 
@@ -16,10 +15,16 @@ GEM
16
15
  celluloid (0.12.4)
17
16
  facter (>= 1.6.12)
18
17
  timers (>= 1.0.0)
18
+ columnize (0.3.6)
19
19
  connection_pool (1.0.0)
20
+ debugger (1.6.0)
21
+ columnize (>= 0.3.1)
22
+ debugger-linecache (~> 1.2.0)
23
+ debugger-ruby_core_source (~> 1.2.1)
24
+ debugger-linecache (1.2.0)
25
+ debugger-ruby_core_source (1.2.2)
20
26
  diff-lcs (1.1.3)
21
27
  facter (1.6.17)
22
- hiredis (0.4.5)
23
28
  i18n (0.6.1)
24
29
  multi_json (1.5.0)
25
30
  redis (3.0.4)
@@ -45,6 +50,7 @@ PLATFORMS
45
50
  ruby
46
51
 
47
52
  DEPENDENCIES
53
+ debugger
48
54
  fairway!
49
55
  rspec
50
56
  sidekiq
data/fairway.gemspec CHANGED
@@ -19,6 +19,5 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency("activesupport")
21
21
  gem.add_dependency("redis")
22
- gem.add_dependency("hiredis")
23
22
  gem.add_dependency("redis-namespace", ">= 1.3.0")
24
23
  end
@@ -33,18 +33,28 @@ module Fairway
33
33
  end
34
34
 
35
35
  def redis
36
- @redis ||= Redis::Namespace.new(@namespace, redis: raw_redis)
36
+ @redis ||= pool { Redis::Namespace.new(@namespace, redis: raw_redis) }
37
37
  end
38
38
 
39
39
  def scripts
40
- @scripts ||= Scripts.new(raw_redis, @namespace)
40
+ @scripts ||= begin
41
+ Scripts.new(pool { raw_redis }, @namespace)
42
+ end
41
43
  end
42
44
 
43
- private
45
+ private
44
46
 
45
- def raw_redis
46
- @raw_redis ||= Redis.new(@redis_options.merge(hiredis: true))
47
+ def pool(&block)
48
+ pool_size = @redis_options[:pool] || 1
49
+ pool_timeout = @redis_options[:timeout] || 5
50
+
51
+ ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
52
+ yield
53
+ end
47
54
  end
48
55
 
56
+ def raw_redis
57
+ Redis.new(@redis_options)
58
+ end
49
59
  end
50
60
  end
@@ -26,9 +26,11 @@ module Fairway
26
26
  end
27
27
 
28
28
  def subscribe(channel_pattern, &block)
29
- redis.psubscribe(channel_pattern) do |on|
30
- on.pmessage do |pattern, channel, message|
31
- block.call(channel, message)
29
+ redis do |conn|
30
+ conn.psubscribe(channel_pattern) do |on|
31
+ on.pmessage do |pattern, channel, message|
32
+ block.call(channel, message)
33
+ end
32
34
  end
33
35
  end
34
36
  end
data/lib/fairway/facet.rb CHANGED
@@ -10,14 +10,18 @@ module Fairway
10
10
  end
11
11
 
12
12
  def length
13
- each_queue do |queue|
14
- redis.llen(facet_key(queue))
15
- end.sum
13
+ redis.with do |conn|
14
+ each_queue do |queue|
15
+ conn.llen(facet_key(queue))
16
+ end.sum
17
+ end
16
18
  end
17
19
 
18
20
  def priority
19
- each_queue do |queue|
20
- (redis.hget(priority_key(queue), name) || 1).to_i
21
+ redis.with do |conn|
22
+ each_queue do |queue|
23
+ (conn.hget(priority_key(queue), name) || 1).to_i
24
+ end
21
25
  end
22
26
  end
23
27
 
data/lib/fairway/queue.rb CHANGED
@@ -8,17 +8,21 @@ module Fairway
8
8
  end
9
9
 
10
10
  def active_facets
11
- facet_names = unique_queues.map do |queue|
12
- redis.smembers("#{queue}:active_facets")
13
- end.flatten.uniq
11
+ redis.with do |conn|
12
+ facet_names = unique_queues.map do |queue|
13
+ conn.smembers("#{queue}:active_facets")
14
+ end.flatten.uniq
14
15
 
15
- facet_names.map do |name|
16
- Facet.new(self, name)
16
+ facet_names.map do |name|
17
+ Facet.new(self, name)
18
+ end
17
19
  end
18
20
  end
19
21
 
20
22
  def length
21
- redis.mget(unique_queues.map{|q| "#{q}:length" }).map(&:to_i).sum
23
+ redis.with do |conn|
24
+ conn.mget(unique_queues.map{|q| "#{q}:length" }).map(&:to_i).sum
25
+ end
22
26
  end
23
27
 
24
28
  def peek
@@ -7,29 +7,43 @@ module Fairway
7
7
  @script_shas ||= {}
8
8
  end
9
9
 
10
+ attr_reader :redis
11
+
10
12
  def initialize(redis, namespace)
11
- @redis = redis
13
+ @redis = redis
12
14
  @namespace = namespace
13
15
  end
14
16
 
15
17
  def register_queue(name, channel)
16
- @redis.hset(registered_queues_key, name, channel)
18
+ redis.with do |conn|
19
+ conn.hset(registered_queues_key, name, channel)
20
+ end
17
21
  end
18
22
 
19
23
  def unregister_queue(name)
20
- @redis.hdel(registered_queues_key, name)
24
+ redis.with do |conn|
25
+ conn.hdel(registered_queues_key, name)
26
+ end
21
27
  end
22
28
 
23
29
  def registered_queues
24
- @redis.hgetall(registered_queues_key)
30
+ redis.with do |conn|
31
+ conn.hgetall(registered_queues_key)
32
+ end
25
33
  end
26
34
 
27
35
  def method_missing(method_name, *args)
28
36
  loaded = false
29
- @redis.evalsha(script_sha(method_name), [namespace], args)
37
+
38
+ redis.with do |conn|
39
+ conn.evalsha(script_sha(method_name), [namespace], args)
40
+ end
30
41
  rescue Redis::CommandError => ex
31
42
  if ex.message.include?("NOSCRIPT") && !loaded
32
- @redis.script(:load, script_source(method_name))
43
+ redis.with do |conn|
44
+ conn.script(:load, script_source(method_name))
45
+ end
46
+
33
47
  loaded = true
34
48
  retry
35
49
  else
@@ -1,3 +1,3 @@
1
1
  module Fairway
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/fairway.rb CHANGED
@@ -2,7 +2,6 @@ require "fairway/version"
2
2
 
3
3
  require "active_support/core_ext"
4
4
  require "redis"
5
- require "hiredis"
6
5
  require "redis-namespace"
7
6
 
8
7
  require "fairway/config"
@@ -13,7 +13,7 @@ module Fairway
13
13
  message[:topic]
14
14
  end
15
15
  end
16
- let(:redis) { config.redis }
16
+ let(:redis) { config.redis }
17
17
  let(:message) { { facet: 1, topic: "event:helloworld" } }
18
18
 
19
19
  it "delegates non existant methods to parent connection" do
@@ -33,8 +33,11 @@ module Fairway
33
33
  config.register_queue("myqueue", ".*:helloworld")
34
34
  config.register_queue("yourqueue", "event:.*world")
35
35
  connection.deliver(message)
36
- redis.llen("myqueue:1").should == 1
37
- redis.llen("yourqueue:1").should == 1
36
+
37
+ redis.with do |conn|
38
+ conn.llen("myqueue:1").should == 1
39
+ conn.llen("yourqueue:1").should == 1
40
+ end
38
41
  end
39
42
  end
40
43
 
@@ -45,25 +48,36 @@ module Fairway
45
48
 
46
49
  it "adds message to the environment facet for the queue" do
47
50
  connection.deliver(message)
48
- redis.llen("myqueue:1").should == 1
49
- redis.lindex("myqueue:1", 0).should == message.to_json
51
+
52
+ redis.with do |conn|
53
+ conn.llen("myqueue:1").should == 1
54
+ conn.lindex("myqueue:1", 0).should == message.to_json
55
+ end
50
56
  end
51
57
 
52
58
  it "adds facet to list of active facets" do
53
59
  connection.deliver(message)
54
- redis.smembers("myqueue:active_facets").should == ["1"]
60
+
61
+ redis.with do |conn|
62
+ conn.smembers("myqueue:active_facets").should == ["1"]
63
+ end
55
64
  end
56
65
 
57
66
  it "pushes facet onto facet queue" do
58
67
  connection.deliver(message)
59
- redis.llen("myqueue:facet_queue").should == 1
60
- redis.lindex("myqueue:facet_queue", 0).should == "1"
68
+
69
+ redis.with do |conn|
70
+ conn.llen("myqueue:facet_queue").should == 1
71
+ conn.lindex("myqueue:facet_queue", 0).should == "1"
72
+ end
61
73
  end
62
74
 
63
75
  it "doesn't push onto to facet queue if currently active" do
64
- redis.sadd("myqueue:active_facets", "1")
65
- connection.deliver(message)
66
- redis.llen("myqueue:facet_queue").should == 0
76
+ redis.with do |conn|
77
+ conn.sadd("myqueue:active_facets", "1")
78
+ connection.deliver(message)
79
+ conn.llen("myqueue:facet_queue").should == 0
80
+ end
67
81
  end
68
82
  end
69
83
 
@@ -74,12 +88,18 @@ module Fairway
74
88
 
75
89
  it "doesn't add message to the queue" do
76
90
  connection.deliver(message)
77
- redis.llen("myqueue:1").should == 0
91
+
92
+ redis.with do |conn|
93
+ conn.llen("myqueue:1").should == 0
94
+ end
78
95
  end
79
96
 
80
97
  it "doesn't add facet to list of active facets" do
81
98
  connection.deliver(message)
82
- redis.smembers("myqueue:active_facets").should == []
99
+
100
+ redis.with do |conn|
101
+ conn.smembers("myqueue:active_facets").should == []
102
+ end
83
103
  end
84
104
  end
85
105
  end
@@ -23,12 +23,27 @@ module Fairway
23
23
  end
24
24
  end
25
25
 
26
- it "allows setting of redis connection options" do
26
+ it "allows redis config" do
27
27
  Config.new do |config|
28
28
  config.redis = { host: "127.0.0.1", port: 6379 }
29
29
  end
30
30
  end
31
31
 
32
+ it "allows setting of connection pooling" do
33
+ config = Config.new do |config|
34
+ config.redis = { pool: 10 }
35
+ end
36
+
37
+ config.redis.instance_variable_get("@size").should == 10
38
+ end
39
+
40
+ it "defaults to pool of 1" do
41
+ config = Config.new do |config|
42
+ end
43
+
44
+ config.redis.instance_variable_get("@size").should == 1
45
+ end
46
+
32
47
  it "allows setting of redis namespace" do
33
48
  config = Config.new do |config|
34
49
  config.namespace = "ns"
@@ -8,14 +8,16 @@ module Fairway
8
8
 
9
9
  describe "#initialize" do
10
10
  it "registers queues from the config" do
11
- Fairway.config.register_queue("myqueue", ".*")
12
- Fairway.config.redis.hgetall("registered_queues").should == {}
11
+ redis.with do |conn|
12
+ Fairway.config.register_queue("myqueue", ".*")
13
+ conn.hgetall("registered_queues").should == {}
13
14
 
14
- Connection.new(Fairway.config)
15
+ Connection.new(Fairway.config)
15
16
 
16
- Fairway.config.redis.hgetall("registered_queues").should == {
17
- "myqueue" => ".*"
18
- }
17
+ conn.hgetall("registered_queues").should == {
18
+ "myqueue" => ".*"
19
+ }
20
+ end
19
21
  end
20
22
 
21
23
  context "when an existing queue definition does not match" do
@@ -25,7 +27,9 @@ module Fairway
25
27
 
26
28
  describe "#queues" do
27
29
  it "returns a Queue for every currently registered queue" do
28
- Fairway.config.redis.hset("registered_queues", "name", "channel")
30
+ redis.with do |conn|
31
+ conn.hset("registered_queues", "name", "channel")
32
+ end
29
33
 
30
34
  connection.queues.should == [
31
35
  Queue.new(connection, "name")
@@ -57,25 +61,36 @@ module Fairway
57
61
 
58
62
  it "adds message to the environment facet for the queue" do
59
63
  connection.deliver(message)
60
- redis.llen("myqueue:1").should == 1
61
- redis.lindex("myqueue:1", 0).should == message.to_json
64
+
65
+ redis.with do |conn|
66
+ conn.llen("myqueue:1").should == 1
67
+ conn.lindex("myqueue:1", 0).should == message.to_json
68
+ end
62
69
  end
63
70
 
64
71
  it "adds facet to list of active facets" do
65
72
  connection.deliver(message)
66
- redis.smembers("myqueue:active_facets").should == ["1"]
73
+
74
+ redis.with do |conn|
75
+ conn.smembers("myqueue:active_facets").should == ["1"]
76
+ end
67
77
  end
68
78
 
69
79
  it "pushes facet onto facet queue" do
70
80
  connection.deliver(message)
71
- redis.llen("myqueue:facet_queue").should == 1
72
- redis.lindex("myqueue:facet_queue", 0).should == "1"
81
+
82
+ redis.with do |conn|
83
+ conn.llen("myqueue:facet_queue").should == 1
84
+ conn.lindex("myqueue:facet_queue", 0).should == "1"
85
+ end
73
86
  end
74
87
 
75
88
  it "doesn't push onto to facet queue if currently active" do
76
- redis.sadd("myqueue:active_facets", "1")
77
- connection.deliver(message)
78
- redis.llen("myqueue:facet_queue").should == 0
89
+ redis.with do |conn|
90
+ conn.sadd("myqueue:active_facets", "1")
91
+ connection.deliver(message)
92
+ conn.llen("myqueue:facet_queue").should == 0
93
+ end
79
94
  end
80
95
  end
81
96
 
@@ -88,7 +103,11 @@ module Fairway
88
103
  connection.deliver(message)
89
104
  connection.unregister_queue("myqueue")
90
105
  connection.deliver(message)
91
- redis.llen("myqueue:1").should == 1
106
+
107
+
108
+ redis.with do |conn|
109
+ conn.llen("myqueue:1").should == 1
110
+ end
92
111
  end
93
112
  end
94
113
  end
@@ -294,7 +294,9 @@ module Fairway
294
294
  connection.deliver(message2)
295
295
  connection.deliver(message1)
296
296
 
297
- connection.redis.del("myqueue:facet_pool")
297
+ connection.redis.with do |conn|
298
+ conn.del("myqueue:facet_pool")
299
+ end
298
300
 
299
301
  queue.pull.should == ["myqueue", message1.to_json]
300
302
  queue.pull.should == ["myqueue", message2.to_json]
@@ -328,11 +330,13 @@ module Fairway
328
330
  it "removes any redis keys related to the queue" do
329
331
  connection.deliver(message)
330
332
 
331
- connection.redis.keys("myqueue:*").length.should > 0
333
+ connection.redis.with do |conn|
334
+ conn.keys("myqueue:*").length.should > 0
332
335
 
333
- queue.destroy
336
+ queue.destroy
334
337
 
335
- connection.redis.keys("myqueue:*").length.should == 0
338
+ conn.keys("myqueue:*").length.should == 0
339
+ end
336
340
  end
337
341
 
338
342
  context "multiple queues" do
@@ -359,13 +363,15 @@ module Fairway
359
363
  end
360
364
 
361
365
  it "removes any redis keys related both queues" do
362
- connection.redis.keys("myqueue1:*").length.should > 0
363
- connection.redis.keys("myqueue2:*").length.should > 0
366
+ connection.redis.with do |conn|
367
+ conn.keys("myqueue1:*").length.should > 0
368
+ conn.keys("myqueue2:*").length.should > 0
364
369
 
365
- queue.destroy
370
+ queue.destroy
366
371
 
367
- connection.redis.keys("myqueue1:*").length.should == 0
368
- connection.redis.keys("myqueue2:*").length.should == 0
372
+ conn.keys("myqueue1:*").length.should == 0
373
+ conn.keys("myqueue2:*").length.should == 0
374
+ end
369
375
  end
370
376
  end
371
377
  end
@@ -2,6 +2,8 @@ require "spec_helper"
2
2
 
3
3
  module Fairway
4
4
  describe Scripts do
5
+ let(:redis) { Fairway::Config.new.redis }
6
+
5
7
  describe "#initialize" do
6
8
  it "requires a redis client" do
7
9
  lambda {
@@ -11,36 +13,45 @@ module Fairway
11
13
  end
12
14
 
13
15
  describe "#register_queue" do
14
- let(:scripts) { Scripts.new(Redis.new, "foo") }
16
+ let(:scripts) { Scripts.new(redis, "foo") }
15
17
 
16
18
  it "adds the queue and channel to the hash of registered queues" do
17
19
  scripts.register_queue("name", "channel")
18
- Redis.new.hgetall("foo:registered_queues").should == { "name" => "channel" }
20
+
21
+ redis.with do |conn|
22
+ conn.hgetall("foo:registered_queues").should == { "name" => "channel" }
23
+ end
19
24
  end
20
25
  end
21
26
 
22
27
  describe "#unregister_queue" do
23
- let(:scripts) { Scripts.new(Redis.new, "foo") }
28
+ let(:scripts) { Scripts.new(redis, "foo") }
24
29
 
25
30
  it "removes the queue and channel from the hash of registered queues" do
26
31
  scripts.register_queue("name", "channel")
27
32
  scripts.unregister_queue("name")
28
- Redis.new.hgetall("foo:registered_queues").should == {}
33
+
34
+ redis.with do |conn|
35
+ conn.hgetall("foo:registered_queues").should == {}
36
+ end
29
37
  end
30
38
  end
31
39
 
32
40
  describe "#registered_queues" do
33
- let(:scripts) { Scripts.new(Redis.new, "foo") }
41
+ let(:scripts) { Scripts.new(redis, "foo") }
34
42
 
35
43
  it "returns hash of all registered queues and their channels" do
36
- Redis.new.hset("foo:registered_queues", "first", "channel1")
37
- Redis.new.hset("foo:registered_queues", "second", "channel2")
44
+ redis.with do |conn|
45
+ conn.hset("foo:registered_queues", "first", "channel1")
46
+ conn.hset("foo:registered_queues", "second", "channel2")
47
+ end
48
+
38
49
  scripts.registered_queues.should == { "first" => "channel1", "second" => "channel2" }
39
50
  end
40
51
  end
41
52
 
42
53
  describe "#method_missing" do
43
- let(:scripts) { Scripts.new(Redis.new, "foo") }
54
+ let(:scripts) { Scripts.new(redis, "foo") }
44
55
 
45
56
  it "runs the script" do
46
57
  scripts.fairway_pull("namespace", "name")
@@ -48,7 +59,10 @@ module Fairway
48
59
 
49
60
  context "when the script does not exist" do
50
61
  it "loads the script" do
51
- Redis.new.script(:flush)
62
+ redis.with do |conn|
63
+ conn.script(:flush)
64
+ end
65
+
52
66
  scripts.fairway_pull("namespace", "name")
53
67
  end
54
68
  end
data/spec/spec_helper.rb CHANGED
@@ -27,10 +27,10 @@ RSpec.configure do |config|
27
27
  config.facet { |message| message[:facet] }
28
28
  end
29
29
 
30
- Fairway::Config.new.redis.flushdb
30
+ Fairway::Config.new.redis.with{ |r| r.flushdb }
31
31
  end
32
32
 
33
33
  config.after(:each) do
34
- Fairway::Config.new.redis.flushdb
34
+ Fairway::Config.new.redis.with{ |r| r.flushdb }
35
35
  end
36
36
  end
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.1.3
4
+ version: 0.1.4
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-05-25 00:00:00.000000000 Z
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: hiredis
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
46
  - !ruby/object:Gem::Dependency
63
47
  name: redis-namespace
64
48
  requirement: !ruby/object:Gem::Requirement