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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5d75f7ea1ae6ba61f7ec9894e4247259d3195f8
4
- data.tar.gz: b1cb14758b9d5729ab9cae5baa22c8cbd40eb9b1
3
+ metadata.gz: d3891997e2673589d91401b2f7c721786264c1b9
4
+ data.tar.gz: b4bbcffae594f108495bfe7de6de20b00038f5d8
5
5
  SHA512:
6
- metadata.gz: 78c8fd5675425a14094a04ff7f745a8da481d51741da8605ff3f460d77160b6421b30ae6ebba9bb412c45a28a0df8bc502c4a2dd19bd4cf8f87056ba753dcee3
7
- data.tar.gz: 3553b9f03f048bb96b83935ff93fede2c034d83e9656d32e314e9c8cacec8bf681715c593663e4ce357ba41c59d075b153ecce581af853755c1479565c1a386d
6
+ metadata.gz: d2da1554c54fdd458f698c66e0a246eaf856d89e5de639f73e7a8592a820972e6a11ea90259ea6421118ed15adb043cd3cd65f40147bbb9ca7a71b87542a023b
7
+ data.tar.gz: b84de83b7b3dfbd61b2c60560b159e17e255cd125b58551620c5382b36cfdbfeb9df762a207493c62cea41639c3ec7ca274a61712f5d817f834464cafbed388c
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ pkg
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 `allow!` will return true if the throttle limit has not yet been reached and will also start tracking a new call if it returned true. There is no way to release a throttled call (that's why it's called SimpleThrottle).
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.0
1
+ 1.0.1
@@ -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 new throttle that can be referenced later with the [] method.
11
+ # Add a global throttle that can be referenced later with the [] method.
9
12
  def add(name, limit:, ttl:)
10
- @throttles ||= {}
11
- @throttles[name.to_s] = new(name, limit: limit, ttl: ttl)
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.
@@ -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/sidekiq_fast_enq"
12
+ spec.homepage = "https://github.com/weheartit/simple_throttle"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
@@ -37,6 +37,7 @@ describe SimpleThrottle do
37
37
  expect(throttle.allowed!).to eq false
38
38
  sleep(0.3)
39
39
  expect(throttle.allowed!).to eq true
40
+ expect(throttle.allowed!).to eq false
40
41
  end
41
42
 
42
43
  it "should be able to add global throttles" do
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.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: 2016-05-16 00:00:00.000000000 Z
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/sidekiq_fast_enq
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.4.5
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