Class: RedisThrottle
- Inherits:
-
Object
- Object
- RedisThrottle
- Defined in:
- lib/redis_throttle.rb,
lib/redis_throttle/api.rb,
lib/redis_throttle/version.rb,
lib/redis_throttle/rate_limit.rb,
lib/redis_throttle/concurrency.rb
Defined Under Namespace
Classes: Api, Concurrency, RateLimit
Constant Summary collapse
- VERSION =
Gem version.
"2.0.1"
Class Method Summary collapse
-
.concurrency ⇒ Throttle
Syntax sugar for ‘RedisThrottle.new.concurrency(…)`.
-
.info(redis, match: "*") ⇒ Hash{Concurrency => Integer, RateLimit => Integer}
Return usage info for all known (in use) strategies.
-
.rate_limit ⇒ Throttle
Syntax sugar for ‘RedisThrottle.new.rate_limit(…)`.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns ‘true` if the `other` is an instance of RedisThrottle with the same set of strategies.
-
#acquire(redis, token: SecureRandom.uuid) ⇒ #to_s?
Acquire execution lock.
-
#call(redis, token: SecureRandom.uuid) ⇒ Object?
Calls given block execution lock was acquired, and ensures to #release it after the block.
-
#concurrency(bucket, limit:, ttl:) ⇒ Throttle
Add concurrency strategy to the throttle.
-
#freeze ⇒ Throttle
Prevents further modifications to the throttle instance.
-
#info(redis) ⇒ Hash{Concurrency => Integer, RateLimit => Integer}
Return usage info for all strategies of the throttle.
-
#initialize ⇒ RedisThrottle
constructor
A new instance of RedisThrottle.
-
#initialize_clone(original) ⇒ void
private
Clone internal strategies plan.
-
#initialize_dup(original) ⇒ void
private
Dup internal strategies plan.
-
#merge(other) ⇒ Throttle
(also: #+)
Non-destructive version of #merge!.
-
#merge!(other) ⇒ Throttle
Merge in strategies of the ‘other` throttle.
-
#rate_limit(bucket, limit:, period:) ⇒ Throttle
Add *rate limit* strategy to the throttle.
-
#release(redis, token:) ⇒ void
Release acquired execution lock.
-
#reset(redis) ⇒ void
Flush all counters.
Constructor Details
#initialize ⇒ RedisThrottle
Returns a new instance of RedisThrottle.
46 47 48 |
# File 'lib/redis_throttle.rb', line 46 def initialize @strategies = Concurrent::Set.new end |
Class Method Details
.concurrency ⇒ Throttle
Syntax sugar for ‘RedisThrottle.new.concurrency(…)`.
18 19 20 |
# File 'lib/redis_throttle.rb', line 18 def concurrency(...) new.concurrency(...) end |
.info(redis, match: "*") ⇒ Hash{Concurrency => Integer, RateLimit => Integer}
Return usage info for all known (in use) strategies.
41 42 43 |
# File 'lib/redis_throttle.rb', line 41 def info(redis, match: "*") Api.new(redis).then { |api| api.info(strategies: api.strategies(match: match.to_s)) } end |
.rate_limit ⇒ Throttle
Syntax sugar for ‘RedisThrottle.new.rate_limit(…)`.
27 28 29 |
# File 'lib/redis_throttle.rb', line 27 def rate_limit(...) new.rate_limit(...) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns ‘true` if the `other` is an instance of RedisThrottle with the same set of strategies.
196 197 198 |
# File 'lib/redis_throttle.rb', line 196 def ==(other) other.is_a?(self.class) && @strategies == other.strategies end |
#acquire(redis, token: SecureRandom.uuid) ⇒ #to_s?
Acquire execution lock.
246 247 248 |
# File 'lib/redis_throttle.rb', line 246 def acquire(redis, token: SecureRandom.uuid) token if Api.new(redis).acquire(strategies: @strategies, token: token.to_s) end |
#call(redis, token: SecureRandom.uuid) ⇒ Object?
Calls given block execution lock was acquired, and ensures to #release it after the block.
219 220 221 222 223 224 225 226 227 |
# File 'lib/redis_throttle.rb', line 219 def call(redis, token: SecureRandom.uuid) return unless acquire(redis, token: token) begin yield ensure release(redis, token: token) end end |
#concurrency(bucket, limit:, ttl:) ⇒ Throttle
Add concurrency strategy to the throttle. Use it to guarantee ‘limit` amount of concurrently running code blocks.
92 93 94 95 96 97 98 |
# File 'lib/redis_throttle.rb', line 92 def concurrency(bucket, limit:, ttl:) raise FrozenError, "can't modify frozen #{self.class}" if frozen? @strategies << Concurrency.new(bucket, limit: limit, ttl: ttl) self end |
#freeze ⇒ Throttle
Prevents further modifications to the throttle instance.
175 176 177 178 179 |
# File 'lib/redis_throttle.rb', line 175 def freeze @strategies.freeze super end |
#info(redis) ⇒ Hash{Concurrency => Integer, RateLimit => Integer}
Return usage info for all strategies of the throttle.
307 308 309 |
# File 'lib/redis_throttle.rb', line 307 def info(redis) Api.new(redis).info(strategies: @strategies) end |
#initialize_clone(original) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Clone internal strategies plan.
66 67 68 69 70 |
# File 'lib/redis_throttle.rb', line 66 def initialize_clone(original) super @strategies = original.strategies.clone end |
#initialize_dup(original) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Dup internal strategies plan.
55 56 57 58 59 |
# File 'lib/redis_throttle.rb', line 55 def initialize_dup(original) super @strategies = original.strategies.dup end |
#merge(other) ⇒ Throttle Also known as: +
Non-destructive version of #merge!. Returns new RedisThrottle instance with union of ‘self` and `other` strategies.
165 166 167 |
# File 'lib/redis_throttle.rb', line 165 def merge(other) dup.merge!(other) end |
#merge!(other) ⇒ Throttle
Merge in strategies of the ‘other` throttle.
143 144 145 146 147 148 149 |
# File 'lib/redis_throttle.rb', line 143 def merge!(other) raise FrozenError, "can't modify frozen #{self.class}" if frozen? @strategies.merge(other.strategies) self end |
#rate_limit(bucket, limit:, period:) ⇒ Throttle
Add *rate limit* strategy to the throttle. Use it to guarantee ‘limit` amount of units in `period` of time.
121 122 123 124 125 126 127 |
# File 'lib/redis_throttle.rb', line 121 def rate_limit(bucket, limit:, period:) raise FrozenError, "can't modify frozen #{self.class}" if frozen? @strategies << RateLimit.new(bucket, limit: limit, period: period) self end |
#release(redis, token:) ⇒ void
This method returns an undefined value.
Release acquired execution lock. Notice that this affects #concurrency locks only.
270 271 272 273 274 |
# File 'lib/redis_throttle.rb', line 270 def release(redis, token:) Api.new(redis).release(strategies: @strategies, token: token.to_s) nil end |