Class: R4r::TokenRetryBudget
- Inherits:
-
RetryBudget
- Object
- RetryBudget
- R4r::TokenRetryBudget
- Defined in:
- lib/r4r/retry_budget.rb
Constant Summary collapse
- SCALE_FACTOR =
This scaling factor allows for ‘percent_can_retry` > 1 without having to use floating points (as the underlying mechanism here is a R4r::TokenBucket which is not floating point based).
1000.0
- DEFAULT_TTL_MS =
60 * 1000
Instance Method Summary collapse
- #balance ⇒ Object
- #deposit ⇒ Object
-
#initialize(bucket:, deposit_amount:, withdrawal_amount:) ⇒ TokenRetryBudget
constructor
Creates a new TokenRetryBudget.
- #to_s ⇒ Object
- #try_withdraw ⇒ Object
Methods inherited from RetryBudget
Constructor Details
#initialize(bucket:, deposit_amount:, withdrawal_amount:) ⇒ TokenRetryBudget
Creates a new R4r::TokenRetryBudget.
121 122 123 124 125 126 127 128 129 |
# File 'lib/r4r/retry_budget.rb', line 121 def initialize(bucket:, deposit_amount:, withdrawal_amount:) raise ArgumentError, "bucket cannot be nil" if bucket.nil? raise ArgumentError, "deposit_amount cannot be nil" if deposit_amount.nil? raise ArgumentError, "withdrawal_amount cannot be nil" if withdrawal_amount.nil? @bucket = bucket @deposit_amount = deposit_amount.to_i @withdrawal_amount = withdrawal_amount.to_i end |
Instance Method Details
#balance ⇒ Object
142 143 144 |
# File 'lib/r4r/retry_budget.rb', line 142 def balance @bucket.count / @withdrawal_amount end |
#deposit ⇒ Object
132 133 134 |
# File 'lib/r4r/retry_budget.rb', line 132 def deposit @bucket.put(@deposit_amount) end |
#to_s ⇒ Object
146 147 148 |
# File 'lib/r4r/retry_budget.rb', line 146 def to_s "R4r::TokenRetryBudget{deposit=#{@deposit_amount}, withdrawal=#{@withdrawal_amount}, balance=#{balance}}" end |
#try_withdraw ⇒ Object
137 138 139 |
# File 'lib/r4r/retry_budget.rb', line 137 def try_withdraw @bucket.try_get(@withdrawal_amount) end |