simple_throttle 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/VERSION +1 -1
  4. data/lib/simple_throttle.rb +16 -11
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47859d64b667fe3e43c8a98ecbce2fa305177530a5d625ed0bd38a500875d268
4
- data.tar.gz: 3c0df333efeec1abc8487328a2d131a9ffc2c87273fb5f0ec4048bc340ba1d55
3
+ metadata.gz: 6777d3382abb6c7e07c503e56676a8fe62be43611b2c623ac18f89928708572e
4
+ data.tar.gz: da8f2432ec4bc9dcf68e42faaf6213d662379c3ded9dfb16c1eb8c2cd27cb62c
5
5
  SHA512:
6
- metadata.gz: 88fb81aaa2ac35d7ae5932ccded080c05fe3dd718bcfa9d73b93e8cfd3eb1706b4b5da42b02a06bcd64c861de6f3c98df9bba6005d1b104881b0d5a91f111d06
7
- data.tar.gz: b8a94b2c2a24b7c88266a70f052a93a0a6a0fac5b48842e2fd89e56200ebf72c0229defca71ee775cc4a72810a5d4c9c8ee4feac37ab8281d8e67a0a924bf311
6
+ metadata.gz: f90008dc9debdfb8e206d25f112c0e312c4c27060256464eba6b175aefb9b5be063bbcdfb1188bbcaab05caedb6f1197d28d7b4a5ee3b40819692e4d57e9642a
7
+ data.tar.gz: 31326e2b2ab31f4fbccf8dbd6fb7d0455acdd1e745da7d90ea77f526f320fef4d77c94cc98dffc03ce9d76f4c3fc7ed167aa5b9bd72761dfc0463296b7424459
data/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## 1.1.1
8
+
9
+ ### Fixed
10
+
11
+ - Fixed `increment!` method to return the correct value after removing expired requests rather than the raw count from Redis.
12
+
7
13
  ## 1.1.0
8
14
 
9
15
  ### Added
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
@@ -17,9 +17,10 @@ class SimpleThrottle
17
17
  local now = ARGV[3]
18
18
  local pause_to_recover = tonumber(ARGV[4])
19
19
  local amount = tonumber(ARGV[5])
20
+ local cleanup = tonumber(ARGV[6])
20
21
 
21
22
  local size = redis.call('llen', list_key)
22
- if size >= limit then
23
+ if size >= limit or (cleanup > 0 and size > 0) then
23
24
  local expired = tonumber(now) - ttl
24
25
  while size > 0 do
25
26
  local t = redis.call('lpop', list_key)
@@ -152,7 +153,7 @@ class SimpleThrottle
152
153
  #
153
154
  # @return [Boolean]
154
155
  def allowed!
155
- size = increment!
156
+ size = add_request(1, false)
156
157
  size <= limit
157
158
  end
158
159
 
@@ -163,15 +164,7 @@ class SimpleThrottle
163
164
  # @param amount [Integer] amount to increment the throttle by
164
165
  # @return [Integer]
165
166
  def increment!(amount = 1)
166
- pause_to_recover_arg = (@pause_to_recover ? 1 : 0)
167
- time_ms = (Time.now.to_f * 1000).round
168
- ttl_ms = (ttl * 1000).ceil
169
- self.class.send(
170
- :execute_lua_script,
171
- redis: redis_client,
172
- keys: [redis_key],
173
- args: [limit, ttl_ms, time_ms, pause_to_recover_arg, amount]
174
- )
167
+ add_request(amount, true)
175
168
  end
176
169
 
177
170
  # Reset a throttle back to zero.
@@ -219,4 +212,16 @@ class SimpleThrottle
219
212
  def redis_key
220
213
  "simple_throttle.#{name}"
221
214
  end
215
+
216
+ def add_request(amount, cleanup)
217
+ pause_to_recover_arg = (@pause_to_recover ? 1 : 0)
218
+ time_ms = (Time.now.to_f * 1000).round
219
+ ttl_ms = (ttl * 1000).ceil
220
+ self.class.send(
221
+ :execute_lua_script,
222
+ redis: redis_client,
223
+ keys: [redis_key],
224
+ args: [limit, ttl_ms, time_ms, pause_to_recover_arg, amount, (cleanup ? 1 : 0)]
225
+ )
226
+ end
222
227
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_throttle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Durand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-30 00:00:00.000000000 Z
11
+ date: 2024-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis