redis-rack 2.0.4 → 2.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea9e8af24cb2b8cef86500d98934f84122c07443
4
- data.tar.gz: 7d86691418586edeeaa29732185ed94db70b4818
3
+ metadata.gz: 66572ef2577985ef4e66d8eff099b41cd2522452
4
+ data.tar.gz: 021ff43d18f10505f52187456132922eddea15b8
5
5
  SHA512:
6
- metadata.gz: 1c0eef6bd5df36c175f431075feb97de8c582cc6bf5df53b95b58977a125053071dc200ac17b3524280850142c72e0f41950ddf581a30aa1c85c82123429e1be
7
- data.tar.gz: da4347e50edbed58a98a461caa57791819195cdd8ba6458144f0e4d0080604eaed0613d6f190479c7970a477a2d14542490f70988b03ca602a99f98b6a2bf845
6
+ metadata.gz: dd6ac3b843ebadac7e56d802680badd2163ac0320a90377e94f730465c739ee74738e5c2b347a34c4fae1b6d94160a8f988c8e748ab540c820750ed7792b2ae0
7
+ data.tar.gz: b26ae6bfb3306d19260454d41ce2559e88997365bfc713c10fd33168b8e70ea052b45be7fed5e5370a8c36b0b66c5415935df19d77ae2a6cef1db64fd925e36c
data/README.md CHANGED
@@ -42,6 +42,11 @@ require 'rack'
42
42
  require 'rack/session/redis'
43
43
 
44
44
  use Rack::Session::Redis
