redis-gcra 0.2.0 → 0.3.0

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: '085c8c8eb0a8f54f613cec4551f777bf0ad3e49d'
4
- data.tar.gz: fad812c225a86dd55234fd51e68911f6099f3c79
3
+ metadata.gz: d4ce10313659f0a4e111a77b037e45afff5e44f9
4
+ data.tar.gz: 791b63e5937c82a7e0757064edb45016bb90e3df
5
5
  SHA512:
6
- metadata.gz: 8458a2915d214ea6f8c9daa247aa92ed25e46e61a42c4d274969970efd1fda3b7fa109f86afb4fcbe2c4bde1d6f6f1839ef0568324a1a56ca76010badcd79fb9
7
- data.tar.gz: 75058e691d72f31eb6a147e157b112fd4968dff4d9897fae3a5a8bda6655c5adf44f51bfebedc5456e6fe8f6327ac8925f2a07fe6012eef623c5c8d120c13a41
6
+ metadata.gz: 4dd33796a611fe9c0d57eae0ba2ff2d7b95a38c8f3da96e98b42f9a9c57a923b53d412c103d43319e2a20c019692be07a8ad9f81366d1ba1e7a85b1fdafe9120
7
+ data.tar.gz: 581125696884139ac3b83972aac95c859a9ee4c163e64b1b02a24e2b52705374dae48e5f76b08da84d240bf935fa60123147391ed3b048ca8d9ebd53e38970da
data/README.md CHANGED
@@ -1,10 +1,11 @@
1
1
  # RedisGCRA
2
2
  [![Build Status](https://travis-ci.org/rwz/redis-gcra.svg?branch=master)](https://travis-ci.org/rwz/redis-gcra)
3
3
 
4
- This gem is an implementation of GCRA for rate limiting based on Redis. The
5
- code requires Redis version 3.2 or newer since it relies on
4
+ This gem is an implementation of [GCRA][gcra] for rate limiting based on Redis.
5
+ The code requires Redis version 3.2 or newer since it relies on
6
6
  [`replicate_commands`][redis-replicate-commands] feature.
7
7
 
8
+ [gcra]: https://en.wikipedia.org/wiki/Generic_cell_rate_algorithm
8
9
  [redis-replicate-commands]: https://redis.io/commands/eval#replicating-commands-instead-of-scripts
9
10
 
10
11
  ## Installation
@@ -41,16 +42,16 @@ result = RedisGCRA.limit(
41
42
  )
42
43
 
43
44
  result.limited? # => false - request should not be limited
44
- result.remaning # => 998 - remaining number of requests until limited
45
+ result.remaining # => 998 - remaining number of requests until limited
45
46
  result.retry_after # => nil - can retry without delay
46
47
  result.reset_after # => ~0.6 - in 0.6 seconds rate limiter will completely reset
47
48
 
48
49
  # call limit 499 more times in rapid succession and you get:
49
50
 
50
- result.limited? # => true - request should be limited
51
- result.remaining # => 0 - no requests can be made at this point
52
- result.retry_after # => ~1.1 - can retry in 1.1 seconds
53
- result.reset_after # => ~600 - in 600 seconds rate limiter will completely reset
51
+ result.limited? # => true - request should be limited
52
+ result.remaining # => 0 - no requests can be made at this point
53
+ result.retry_after # => ~1.2 - can retry in 1.2 seconds
54
+ result.reset_after # => ~600 - in 600 seconds rate limiter will completely reset
54
55
  ```
55
56
 
56
57
  The implementation utilizes single key in Redis that matches the key you pass
@@ -64,7 +65,7 @@ redis.del "overall-account/bob@example.com"
64
65
  ```
65
66
 
66
67
  You call also retrieve the current state of rate limiter for particular key
67
- without actually modifying the state. Of order to do that, use the `peek`
68
+ without actually modifying the state. In order to do that, use the `peek`
68
69
  method:
69
70
 
70
71
  ```ruby
@@ -76,14 +77,23 @@ RedisGCRA.peek(
76
77
  period: 60 # seconds
77
78
  )
78
79
 
79
- result.limited? # => true - current state is limited
80
- result.remaining # => 0 - no requests can be made
81
- result.retry_after # => nil - peek always return nil here
82
- result.reset_after # => ~600 - in 600 seconds rate limiter will completely reset
80
+ result.limited? # => true - current state is limited
81
+ result.remaining # => 0 - no requests can be made
82
+ result.retry_after # => ~0.6 - in 0.6 seconds remaining will become 1
83
+ result.reset_after # => ~600 - in 600 seconds rate limiter will completely reset
83
84
  ```
84
85
 
86
+ ## Inspiration
87
+
88
+ This code was inspired by this great [blog post][blog-post] by [Brandur
89
+ Leach][brandur] and his amazing work on [throttled Go package][throttled].
90
+
91
+ [blog-post]: https://brandur.org/rate-limiting
92
+ [brandur]: https://github.com/brandur
93
+ [throttled]: https://github.com/throttled/throttled
94
+
85
95
  ## License
86
96
 
87
- The gem is available as open source under the terms of the [MIT
88
- License](http://opensource.org/licenses/MIT).
97
+ The gem is available as open source under the terms of the [MIT License][mit].
89
98
 
99
+ [mit]: http://opensource.org/licenses/MIT
@@ -1,3 +1,3 @@
1
1
  module RedisGCRA
2
- VERSION = "0.2.0".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
@@ -31,15 +31,14 @@ if reset_after == 0 then
31
31
  end
32
32
 
33
33
  local limited
34
+ local retry_after
34
35
 
35
36
  if remaining == 0 then
36
37
  limited = 1
38
+ retry_after = emission_interval - diff
37
39
  else
38
40
  limited = 0
41
+ retry_after = -1
39
42
  end
40
43
 
41
- -- retry_after is always nil because it doesn't make much sense in inspect
42
- -- context
43
- local retry_after = -1
44
-
45
44
  return {limited, remaining, tostring(retry_after), tostring(reset_after)}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-gcra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Pravosud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-23 00:00:00.000000000 Z
11
+ date: 2017-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis