Class: Ihasa::Bucket
- Inherits:
-
Object
- Object
- Ihasa::Bucket
- Defined in:
- lib/ihasa/bucket.rb
Overview
Bucket class. That bucket fills up to burst, by rate per second. Each accept? or accept?! call decrement it from 1.
Defined Under Namespace
Classes: EmptyBucket, Implementation, LegacyImplementation, RedisNamespaceSetupError
Constant Summary collapse
- REDIS_VERSION_WITH_REPLICATE_COMMANDS_SUPPORT =
3.2
- SETUP_ADVICE =
'Ensure that the method '\ 'Ihasa::Bucket#save was called.'.freeze
- SETUP_ERROR =
('Redis raised an error: %{msg}. ' + SETUP_ADVICE).freeze
Instance Attribute Summary collapse
-
#burst ⇒ Object
readonly
Returns the value of attribute burst.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Class Method Summary collapse
Instance Method Summary collapse
- #accept! ⇒ Object
- #accept? ⇒ Boolean
- #delete ⇒ Object
-
#initialize(rate, burst, prefix, redis) ⇒ Bucket
constructor
A new instance of Bucket.
- #save ⇒ Object
Constructor Details
#initialize(rate, burst, prefix, redis) ⇒ Bucket
Returns a new instance of Bucket.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ihasa/bucket.rb', line 25 def initialize(rate, burst, prefix, redis) @implementation = if self.class.legacy_mode?(redis) require 'ihasa/bucket/legacy_implementation' LegacyImplementation.instance else require 'ihasa/bucket/implementation' Implementation.instance end @prefix = prefix @keys = Ihasa::OPTIONS.map { |opt| "#{prefix}:#{opt.upcase}" } @redis = redis @rate = Float rate @burst = Float burst end |
Instance Attribute Details
#burst ⇒ Object (readonly)
Returns the value of attribute burst.
24 25 26 |
# File 'lib/ihasa/bucket.rb', line 24 def burst @burst end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
24 25 26 |
# File 'lib/ihasa/bucket.rb', line 24 def keys @keys end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
24 25 26 |
# File 'lib/ihasa/bucket.rb', line 24 def prefix @prefix end |
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
24 25 26 |
# File 'lib/ihasa/bucket.rb', line 24 def rate @rate end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
24 25 26 |
# File 'lib/ihasa/bucket.rb', line 24 def redis @redis end |
Class Method Details
.create(*args) ⇒ Object
7 8 9 |
# File 'lib/ihasa/bucket.rb', line 7 def create(*args) new(*args).tap(&:save) end |
.legacy_mode?(redis) ⇒ Boolean
13 14 15 |
# File 'lib/ihasa/bucket.rb', line 13 def legacy_mode?(redis) redis_version(redis) < REDIS_VERSION_WITH_REPLICATE_COMMANDS_SUPPORT end |
Instance Method Details
#accept! ⇒ Object
56 57 58 59 60 |
# File 'lib/ihasa/bucket.rb', line 56 def accept! result = (block_given? ? accept?(&Proc.new) : accept?) raise EmptyBucket, "Bucket #{prefix} throttle limit" unless result result end |
#accept? ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/ihasa/bucket.rb', line 46 def accept? result = @implementation.accept?(self) == OK return yield if result && block_given? result rescue Redis::CommandError => e raise RedisNamespaceSetupError, SETUP_ERROR % { msg: e. } end |
#delete ⇒ Object
66 67 68 |
# File 'lib/ihasa/bucket.rb', line 66 def delete redis.del(keys) end |
#save ⇒ Object
62 63 64 |
# File 'lib/ihasa/bucket.rb', line 62 def save @implementation.save(self) end |