Class: R4r::TokenRetryBudget

Inherits:
RetryBudget show all
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

Methods inherited from RetryBudget

create, empty, infinite

Constructor Details

#initialize(bucket:, deposit_amount:, withdrawal_amount:) ⇒ TokenRetryBudget

Creates a new R4r::TokenRetryBudget.

Parameters:

Raises:

  • (ArgumentError)


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

#balanceObject



142
143
144
# File 'lib/r4r/retry_budget.rb', line 142

def balance
  @bucket.count / @withdrawal_amount
end

#depositObject



132
133
134
# File 'lib/r4r/retry_budget.rb', line 132

def deposit
  @bucket.put(@deposit_amount)
end

#to_sObject



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_withdrawObject



137
138
139
# File 'lib/r4r/retry_budget.rb', line 137

def try_withdraw
  @bucket.try_get(@withdrawal_amount)
end