ratelimit 0.0.2 → 0.0.3
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/ratelimit.rb +6 -5
- metadata +6 -6
data/lib/ratelimit.rb
CHANGED
@@ -10,15 +10,16 @@ class Ratelimit
|
|
10
10
|
# @param [Hash] options Options hash
|
11
11
|
# @option options [Integer] :bucket_span (600) Time span to track in seconds
|
12
12
|
# @option options [Integer] :bucket_interval (5) How many seconds each bucket represents
|
13
|
-
# @option options [Integer] :bucket_expiry (
|
13
|
+
# @option options [Integer] :bucket_expiry (@bucket_span) How long we keep data in each bucket before it is auto expired. Cannot be larger than the bucket_span.
|
14
14
|
#
|
15
15
|
# @return [RateLimit] RateLimit instance
|
16
16
|
#
|
17
|
-
def initialize(key, redis = nil, options = {})
|
17
|
+
def initialize(key, redis = nil, options = {})
|
18
18
|
@key = key
|
19
19
|
@bucket_span = options[:bucket_span] || 600
|
20
20
|
@bucket_interval = options[:bucket_interval] || 5
|
21
|
-
@bucket_expiry = options[:bucket_expiry] ||
|
21
|
+
@bucket_expiry = options[:bucket_expiry] || @bucket_span
|
22
|
+
raise ArgumentError.new("Bucket expiry cannot be larger than the bucket span") if @bucket_expiry > @bucket_span
|
22
23
|
@bucket_count = (@bucket_span / @bucket_interval).round
|
23
24
|
@redis = redis
|
24
25
|
end
|
@@ -28,7 +29,7 @@ class Ratelimit
|
|
28
29
|
# @param [String] subject A unique key to identify the subject. For example, 'user@foo.com'
|
29
30
|
def add(subject)
|
30
31
|
bucket = get_bucket
|
31
|
-
subject = @key
|
32
|
+
subject = "#{@key}:#{subject}"
|
32
33
|
redis.multi do
|
33
34
|
redis.hincrby(subject, bucket, 1)
|
34
35
|
redis.hdel(subject, (bucket + 1) % @bucket_count)
|
@@ -45,7 +46,7 @@ class Ratelimit
|
|
45
46
|
bucket = get_bucket
|
46
47
|
interval = [interval, @bucket_interval].max
|
47
48
|
count = (interval / @bucket_interval).floor
|
48
|
-
subject = @key
|
49
|
+
subject = "#{@key}:#{subject}"
|
49
50
|
counts = redis.multi do
|
50
51
|
redis.hget(subject, bucket)
|
51
52
|
count.downto(1) do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ratelimit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-10-29 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
16
|
-
requirement: &
|
16
|
+
requirement: &13383120 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *13383120
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: redis-namespace
|
27
|
-
requirement: &
|
27
|
+
requirement: &13382560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *13382560
|
36
36
|
description: This library uses Redis to track the number of actions for a given subject
|
37
37
|
over a flexible time frame.
|
38
38
|
email:
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
64
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.8.
|
65
|
+
rubygems_version: 1.8.10
|
66
66
|
signing_key:
|
67
67
|
specification_version: 3
|
68
68
|
summary: Rate limiting backed by redis
|