ratelimit 1.0.2 → 1.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.
- checksums.yaml +4 -4
- data/.travis.yml +5 -7
- data/README.md +2 -1
- data/lib/ratelimit.rb +5 -4
- data/lib/ratelimit/version.rb +1 -1
- data/spec/ratelimit_spec.rb +25 -2
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ca5c11e810378b9f764fc4df1a9eb95c5be682f
|
4
|
+
data.tar.gz: 29be916842600285b975409daa377a6ef0a47d7b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8bf4fdb9ccb0e298870c1cb6e19d44e90d9fb92d5f5f311dd7d68eff139150c38b1a7bfcd3175c0cff317b3d96fbe02c72f20a4acdf26243cd3c28f4a74d0b3
|
7
|
+
data.tar.gz: fa1910970d4f93ec8e5c947012344a002bb0e8f7ed56b130fb8b454ae9ff373a140dd2e9e4865bd13772e51ba5485ca2448f0c2165f92d7549c41756d8935bbf
|
data/.travis.yml
CHANGED
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.rb
CHANGED
@@ -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,7 +41,7 @@ class Ratelimit
|
|
41
41
|
def add(subject, count = 1)
|
42
42
|
bucket = get_bucket
|
43
43
|
subject = "#{@key}:#{subject}"
|
44
|
-
redis.
|
44
|
+
redis.multi do
|
45
45
|
redis.hincrby(subject, bucket, count)
|
46
46
|
redis.hdel(subject, (bucket + 1) % @bucket_count)
|
47
47
|
redis.hdel(subject, (bucket + 2) % @bucket_count)
|
@@ -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/lib/ratelimit/version.rb
CHANGED
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- E.J. Finneran
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -201,4 +201,3 @@ summary: Rate limiting backed by redis
|
|
201
201
|
test_files:
|
202
202
|
- spec/ratelimit_spec.rb
|
203
203
|
- spec/spec_helper.rb
|
204
|
-
has_rdoc:
|