simple_throttle 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/.gitignore +2 -0
- data/README.md +15 -4
- data/VERSION +1 -1
- data/lib/simple_throttle.rb +8 -3
- data/simple_throttle.gemspec +1 -1
- data/spec/simple_throttle_spec.rb +1 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3891997e2673589d91401b2f7c721786264c1b9
|
4
|
+
data.tar.gz: b4bbcffae594f108495bfe7de6de20b00038f5d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2da1554c54fdd458f698c66e0a246eaf856d89e5de639f73e7a8592a820972e6a11ea90259ea6421118ed15adb043cd3cd65f40147bbb9ca7a71b87542a023b
|
7
|
+
data.tar.gz: b84de83b7b3dfbd61b2c60560b159e17e255cd125b58551620c5382b36cfdbfeb9df762a207493c62cea41639c3ec7ca274a61712f5d817f834464cafbed388c
|
data/.gitignore
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
This gem provides a very simple throttling mechanism backed by redis for limiting access to a resource.
|
1
|
+
This gem provides a very simple throttling mechanism backed by redis for limiting access to a resource. The throttle can be thought of as a limit on the number of calls in a set time frame (i.e. 100 calls per hour). These
|
2
2
|
|
3
3
|
## Usage
|
4
4
|
|
@@ -6,19 +6,30 @@ This gem provides a very simple throttling mechanism backed by redis for limitin
|
|
6
6
|
# Initialize Redis client
|
7
7
|
SimpleThrottle.set_redis(Redis.new)
|
8
8
|
|
9
|
+
# ...or provide a block that returns a redis client
|
10
|
+
SimpleThrottle.set_redis{ connection_pool.redis }
|
11
|
+
|
9
12
|
# Add a global throttle (max of 10 requests in 60 seconds)
|
10
13
|
SimpleThrottle.add(:things, limit: 10, ttl: 60)
|
11
14
|
|
12
|
-
# Throttle a resource
|
15
|
+
# Throttle a global resource
|
13
16
|
if SimpleThrottle[:things].allowed!
|
14
17
|
do_somthing
|
15
18
|
else
|
16
19
|
raise "Too many requests. Resource available in #{SimpleThrottle[:things].wait_time} seconds"
|
17
20
|
end
|
21
|
+
|
22
|
+
# Throttle resource useage per user (100 per hour)
|
23
|
+
throttle = SimpleThrottle.new("resource@#{current_user.id}", limit: 100, ttl: 3600)
|
24
|
+
if throttle.allowed!
|
25
|
+
do_somthing
|
26
|
+
else
|
27
|
+
raise "Too many requests. Resource available in #{throttle.wait_time} seconds"
|
28
|
+
end
|
18
29
|
```
|
19
30
|
|
20
|
-
Calling `
|
31
|
+
Calling `allowed!` will return `true` if the throttle limit has not yet been reached. If it does return `true`, then it will also start tracking that the call was made (hence the exclamation point syntax). There is no way to release a throttled call (that's why it's called `SimpleThrottle`).
|
21
32
|
|
22
|
-
The throttle data is kept in redis as a list of timestamps and will be auto expired if it falls out of use. The thottles time windows are rolling time windows and more calls will be allowed as soon as possible.
|
33
|
+
The throttle data is kept in redis as a list of timestamps and will be auto expired if it falls out of use. The thottles time windows are rolling time windows and more calls will be allowed as soon as possible. So, if you have a throttle of, 100 requests per hour, and the throttle kicks in, you will be able to make the next throttled call one hour after the first call being tracked, not one hour after the last call.
|
23
34
|
|
24
35
|
Redis server 2.6 or greater is required.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/lib/simple_throttle.rb
CHANGED
@@ -1,14 +1,19 @@
|
|
1
1
|
require 'redis'
|
2
|
+
require 'thread'
|
2
3
|
|
3
4
|
# Create a simple throttle that can be used to limit the number of request for a resouce
|
4
5
|
# per time period. These objects are thread safe.
|
5
6
|
class SimpleThrottle
|
6
7
|
|
8
|
+
@@lock = Mutex.new
|
9
|
+
|
7
10
|
class << self
|
8
|
-
# Add a
|
11
|
+
# Add a global throttle that can be referenced later with the [] method.
|
9
12
|
def add(name, limit:, ttl:)
|
10
|
-
|
11
|
-
|
13
|
+
@@lock.synchronize do
|
14
|
+
@throttles ||= {}
|
15
|
+
@throttles[name.to_s] = new(name, limit: limit, ttl: ttl)
|
16
|
+
end
|
12
17
|
end
|
13
18
|
|
14
19
|
# Returns a globally defined throttle with the specfied name.
|
data/simple_throttle.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ["dev@weheartit.com", "bbdurand@gmail.com"]
|
10
10
|
spec.summary = "Simple redis backed throttling mechanism to limit access to a resource"
|
11
11
|
spec.description = "Simple redis backed throttling mechanism to limit access to a resource."
|
12
|
-
spec.homepage = "https://github.com/weheartit/
|
12
|
+
spec.homepage = "https://github.com/weheartit/simple_throttle"
|
13
13
|
spec.license = "MIT"
|
14
14
|
|
15
15
|
spec.files = `git ls-files`.split($/)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_throttle
|
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
|
- We Heart It
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-03-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -75,6 +75,7 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
+
- ".gitignore"
|
78
79
|
- MIT_LICENSE.txt
|
79
80
|
- README.md
|
80
81
|
- Rakefile
|
@@ -83,7 +84,7 @@ files:
|
|
83
84
|
- simple_throttle.gemspec
|
84
85
|
- spec/simple_throttle_spec.rb
|
85
86
|
- spec/spec_helper.rb
|
86
|
-
homepage: https://github.com/weheartit/
|
87
|
+
homepage: https://github.com/weheartit/simple_throttle
|
87
88
|
licenses:
|
88
89
|
- MIT
|
89
90
|
metadata: {}
|
@@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
104
|
version: '0'
|
104
105
|
requirements: []
|
105
106
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
107
|
+
rubygems_version: 2.6.12
|
107
108
|
signing_key:
|
108
109
|
specification_version: 4
|
109
110
|
summary: Simple redis backed throttling mechanism to limit access to a resource
|