ratelimit 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +19 -0
- data/README.md +31 -19
- data/lib/ratelimit.rb +2 -2
- data/lib/ratelimit/version.rb +1 -1
- data/spec/ratelimit_spec.rb +8 -0
- metadata +3 -3
- data/CHANGELOG +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e82db7b9218b7b7df79470dc1220e90df8f5bc2
|
4
|
+
data.tar.gz: 73ab3802584dd987f5cb944335e37958ebdcad08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cef293468c29242233d60053461d244ef025d85eb3b593684ec8ec7de76c9eb40c12a625532225aa27e14e77c5c9a83410400c5c909b983c43eda4f44c9ec87f
|
7
|
+
data.tar.gz: 71029040e9883fb9949b08d47cb69abcef9d9b93a02a42758c22bffffa67b3c41db617e6a8ef80baadbd500b5410b9deb2541c0bde4d697efa4b5082c06b5665
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
ruby-2.1.
|
1
|
+
ruby-2.1.2
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# 1.0.0 06/15/2014
|
2
|
+
|
3
|
+
|
4
|
+
* #7 Allow adding more than one to a counter - @Nielsomat
|
5
|
+
* #8 Add Ruby 2 support and remove Ruby 1.8 support
|
6
|
+
* #6 Clean up initialization method
|
7
|
+
* #11 Optimize Redis Commits - @lautis
|
8
|
+
* #10 Add a return value - @lautis
|
9
|
+
|
10
|
+
|
11
|
+
# 0.0.3 11/08/2011
|
12
|
+
|
13
|
+
* Allow non-string subject and key -- Thanks Alexey Noskov
|
14
|
+
Fix for bucket_expiry allowed to be larger than bucket_span and causing bad results -- Thanks Alexey Noskov
|
15
|
+
|
16
|
+
|
17
|
+
# 0.0.2 10/29/2011
|
18
|
+
|
19
|
+
Initial Relase
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Ratelimit: Slow your roll
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/ejfinneran/ratelimit.
|
4
|
-
[![Code Climate](https://
|
5
|
-
[![Coverage Status](https://
|
3
|
+
[![Build Status](https://secure.travis-ci.org/ejfinneran/ratelimit.svg?branch=master)](http://travis-ci.org/ejfinneran/ratelimit)
|
4
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/ejfinneran/ratelimit.svg)](https://codeclimate.com/github/ejfinneran/ratelimit)
|
5
|
+
[![Coverage Status](https://img.shields.io/coveralls/ejfinneran/ratelimit.svg)](https://coveralls.io/r/ejfinneran/ratelimit)
|
6
6
|
|
7
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](http://chris6f.com/rate-limiting-with-redis).
|
8
8
|
|
@@ -11,15 +11,21 @@ Ratelimit provides a way to rate limit actions across multiple servers using Red
|
|
11
11
|
|
12
12
|
Add this line to your application's Gemfile:
|
13
13
|
|
14
|
-
|
14
|
+
``` rb
|
15
|
+
gem 'ratelimit'
|
16
|
+
```
|
15
17
|
|
16
18
|
And then execute:
|
17
19
|
|
18
|
-
|
20
|
+
``` sh
|
21
|
+
$ bundle
|
22
|
+
```
|
19
23
|
|
20
24
|
Or install it yourself as:
|
21
25
|
|
22
|
-
|
26
|
+
``` sh
|
27
|
+
$ gem install ratelimit
|
28
|
+
```
|
23
29
|
|
24
30
|
## Usage
|
25
31
|
|
@@ -29,25 +35,31 @@ Add to the count for a given subject via add with a unique key. I've used the ex
|
|
29
35
|
|
30
36
|
You can then fetch the number of executions for given interval in seconds via the count method.
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
``` rb
|
39
|
+
ratelimit = Ratelimit.new("messages")
|
40
|
+
5.times do
|
41
|
+
ratelimit.add(phone_number)
|
42
|
+
end
|
43
|
+
ratelimit.count(phone_number, 30)
|
44
|
+
# => 5
|
45
|
+
```
|
38
46
|
|
39
47
|
You can check if a given threshold has been exceeded or not. The following code checks if the currently rate is over 10 executions in the last 30 seconds or not.
|
40
48
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
49
|
+
``` rb
|
50
|
+
ratelimit.exceeded?(phone_number, threshold: 10, interval: 30)
|
51
|
+
# => false
|
52
|
+
ratelimit.within_bounds?(phone_number, threshold: 10, interval: 30)
|
53
|
+
# => true
|
54
|
+
```
|
45
55
|
|
46
56
|
You can also pass a block that will only get executed if the given threshold is within bounds. Beware, this code blocks until the block can be run.
|
47
57
|
|
48
|
-
|
49
|
-
|
50
|
-
|
58
|
+
``` rb
|
59
|
+
ratelimit.exec_within_threshold phone_number, threshold: 10, interval: 30 do
|
60
|
+
some_rate_limited_code
|
61
|
+
end
|
62
|
+
```
|
51
63
|
|
52
64
|
## Documentation
|
53
65
|
|
data/lib/ratelimit.rb
CHANGED
@@ -56,8 +56,8 @@ class Ratelimit
|
|
56
56
|
count = (interval / @bucket_interval).floor
|
57
57
|
subject = "#{@key}:#{subject}"
|
58
58
|
|
59
|
-
keys = (0..count).map do |i|
|
60
|
-
(bucket - i
|
59
|
+
keys = (0..count - 1).map do |i|
|
60
|
+
(bucket - i) % @bucket_count
|
61
61
|
end
|
62
62
|
return redis.hmget(subject, *keys).inject(0) {|a, i| a + i.to_i}
|
63
63
|
end
|
data/lib/ratelimit/version.rb
CHANGED
data/spec/ratelimit_spec.rb
CHANGED
@@ -92,4 +92,12 @@ describe Ratelimit do
|
|
92
92
|
end
|
93
93
|
expect(@value).to be 1
|
94
94
|
end
|
95
|
+
|
96
|
+
|
97
|
+
it "counts correclty if bucket_span equals count-interval " do
|
98
|
+
@r = Ratelimit.new("key", {:bucket_span => 10, bucket_interval: 1})
|
99
|
+
@r.add('value1')
|
100
|
+
expect(@r.count('value1', 10)).to eql(1)
|
101
|
+
end
|
102
|
+
|
95
103
|
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.1
|
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: 2014-06
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: redis
|
@@ -164,7 +164,7 @@ files:
|
|
164
164
|
- ".ruby-gemset"
|
165
165
|
- ".ruby-version"
|
166
166
|
- ".travis.yml"
|
167
|
-
- CHANGELOG
|
167
|
+
- CHANGELOG.md
|
168
168
|
- Gemfile
|
169
169
|
- LICENSE.txt
|
170
170
|
- README.md
|
data/CHANGELOG
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
0.0.2 10/29/2011 38422666592c32dc079fd46bb41b5f526a7cf1d2
|
2
|
-
=========================================================
|
3
|
-
|
4
|
-
Initial Relase
|
5
|
-
|
6
|
-
|
7
|
-
0.0.3 11/08/2011 b5b81dcc488f231b6a7abbdeb87c7ddf247b4029
|
8
|
-
=========================================================
|
9
|
-
|
10
|
-
Allow non-string subject and key -- Thanks Alexey Noskov
|
11
|
-
Fix for bucket_expiry allowed to be larger than bucket_span and causing bad results -- Thanks Alexey Noskov
|