redis_token_bucket 0.1.0 → 0.2.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: e1991459cf61f197e4b366ade53dd41ecbaf26bd
4
- data.tar.gz: 4931d16f9aa7fb622bf167fdbc74d4781bbdc548
3
+ metadata.gz: b5f3614cd42c5a2d7918d03e4e36c5d0bc6feb5f
4
+ data.tar.gz: 02d7546e49f27969c21f18986db43a6af57d6362
5
5
  SHA512:
6
- metadata.gz: 3aa2fdf2aca0e7c374cd6845dece306ef0de74651f50b17328269d0abb5a38f9d742ae32ed9fb25119bd5922e166d00aceb42ce2574dae1da02df712d31e581a
7
- data.tar.gz: 7f99aef70badd3708e784d240a81c1fa0f1c25a543f6d7fb66d40f7c5e7eb3d6cb4c7e64d2985aeb90aab834ea652ed8f940109fd4f39214e303d33e01de7ccc
6
+ metadata.gz: 15a6040f4b8b733ab9272b7d0232375a2dff33555a4f14e5c14339b056e76c6d8fc582ee0de141356259ac59e06f65b93dd5d38302a74a61524d3ff0f0c4a677
7
+ data.tar.gz: bab9137d9a5ab47c79eff97a3f560c0938c0a1e00117e3deb72a81b7447062e01edec45abaaf4f37b7166abb1465c605f22240ef87e2772ab555a8ec9027ebf5
data/README.md CHANGED
@@ -9,11 +9,12 @@ Features:
9
9
  * Buckets are automatically removed when no longer used
10
10
  * Fast and concurrency safe
11
11
  * Each operation uses just a single network roundtrip to Redis
12
- * Charging tokens is done with all-or-nothing semantics
12
+ * Charging tokens can be done with all-or-nothing semantics
13
13
  * Computed continuously
14
14
  * Token values (rate, size, current level, cost) use floating point numbers
15
15
  * Bucket level is computed with microsecond precision
16
16
  * Powerful and flexible
17
+ * Ability to use take-what-is-there semantics (instead of all-or-nothing)
17
18
  * Ability to charge multiple buckets with arbitrary token amounts at once
18
19
  * Ability to "reserve" tokens and to create "token debt"
19
20
 
@@ -113,6 +114,19 @@ puts "The current level of tokens in bucket short: #{levels[short_bucket[:key]]}
113
114
  puts "The current level of tokens in bucket long: #{levels[long_bucket[:key]]}"
114
115
  ```
115
116
 
117
+ Charging with "take-what-is-there" semantics:
118
+
119
+ ```ruby
120
+
121
+ call_my_business_logic
122
+
123
+ success, levels = limiter.batch_charge(
124
+ [bucket, 1, {allow_charge_adjustment: true}]
125
+ )
126
+ ```
127
+
128
+ If you use the `allow_charge_adjustment` option, the charge will succeed, even if the bucket has insufficient tokens. In that case, as much as possible will be charged from the bucket. This may be useful, if your application wants to charge "after the fact", e.g. after your business logic has already run.
129
+
116
130
  Advanced: Bucket with Reserved Tokens
117
131
 
118
132
  ```ruby
@@ -11,7 +11,7 @@ local timeouts = {}
11
11
  local exceeded = false
12
12
 
13
13
  for key_index, key in ipairs(KEYS) do
14
- local arg_index = key_index * 4 - 2
14
+ local arg_index = key_index * 5 - 3
15
15
  local rate = tonumber(ARGV[arg_index])
16
16
  local size = tonumber(ARGV[arg_index + 1])
17
17
  local amount = tonumber(ARGV[arg_index + 2])
@@ -31,14 +31,20 @@ for key_index, key in ipairs(KEYS) do
31
31
  local limit = tonumber(ARGV[arg_index + 3]) or 0
32
32
 
33
33
  local new_level = current_level - amount
34
- new_bucket_levels[key_index] = new_level
35
34
 
36
35
  local seconds_to_full = (size - new_level) / rate
37
36
  timeouts[key_index] = seconds_to_full
38
37
 
39
38
  if new_level < limit then
40
- exceeded = true
39
+ local allow_charge_adjustment = tonumber(ARGV[arg_index + 4]) or 0
40
+
41
+ if allow_charge_adjustment > 0 then
42
+ new_level = limit
43
+ else
44
+ exceeded = true
45
+ end
41
46
  end
47
+ new_bucket_levels[key_index] = new_level
42
48
  end
43
49
  end
44
50
 
@@ -75,7 +75,13 @@ class Limiter
75
75
  def props_for_charge(charge)
76
76
  bucket, amount, options = charge
77
77
 
78
- [bucket[:rate], bucket[:size], amount, options ? options[:limit] : nil]
78
+ [
79
+ bucket[:rate],
80
+ bucket[:size],
81
+ amount,
82
+ options ? options[:limit] : nil,
83
+ options && options[:allow_charge_adjustment] ? 1 : 0,
84
+ ]
79
85
  end
80
86
 
81
87
  def eval_script(options)
@@ -1,3 +1,3 @@
1
1
  module RedisTokenBucket
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_token_bucket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Hanekamp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-02 00:00:00.000000000 Z
11
+ date: 2017-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  version: '0'
109
109
  requirements: []
110
110
  rubyforge_project:
111
- rubygems_version: 2.2.2
111
+ rubygems_version: 2.5.2
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Token Bucket Rate Limiting using Redis