simple_throttle 1.0.2 → 1.0.4

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
  SHA256:
3
- metadata.gz: 25cae7f3e3702407aca9ce8b20dd4a2016cdefffd9220261a0397150b10796de
4
- data.tar.gz: f460f12908747e7d8d5431a7ed13274fa6eec4980a60b95949d78e553b297fb0
3
+ metadata.gz: cd25dd96b13df27a165952bee5a5ff4fd6731c164c88aaa586e2c990a47650d3
4
+ data.tar.gz: 4298862682b63ef05bebf1e70abb62bf1dad5e7c3a23835e4c9ad94ed38c8cd5
5
5
  SHA512:
6
- metadata.gz: 96a6efaf9ed64b918ef077296bc1b04c36ef10f8e0e08948e505bf5203b79c4673a0435dc052c750249fb5dd7de1f9b71cab1790743aee10508bef72546bf3bc
7
- data.tar.gz: 5dd505cf1da9506c505598013ab644367da24b9542687d5cf2e1f8a5cb571ad685388bd1dad8621598035d5810a615ce85baf65efc272aa6f1642fc637cf187b
6
+ metadata.gz: 52eda43dd29e5c4ceadb806c0a132abeb30737365aea897a33815e7aa40ca518ea97ed4506c3b612e4729f08e9e3534877dd9f3d6890925cdb8bf7afdc4a5e70
7
+ data.tar.gz: 839f5e758fe348fa974dd3b8e3b2d37827002ab39db178058095344ed90c94b25aeb6a477d93ac042a9d291bf3afd8bbf13672b60a91233fa09cb13861a94bf1
data/CHANGELOG.md CHANGED
@@ -4,12 +4,20 @@ 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.0.4
8
+ - Fix wait_time method to match the documentation from [bc-swoop](https://github.com/bc-swoop)
9
+
10
+ ## 1.0.3
11
+
12
+ ### Changed
13
+ - Ensure that arguments sent to Redis Lua script are cast to integers.
14
+
7
15
  ## 1.0.2
8
16
 
9
17
  ### Added
10
- - Throttle insances can now specify the Redis instance to override the global setting
11
- - Redis instance now defaults to the default redis instance: `Redis.new`
12
- - Optimize loading LUA script to Redis; now done globally instead of per throttle instance
18
+ - Throttle insances can now specify the Redis instance to override the global setting.
19
+ - Redis instance now defaults to the default redis instance: `Redis.new`.
20
+ - Optimize loading LUA script to Redis; now done globally instead of per throttle instance.
13
21
 
14
22
 
15
23
  ## 1.0.1
data/README.md CHANGED
@@ -60,7 +60,7 @@ $ gem install simple_throttle
60
60
 
61
61
  ## Contributing
62
62
 
63
- Open a pull request on GitHub.
63
+ Fork the repository and open a pull request on GitHub.
64
64
 
65
65
  Please use the [standardrb](https://github.com/testdouble/standard) syntax and lint your code with `standardrb --fix` before submitting.
66
66
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.4
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "redis"
4
+
4
5
  # Create a simple throttle that can be used to limit the number of request for a resouce
5
6
  # per time period. These objects are thread safe.
6
7
  class SimpleThrottle
@@ -41,7 +42,15 @@ class SimpleThrottle
41
42
 
42
43
  class << self
43
44
  # Add a global throttle that can be referenced later with the [] method.
44
- def add(name, limit:, ttl:, redis: nil)
45
+ # This can be used to configure global throttles that you want to setup once
46
+ # and then use in multiple places.
47
+ #
48
+ # @param name [String] unique name for the throttle
49
+ # @param ttl [Numeric] number of seconds that the throttle will remain active
50
+ # @param limit [Integer] number of allowed requests within the throttle ttl
51
+ # @param redis [Redis, Proc] Redis instance to use or a Proc that yields a Redos instance
52
+ # @return [void]
53
+ def add(name, ttl:, limit:, redis: nil)
45
54
  @lock.synchronize do
46
55
  @throttles ||= {}
47
56
  @throttles[name.to_s] = new(name, limit: limit, ttl: ttl, redis: redis)
@@ -49,6 +58,9 @@ class SimpleThrottle
49
58
  end
50
59
 
51
60
  # Returns a globally defined throttle with the specfied name.
61
+ #
62
+ # @param name [String, Symbol] name of the throttle
63
+ # @return [SimpleThrottle]
52
64
  def [](name)
53
65
  if defined?(@throttles) && @throttles
54
66
  @throttles[name.to_s]
@@ -60,11 +72,17 @@ class SimpleThrottle
60
72
  # it will be invoked at runtime to get the instance. Use this method if your Redis instance
61
73
  # isn't constant (for example if you're in a forking environment and re-initialize connections
62
74
  # on fork)
75
+ #
76
+ # @param client [Redis, Proc]
77
+ # @yieldreturn [Redis]
78
+ # @return [void]
63
79
  def set_redis(client = nil, &block)
64
80
  @redis_client = (client || block)
65
81
  end
66
82
 
67
83
  # Return the Redis instance where the throttles are stored.
84
+ #
85
+ # @return [Redis]
68
86
  def redis
69
87
  @redis_client ||= Redis.new
70
88
  if @redis_client.is_a?(Proc)
@@ -93,32 +111,39 @@ class SimpleThrottle
93
111
 
94
112
  attr_reader :name, :limit, :ttl
95
113
 
96
- # Create a new throttle
114
+ # Create a new throttle.
115
+ #
97
116
  # @param name [String] unique name for the throttle
98
117
  # @param ttl [Numeric] number of seconds that the throttle will remain active
99
118
  # @param limit [Integer] number of allowed requests within the throttle ttl
100
- # @param redis [Redis] Redis client to use
119
+ # @param redis [Redis, Proc] Redis instance to use or a Proc that yields a Redos instance
101
120
  def initialize(name, ttl:, limit:, redis: nil)
102
121
  @name = name.to_s
103
122
  @name = name.dup.freeze unless name.frozen?
104
- @limit = limit
105
- @ttl = ttl
123
+ @limit = limit.to_i
124
+ @ttl = ttl.to_f
106
125
  @redis = redis
107
126
  end
108
127
 
109
128
  # Returns true if the limit for the throttle has not been reached yet. This method
110
129
  # will also track the throttled resource as having been invoked on each call.
130
+ #
131
+ # @return [Boolean]
111
132
  def allowed!
112
133
  size = current_size(true)
113
134
  size < limit
114
135
  end
115
136
 
116
137
  # Reset a throttle back to zero.
138
+ #
139
+ # @return [void]
117
140
  def reset!
118
141
  redis_client.del(redis_key)
119
142
  end
120
143
 
121
144
  # Peek at the current number for throttled calls being tracked.
145
+ #
146
+ # @return [Integer]
122
147
  def peek
123
148
  current_size(false)
124
149
  end
@@ -126,6 +151,8 @@ class SimpleThrottle
126
151
  # Returns when the next resource call should be allowed. Note that this doesn't guarantee that
127
152
  # calling allow! will return true if the wait time is zero since other processes or threads can
128
153
  # claim the resource.
154
+ #
155
+ # @return [Float]
129
156
  def wait_time
130
157
  if peek < limit
131
158
  0.0
@@ -133,7 +160,7 @@ class SimpleThrottle
133
160
  first = redis_client.lindex(redis_key, 0).to_f / 1000.0
134
161
  delta = Time.now.to_f - first
135
162
  delta = 0.0 if delta < 0
136
- delta
163
+ ttl - delta
137
164
  end
138
165
  end
139
166
 
@@ -152,7 +179,7 @@ class SimpleThrottle
152
179
  def current_size(push)
153
180
  push_arg = (push ? 1 : 0)
154
181
  time_ms = (Time.now.to_f * 1000).round
155
- ttl_ms = ttl * 1000
182
+ ttl_ms = (ttl * 1000).ceil
156
183
  self.class.send(:execute_lua_script, redis: redis_client, keys: [redis_key], args: [limit, ttl_ms, time_ms, push_arg])
157
184
  end
158
185
 
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.2
4
+ version: 1.0.4
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: 2021-09-03 00:00:00.000000000 Z
12
+ date: 2023-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis