Class: DoubleRestraint
- Inherits:
-
Object
- Object
- DoubleRestraint
- Defined in:
- lib/double_restraint.rb
Instance Attribute Summary collapse
-
#long_running_timeout ⇒ Object
readonly
Returns the value of attribute long_running_timeout.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#default_pool_limit ⇒ Integer
Get the limit for the default pool.
-
#default_pool_size ⇒ Integer
Get the current size of the default pool.
-
#execute {|the| ... } ⇒ Object
Execute a block of code.
-
#initialize(name, timeout:, long_running_timeout:, long_running_limit:, limit: nil, timeout_errors: [Timeout::Error], redis: nil) ⇒ DoubleRestraint
constructor
A new instance of DoubleRestraint.
-
#long_running?(timeout) ⇒ Boolean
Helper method to determine if a timeout represents the long running timeout.
-
#long_running_pool_limit ⇒ Integer
Get the limit for the long running pool.
-
#long_running_pool_size ⇒ Integer
Get the current size of the long running pool.
Constructor Details
#initialize(name, timeout:, long_running_timeout:, long_running_limit:, limit: nil, timeout_errors: [Timeout::Error], redis: nil) ⇒ DoubleRestraint
Returns a new instance of DoubleRestraint.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/double_restraint.rb', line 22 def initialize(name, timeout:, long_running_timeout:, long_running_limit:, limit: nil, timeout_errors: [Timeout::Error], redis: nil) @timeout = timeout @long_running_timeout = long_running_timeout @timeout_errors = Array(timeout_errors) limit = -1 if limit.nil? # Slots in the underlying restrainers expire as a safeguard against orphaned locks, # so the expiration must comfortably exceed the longest time a block can legitimately # hold a slot. restrainer_timeout = [60, long_running_timeout.to_f * 2].max # The restrainer names use "DoubleRestrainer" rather than "DoubleRestraint" for # backward compatibility with the Redis keys created by earlier versions of the gem. @restrainer = Restrainer.new("DoubleRestrainer(#{name})", limit: limit, timeout: restrainer_timeout, redis: redis) @long_running_restrainer = Restrainer.new("DoubleRestrainer(#{name}).long_running", limit: long_running_limit, timeout: restrainer_timeout, redis: redis) end |
Instance Attribute Details
#long_running_timeout ⇒ Object (readonly)
Returns the value of attribute long_running_timeout.
7 8 9 |
# File 'lib/double_restraint.rb', line 7 def long_running_timeout @long_running_timeout end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/double_restraint.rb', line 7 def timeout @timeout end |
Instance Method Details
#default_pool_limit ⇒ Integer
Get the limit for the default pool. This will return -1 if there is no limit set on that pool.
84 85 86 |
# File 'lib/double_restraint.rb', line 84 def default_pool_limit @restrainer.limit end |
#default_pool_size ⇒ Integer
Get the current size of the default pool. This can be useful in collecting realtime stats about how the pool is being utilized.
68 69 70 |
# File 'lib/double_restraint.rb', line 68 def default_pool_size @restrainer.current end |
#execute {|the| ... } ⇒ Object
Execute a block of code. The block will be yielded with the timeout value. If the block raises a timeout error, then it will be called again with the long running timeout. The code in the block must be idempotent since it can be run twice.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/double_restraint.rb', line 44 def execute timed_out = false result = @restrainer.throttle do yield @timeout rescue *@timeout_errors # Just flag the timeout here so the retry happens after the throttle # block exits and releases its slot in the default pool. timed_out = true nil end if timed_out result = @long_running_restrainer.throttle do yield @long_running_timeout end end result end |
#long_running?(timeout) ⇒ Boolean
Helper method to determine if a timeout represents the long running timeout. Note that the timeout and long running timeouts need to be different values in order for this to work.
100 101 102 |
# File 'lib/double_restraint.rb', line 100 def long_running?(timeout) timeout.to_f.round(6) == @long_running_timeout.to_f.round(6) end |
#long_running_pool_limit ⇒ Integer
Get the limit for the long running pool.
91 92 93 |
# File 'lib/double_restraint.rb', line 91 def long_running_pool_limit @long_running_restrainer.limit end |
#long_running_pool_size ⇒ Integer
Get the current size of the long running pool. This can be useful in collecting realtime stats about how the pool is being utilized.
76 77 78 |
# File 'lib/double_restraint.rb', line 76 def long_running_pool_size @long_running_restrainer.current end |