rotary 0.1.3 → 0.1.4

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: 012ecae133b36bcf64ab646f7a04685de660d903
4
- data.tar.gz: d8cfb02250fd5910cbec058150a4e09b1f0ad474
3
+ metadata.gz: 1e85fa11711aaad4a8bcf34825a90b0b18988be9
4
+ data.tar.gz: 09c77574e28dc21544ea6da5cca756aa4e9dca09
5
5
  SHA512:
6
- metadata.gz: 86373d0b2e2e09f21554a782d6829614cd40ca062fbc7da4756c18daa2bec8b922895d997f01f072f5a7c33b74e54cda40a1e20c34c2062a698c971f95a40b3d
7
- data.tar.gz: a3ed894449b2d5bdaa7b71e8a2ae29ba82b2af754260a412e1a952da8fccf756399ff8f4ae54fd86e5f2eb45a890c003c6382a09b785a01395b1c3b82ea9f4b7
6
+ metadata.gz: 9ad03e08f14b592110c2e6fe5977f5aef5d054b4478fa5748a8cfd6abf47ccf3f924446279b2cd59d71d0381a8c99cadfc1fe3b6395ac3202717439f654d617e
7
+ data.tar.gz: ed2dbb9e42f3a1392335e4e50795e3786d5c1b1b3e651e0291d9330855b399aeca4bfbc2e82ba31459a19822e2e6affc9028483202f617859d307f075bb1480e
data/README.md CHANGED
@@ -0,0 +1,2 @@
1
+ # rotary [![Build Status](https://secure.travis-ci.org/dredozubov/rotary.png)](http://travis-ci.org/dredozubov/rotary?branch=master) [![Dependency Status](https://gemnasium.com/dredozubov/rotary.png)](https://gemnasium.com/dredozubov/rotary) [![Code Climate](https://codeclimate.com/github/dredozubov/rotary.png)](https://codeclimate.com/github/dredozubov/rotary)
2
+ Generic pool with pluggable backends for external storage. This way pool can be used by multiple application servers. It can be used to store sessions for external services.
@@ -12,4 +12,3 @@ module Rotary
12
12
  end
13
13
  end
14
14
  end
15
-
@@ -35,4 +35,3 @@ module Rotary::Serializer
35
35
  end
36
36
  end
37
37
  end
38
-
@@ -1,3 +1,4 @@
1
+ # This storage method is not threadsafe
1
2
  module Rotary
2
3
  module Storage
3
4
  class Memory
@@ -4,6 +4,7 @@ require 'redis'
4
4
  module Rotary
5
5
  module Storage
6
6
  class Redis
7
+ DEFAULT_PREFIX = 'rotary'.freeze
7
8
 
8
9
  class Retry < Exception
9
10
  end
@@ -12,10 +13,10 @@ module Rotary
12
13
  ::Redis.new
13
14
  end
14
15
 
15
- def initialize(connection:, ttl:, serializer:, prefix: 'rotary')
16
+ def initialize(connection:, ttl:, serializer:, prefix: DEFAULT_PREFIX)
16
17
  @redis = connection
17
- @prefix = "#{prefix}::"
18
18
  @ttl = ttl # in seconds
19
+ @prefix = "#{prefix}::"
19
20
  @serializer = serializer
20
21
  @pool_list = "#{@prefix}pool"
21
22
  end
@@ -57,6 +58,37 @@ module Rotary
57
58
  @redis.del(@pool_list)
58
59
  end
59
60
 
61
+ # Removes sessions, where ttl is bigger than threshold n.
62
+ def clean_older_than(n)
63
+ len = @redis.llen(@pool_list)
64
+
65
+ # It doesn't have to happen atomically.
66
+ # New session will be lpush'ed, we can easily check only
67
+ # N sessions from the right.
68
+ len.times do
69
+ session = @redis.rpop(@pool_list)
70
+
71
+ # We have no sessions left. It can happen.
72
+ break unless session
73
+
74
+ key = ttl_key(session)
75
+ ttl_marker = @redis.ttl(key)
76
+
77
+ # redis.rb returns -2 when key doesn't exist
78
+ no_ttl = ttl_marker == -2
79
+ next if no_ttl
80
+
81
+ old = ttl_marker < (@ttl - n)
82
+ if old
83
+ # delete ttl key
84
+ @redis.del(key)
85
+ else
86
+ # push back from the left side
87
+ @redis.lpush(@pool_list, session)
88
+ end
89
+ end
90
+ end
91
+
60
92
  protected
61
93
 
62
94
  def ttl_key(obj)
@@ -65,4 +97,3 @@ module Rotary
65
97
  end
66
98
  end
67
99
  end
68
-
@@ -1,4 +1,4 @@
1
1
  module Rotary
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
4
4
 
data/lib/rotary.rb CHANGED
@@ -64,6 +64,14 @@ module Rotary
64
64
  @storage.push(obj)
65
65
  end
66
66
 
67
+ # Removes all elements from pool, which means specified by block
68
+ # argument criteria.
69
+ def clean_older_than(n)
70
+ if storage.respond_to?(:clean_older_than)
71
+ storage.clean_older_than(n)
72
+ else
73
+ fail "#{storage.class}#clean_where not implemented"
74
+ end
75
+ end
67
76
  end
68
77
  end
69
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rotary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Redozubov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-04 00:00:00.000000000 Z
11
+ date: 2014-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake