ratelimit 1.0.2 → 1.0.4
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.
- checksums.yaml +5 -5
- data/.travis.yml +5 -7
- data/CHANGELOG.md +13 -2
- data/README.md +2 -1
- data/lib/ratelimit/version.rb +1 -1
- data/lib/ratelimit.rb +11 -10
- data/ratelimit.gemspec +2 -2
- data/spec/ratelimit_spec.rb +25 -2
- metadata +10 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0b05cd16072c91b5a26037a5455e7ae1d5952eea22e0b17187a3e25be4623728
|
4
|
+
data.tar.gz: b57fdeba66adbe85f1303ff5c4bfedead5bc6ce5d7b1d4d3608da1ebc847bb5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef451adc48b2e6248231e43a633544bcd823013059a0736d4fb284e1b4a3d8ab002cec7ebae9b9c26e1aad5ffa192a35c3f7a623b1fd438a20ed4526a8d0d989
|
7
|
+
data.tar.gz: bae41807e4b35c2050353d4bdebeb0ba35b0408464901682b86e6870f6c1744162773287fcca96fc35ca2b80daa0db8a844db9f4e7ee16b5cbe46d8eb28ee36f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,21 @@
|
|
1
|
+
# 1.0.4
|
2
|
+
|
3
|
+
* #41 Remove redis deprecation warning
|
4
|
+
|
5
|
+
# 1.0.3
|
6
|
+
|
7
|
+
* #28 Fix counting the same buckets multiple times when the interval > bucket_span
|
8
|
+
|
9
|
+
# 1.0.2 09/09/2014
|
10
|
+
|
11
|
+
* #15 Raise error if bucket_count too small
|
12
|
+
|
1
13
|
# 1.0.1 08/05/2014
|
14
|
+
|
2
15
|
* #14 fix issue with wrong count on bucket_span == count(..interval) - @olgen
|
3
16
|
|
4
17
|
# 1.0.0 06/15/2014
|
5
18
|
|
6
|
-
|
7
19
|
* #7 Allow adding more than one to a counter - @Nielsomat
|
8
20
|
* #8 Add Ruby 2 support and remove Ruby 1.8 support
|
9
21
|
* #6 Clean up initialization method
|
@@ -16,7 +28,6 @@
|
|
16
28
|
* Allow non-string subject and key -- Thanks Alexey Noskov
|
17
29
|
Fix for bucket_expiry allowed to be larger than bucket_span and causing bad results -- Thanks Alexey Noskov
|
18
30
|
|
19
|
-
|
20
31
|
# 0.0.2 10/29/2011
|
21
32
|
|
22
33
|
Initial Relase
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
[](https://codeclimate.com/github/ejfinneran/ratelimit)
|
5
5
|
[](https://coveralls.io/r/ejfinneran/ratelimit)
|
6
6
|
|
7
|
-
Ratelimit provides a way to rate limit actions across multiple servers using Redis. This is a port of RateLimit.js found [here](https://github.com/chriso/redback/blob/master/lib/advanced_structures/RateLimit.js) and inspired by [this post](
|
7
|
+
Ratelimit provides a way to rate limit actions across multiple servers using Redis. This is a port of RateLimit.js found [here](https://github.com/chriso/redback/blob/master/lib/advanced_structures/RateLimit.js) and inspired by [this post](https://gist.github.com/chriso/54dd46b03155fcf555adccea822193da).
|
8
8
|
|
9
9
|
|
10
10
|
## Installation
|
@@ -58,6 +58,7 @@ You can also pass a block that will only get executed if the given threshold is
|
|
58
58
|
``` rb
|
59
59
|
ratelimit.exec_within_threshold phone_number, threshold: 10, interval: 30 do
|
60
60
|
some_rate_limited_code
|
61
|
+
ratelimit.add(phone_number)
|
61
62
|
end
|
62
63
|
```
|
63
64
|
|
data/lib/ratelimit/version.rb
CHANGED
data/lib/ratelimit.rb
CHANGED
@@ -3,7 +3,7 @@ require 'redis-namespace'
|
|
3
3
|
|
4
4
|
class Ratelimit
|
5
5
|
|
6
|
-
# Create a
|
6
|
+
# Create a Ratelimit object.
|
7
7
|
#
|
8
8
|
# @param [String] key A name to uniquely identify this rate limit. For example, 'emails'
|
9
9
|
# @param [Hash] options Options hash
|
@@ -12,7 +12,7 @@ class Ratelimit
|
|
12
12
|
# @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.
|
13
13
|
# @option options [Redis] :redis (nil) Redis client if you need to customize connection options
|
14
14
|
#
|
15
|
-
# @return [
|
15
|
+
# @return [Ratelimit] Ratelimit instance
|
16
16
|
#
|
17
17
|
def initialize(key, options = {})
|
18
18
|
@key = key
|
@@ -29,7 +29,7 @@ class Ratelimit
|
|
29
29
|
if @bucket_count < 3
|
30
30
|
raise ArgumentError.new("Cannot have less than 3 buckets")
|
31
31
|
end
|
32
|
-
@
|
32
|
+
@raw_redis = options[:redis]
|
33
33
|
end
|
34
34
|
|
35
35
|
# Add to the counter for a given subject.
|
@@ -41,11 +41,11 @@ class Ratelimit
|
|
41
41
|
def add(subject, count = 1)
|
42
42
|
bucket = get_bucket
|
43
43
|
subject = "#{@key}:#{subject}"
|
44
|
-
redis.
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
redis.multi do |transaction|
|
45
|
+
transaction.hincrby(subject, bucket, count)
|
46
|
+
transaction.hdel(subject, (bucket + 1) % @bucket_count)
|
47
|
+
transaction.hdel(subject, (bucket + 2) % @bucket_count)
|
48
|
+
transaction.expire(subject, @bucket_expiry)
|
49
49
|
end.first
|
50
50
|
end
|
51
51
|
|
@@ -55,7 +55,7 @@ class Ratelimit
|
|
55
55
|
# @param [Integer] interval How far back (in seconds) to retrieve activity.
|
56
56
|
def count(subject, interval)
|
57
57
|
bucket = get_bucket
|
58
|
-
interval = [interval, @bucket_interval].max
|
58
|
+
interval = [[interval, @bucket_interval].max, @bucket_span].min
|
59
59
|
count = (interval / @bucket_interval).floor
|
60
60
|
subject = "#{@key}:#{subject}"
|
61
61
|
|
@@ -97,6 +97,7 @@ class Ratelimit
|
|
97
97
|
# @example Send an email as long as we haven't send 5 in the last 10 minutes
|
98
98
|
# ratelimit.exec_with_threshold(email, [:threshold => 5, :interval => 600]) do
|
99
99
|
# send_another_email
|
100
|
+
# ratelimit.add(email)
|
100
101
|
# end
|
101
102
|
def exec_within_threshold(subject, options = {}, &block)
|
102
103
|
options[:threshold] ||= 30
|
@@ -114,6 +115,6 @@ class Ratelimit
|
|
114
115
|
end
|
115
116
|
|
116
117
|
def redis
|
117
|
-
@redis ||= Redis::Namespace.new(:ratelimit, :
|
118
|
+
@redis ||= Redis::Namespace.new(:ratelimit, redis: @raw_redis || Redis.new)
|
118
119
|
end
|
119
120
|
end
|
data/ratelimit.gemspec
CHANGED
@@ -18,9 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "redis", ">=
|
21
|
+
spec.add_dependency "redis", ">= 3.0.0"
|
22
22
|
spec.add_dependency "redis-namespace", ">= 1.0.0"
|
23
|
-
spec.add_development_dependency "bundler", "
|
23
|
+
spec.add_development_dependency "bundler", ">= 1.6"
|
24
24
|
spec.add_development_dependency "rake"
|
25
25
|
spec.add_development_dependency "fakeredis"
|
26
26
|
spec.add_development_dependency "timecop"
|
data/spec/ratelimit_spec.rb
CHANGED
@@ -1,6 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Ratelimit do
|
4
|
+
describe '.initialize' do
|
5
|
+
subject { described_class.new(key, options) }
|
6
|
+
|
7
|
+
let(:options) { Hash.new }
|
8
|
+
|
9
|
+
context 'with key' do
|
10
|
+
let(:key) { 'key' }
|
11
|
+
|
12
|
+
context 'with redis option' do
|
13
|
+
let(:redis) { double('redis') }
|
14
|
+
let(:options) { super().merge(redis: redis) }
|
15
|
+
|
16
|
+
it 'wraps redis in redis-namespace' do
|
17
|
+
expect(subject.send(:redis)).to be_instance_of(Redis::Namespace)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
4
22
|
|
5
23
|
before do
|
6
24
|
@r = Ratelimit.new("key")
|
@@ -75,7 +93,7 @@ describe Ratelimit do
|
|
75
93
|
expect(@r.within_bounds?("value1", {:threshold => 10, :interval => 30})).to be false
|
76
94
|
end
|
77
95
|
|
78
|
-
it "accept a
|
96
|
+
it "accept a threshold and a block that gets executed once it's below the threshold" do
|
79
97
|
expect(@r.count("key", 30)).to eq(0)
|
80
98
|
31.times do
|
81
99
|
@r.add("key")
|
@@ -100,10 +118,15 @@ describe Ratelimit do
|
|
100
118
|
end
|
101
119
|
|
102
120
|
|
103
|
-
it "counts
|
121
|
+
it "counts correctly if bucket_span equals count-interval " do
|
104
122
|
@r = Ratelimit.new("key", {:bucket_span => 10, bucket_interval: 1})
|
105
123
|
@r.add('value1')
|
106
124
|
expect(@r.count('value1', 10)).to eql(1)
|
107
125
|
end
|
108
126
|
|
127
|
+
it "counts correctly if interval is greater than bucket_span" do
|
128
|
+
@r = Ratelimit.new("key", { bucket_span: 10, bucket_interval: 1})
|
129
|
+
@r.add('value1')
|
130
|
+
expect(@r.count('value1', 40)).to eql(1)
|
131
|
+
end
|
109
132
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ratelimit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- E.J. Finneran
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: redis-namespace
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,14 +42,14 @@ dependencies:
|
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.6'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.6'
|
55
55
|
- !ruby/object:Gem::Dependency
|
@@ -178,7 +178,7 @@ homepage: https://github.com/ejfinneran/ratelimit
|
|
178
178
|
licenses:
|
179
179
|
- MIT
|
180
180
|
metadata: {}
|
181
|
-
post_install_message:
|
181
|
+
post_install_message:
|
182
182
|
rdoc_options: []
|
183
183
|
require_paths:
|
184
184
|
- lib
|
@@ -193,12 +193,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
|
-
|
197
|
-
|
198
|
-
signing_key:
|
196
|
+
rubygems_version: 3.1.4
|
197
|
+
signing_key:
|
199
198
|
specification_version: 4
|
200
199
|
summary: Rate limiting backed by redis
|
201
200
|
test_files:
|
202
201
|
- spec/ratelimit_spec.rb
|
203
202
|
- spec/spec_helper.rb
|
204
|
-
has_rdoc:
|