simple_throttle 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +1 -1
- data/VERSION +1 -1
- data/lib/simple_throttle.rb +31 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd25dd96b13df27a165952bee5a5ff4fd6731c164c88aaa586e2c990a47650d3
|
4
|
+
data.tar.gz: 4298862682b63ef05bebf1e70abb62bf1dad5e7c3a23835e4c9ad94ed38c8cd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52eda43dd29e5c4ceadb806c0a132abeb30737365aea897a33815e7aa40ca518ea97ed4506c3b612e4729f08e9e3534877dd9f3d6890925cdb8bf7afdc4a5e70
|
7
|
+
data.tar.gz: 839f5e758fe348fa974dd3b8e3b2d37827002ab39db178058095344ed90c94b25aeb6a477d93ac042a9d291bf3afd8bbf13672b60a91233fa09cb13861a94bf1
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,9 @@ 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
|
+
|
7
10
|
## 1.0.3
|
8
11
|
|
9
12
|
### Changed
|
data/README.md
CHANGED
@@ -60,7 +60,7 @@ $ gem install simple_throttle
|
|
60
60
|
|
61
61
|
## Contributing
|
62
62
|
|
63
|
-
|
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.
|
1
|
+
1.0.4
|
data/lib/simple_throttle.rb
CHANGED
@@ -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
|
-
|
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,11 +111,12 @@ 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
|
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?
|
@@ -108,17 +127,23 @@ class SimpleThrottle
|
|
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
|
|
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.
|
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:
|
12
|
+
date: 2023-02-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|