45
+
46
+ # Alternatively you can specify options to use:
47
+ use Rack::Session::Redis,
48
+ :redis_server => "redis://redis:6379/0",
49
+ :expires_in => 3600 # Seconds. If you are using ActiveSupport you can use 1.hour
45
50
  ```
46
51
 
47
52
  ## Development
@@ -1,38 +1,26 @@
1
1
  require 'rack/session/abstract/id'
2
2
  require 'redis-store'
3
3
  require 'thread'
4
+ require 'redis/rack/connection'
4
5
 
5
6
  module Rack
6
7
  module Session
7
8
  class Redis < Abstract::ID
8
- attr_reader :mutex, :pool
9
+ attr_reader :mutex
9
10
 
10
- DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge \
11
+ DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge(
11
12
  :redis_server => 'redis://127.0.0.1:6379/0/rack:session'
13
+ )
12
14
 
13
15
  def initialize(app, options = {})
14
16
  super
15
17
 
16
18
  @mutex = Mutex.new
17
- @pool = if @default_options[:pool]
18
- raise "pool must be an instance of ConnectionPool" unless @default_options[:pool].is_a?(ConnectionPool)
19
- @pooled = true
20
- @default_options[:pool]
21
- elsif [:pool_size, :pool_timeout].any? { |key| @default_options.has_key?(key) }
22
- pool_options = {}
23
- pool_options[:size] = options[:pool_size] if options[:pool_size]
24
- pool_options[:timeout] = options[:pool_timeout] if options[:pool_timeout]
25
- @pooled = true
26
- ::ConnectionPool.new(pool_options) { ::Redis::Store::Factory.create(@default_options[:redis_server]) }
27
- else
28
- @default_options.has_key?(:redis_store) ?
29
- @default_options[:redis_store] :
30
- ::Redis::Store::Factory.create(@default_options[:redis_server])
31
-
32
- end
19
+ @conn = ::Redis::Rack::Connection.new(@default_options)
33
20
  end
34
21
 
35
22
  def generate_unique_sid(session)
23
+ return generate_sid if session.empty?
36
24
  loop do
37
25
  sid = generate_sid
38
26
  first = with do |c|
@@ -88,13 +76,8 @@ module Rack
88
76
  end
89
77
 
90
78
  def with(&block)
91
- if @pooled
92
- @pool.with(&block)
93
- else
94
- block.call(@pool)
95
- end
79
+ @conn.with(&block)
96
80
  end
97
-
98
81
  end
99
82
  end
100
83
  end
@@ -0,0 +1,42 @@
1
+ class Redis
2
+ module Rack
3
+ class Connection
4
+ def initialize(options = {})
5
+ @options = options
6
+ @store = options[:redis_store]
7
+ @pool = options[:pool]
8
+
9
+ if @pool && !@pool.is_a?(ConnectionPool)
10
+ raise ArgumentError, "pool must be an instance of ConnectionPool"
11
+ end
12
+ end
13
+
14
+ def with(&block)
15
+ if pooled?
16
+ pool.with(&block)
17
+ else
18
+ block.call(store)
19
+ end
20
+ end
21
+
22
+ def pooled?
23
+ [:pool, :pool_size, :pool_timeout].any? { |key| @options.key?(key) }
24
+ end
25
+
26
+ def pool
27
+ @pool ||= ConnectionPool.new(pool_options) { store } if pooled?
28
+ end
29
+
30
+ def store
31
+ @store ||= Redis::Store::Factory.create(@options[:redis_server])
32
+ end
33
+
34
+ def pool_options
35
+ {
36
+ size: @options[:pool_size],
37
+ timeout: @options[:pool_timeout]
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,6 +1,6 @@
1
1
  class Redis
2
2
  module Rack
3
- VERSION = '2.0.4'
3
+ VERSION = '2.0.5'
4
4
  end
5
5
  end
6
6
 
@@ -39,8 +39,9 @@ describe Rack::Session::Redis do
39
39
 
40
40
  it "can create it's own pool" do
41
41
  session_store = Rack::Session::Redis.new(incrementor, pool_size: 5, pool_timeout: 10)
42
- session_store.pool.class.must_equal ::ConnectionPool
43
- session_store.pool.instance_variable_get(:@size).must_equal 5
42
+ conn = session_store.instance_variable_get(:@conn)
43
+ conn.pool.class.must_equal ::ConnectionPool
44
+ conn.pool.instance_variable_get(:@size).must_equal 5
44
45
  end
45
46
 
46
47
  it "can create it's own pool using default Redis server" do
@@ -55,30 +56,33 @@ describe Rack::Session::Redis do
55
56
 
56
57
  it "can use a supplied pool" do
57
58
  session_store = Rack::Session::Redis.new(incrementor, pool: ::ConnectionPool.new(size: 1, timeout: 1) { ::Redis::Store::Factory.create("redis://127.0.0.1:6380/1")})
58
- session_store.pool.class.must_equal ::ConnectionPool
59
- session_store.pool.instance_variable_get(:@size).must_equal 1
59
+ conn = session_store.instance_variable_get(:@conn)
60
+ conn.pool.class.must_equal ::ConnectionPool
61
+ conn.pool.instance_variable_get(:@size).must_equal 1
60
62
  end
61
63
 
62
64
  it "uses the specified Redis store when provided" do
63
65
  store = ::Redis::Store::Factory.create('redis://127.0.0.1:6380/1')
64
66
  pool = Rack::Session::Redis.new(incrementor, :redis_store => store)
65
- pool.pool.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
66
- pool.pool.must_equal(store)
67
+ pool.with do |p|
68
+ p.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
69
+ p.must_equal(store)
70
+ end
67
71
  end
68
72
 
69
73
  it "uses the default Redis server and namespace when not provided" do
70
74
  pool = Rack::Session::Redis.new(incrementor)
71
- pool.pool.to_s.must_match(/127\.0\.0\.1:6379 against DB 0 with namespace rack:session$/)
75
+ pool.with { |p| p.to_s.must_match(/127\.0\.0\.1:6379 against DB 0 with namespace rack:session$/) }
72
76
  end
73
77
 
74
78
  it "uses the specified namespace when provided" do
75
79
  pool = Rack::Session::Redis.new(incrementor, :redis_server => {:namespace => 'test:rack:session'})
76
- pool.pool.to_s.must_match(/namespace test:rack:session$/)
80
+ pool.with { |p| p.to_s.must_match(/namespace test:rack:session$/) }
77
81
  end
78
82
 
79
83
  it "uses the specified Redis server when provided" do
80
84
  pool = Rack::Session::Redis.new(incrementor, :redis_server => 'redis://127.0.0.1:6380/1')
81
- pool.pool.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
85
+ pool.with { |p| p.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/) }
82
86
  end
83
87
 
84
88
  it "is threadsafe by default" do
@@ -86,6 +90,12 @@ describe Rack::Session::Redis do
86
90
  sesion_store.threadsafe?.must_equal(true)
87
91
  end
88
92
 
93
+ it "does not store a blank session" do
94
+ session_store = Rack::Session::Redis.new(incrementor)
95
+ sid = session_store.generate_unique_sid({})
96
+ session_store.with { |c| c.get(sid).must_be_nil }
97
+ end
98
+
89
99
  it "locks the store mutex" do
90
100
  mutex = Mutex.new
91
101
  mutex.expects(:lock).once
@@ -260,15 +270,12 @@ describe Rack::Session::Redis do
260
270
  end
261
271
 
262
272
  it "does not hit with :skip option" do
263
- with_pool_management(incrementor) do |pool|
264
- skip = Rack::Utils::Context.new(pool, skip_session)
273
+ with_pool_management(incrementor) do |session_store|
274
+ skip = Rack::Utils::Context.new(session_store, skip_session)
265
275
  sreq = Rack::MockRequest.new(skip)
266
276
 
267
- pool.instance_variable_set('@pool', MiniTest::Mock.new)
268
-
269
277
  res0 = sreq.get("/")
270
278
  res0.body.must_equal('{"counter"=>1}')
271
- assert pool.pool.verify
272
279
  end
273
280
  end
274
281
 
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+ require 'connection_pool'
3
+ require 'redis/rack/connection'
4
+
5
+ class Redis
6
+ module Rack
7
+ describe Connection do
8
+ def setup
9
+ @defaults = {
10
+ host: 'localhost'
11
+ }
12
+ end
13
+
14
+ it "can create it's own pool" do
15
+ conn = Connection.new @defaults.merge(pool_size: 5, pool_timeout: 10)
16
+
17
+ conn.pooled?.must_equal true
18
+ conn.pool.class.must_equal ConnectionPool
19
+ conn.pool.instance_variable_get(:@size).must_equal 5
20
+ end
21
+
22
+ it "can create it's own pool using default Redis server" do
23
+ conn = Connection.new @defaults.merge(pool_size: 5, pool_timeout: 10)
24
+
25
+ conn.pooled?.must_equal true
26
+
27
+ conn.with do |connection|
28
+ connection.to_s.must_match(/127\.0\.0\.1:6379 against DB 0$/)
29
+ end
30
+ end
31
+
32
+ it "can create it's own pool using provided Redis server" do
33
+ conn = Connection.new(redis_server: 'redis://127.0.0.1:6380/1', pool_size: 5, pool_timeout: 10)
34
+ conn.pooled?.must_equal true
35
+ conn.with do |connection|
36
+ connection.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
37
+ end
38
+ end
39
+
40
+ it "can use a supplied pool" do
41
+ pool = ConnectionPool.new size: 1, timeout: 1 do
42
+ ::Redis::Store::Factory.create('redis://127.0.0.1:6380/1')
43
+ end
44
+ conn = Connection.new pool: pool
45
+ conn.pooled?.must_equal true
46
+ conn.pool.class.must_equal ConnectionPool
47
+ conn.pool.instance_variable_get(:@size).must_equal 1
48
+ end
49
+
50
+ it "uses the specified Redis store when provided" do
51
+ store = ::Redis::Store::Factory.create('redis://127.0.0.1:6380/1')
52
+ conn = Connection.new(redis_store: store)
53
+
54
+ conn.pooled?.must_equal false
55
+ conn.store.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
56
+ conn.store.must_equal(store)
57
+ end
58
+
59
+ it "uses the specified Redis server when provided" do
60
+ conn = Connection.new(redis_server: 'redis://127.0.0.1:6380/1')
61
+
62
+ conn.pooled?.must_equal false
63
+ conn.store.to_s.must_match(/127\.0\.0\.1:6380 against DB 1$/)
64
+ end
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.4
4
+ version: 2.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-18 00:00:00.000000000 Z
11
+ date: 2018-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-store
@@ -168,9 +168,11 @@ files:
168
168
  - gemfiles/rack_2.gemfile
169
169
  - lib/rack/session/redis.rb
170
170
  - lib/redis-rack.rb
171
+ - lib/redis/rack/connection.rb
171
172
  - lib/redis/rack/version.rb
172
173
  - redis-rack.gemspec
173
174
  - test/rack/session/redis_test.rb
175
+ - test/redis/rack/connection_test.rb
174
176
  - test/test_helper.rb
175
177
  homepage: http://redis-store.org/redis-rack
176
178
  licenses:
@@ -198,4 +200,5 @@ specification_version: 4
198
200
  summary: Redis Store for Rack
199
201
  test_files:
200
202
  - test/rack/session/redis_test.rb
203
+ - test/redis/rack/connection_test.rb
201
204
  - test/test_helper.rb