Module: Braintrust::Internal::Retry
- Defined in:
- lib/braintrust/internal/retry.rb
Constant Summary collapse
- MAX_RETRIES =
7- BASE_DELAY =
1.0- MAX_DELAY =
8.0
Class Method Summary collapse
-
.with_backoff(max_retries: MAX_RETRIES, base_delay: BASE_DELAY, max_delay: MAX_DELAY, until: nil, &task) ⇒ Object
Retry a block with exponential backoff.
Class Method Details
.with_backoff(max_retries: MAX_RETRIES, base_delay: BASE_DELAY, max_delay: MAX_DELAY, until: nil, &task) ⇒ Object
Retry a block with exponential backoff.
The block is the task to attempt. Its return value is captured each attempt.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/braintrust/internal/retry.rb', line 27 def self.with_backoff(max_retries: MAX_RETRIES, base_delay: BASE_DELAY, max_delay: MAX_DELAY, until: nil, &task) check = binding.local_variable_get(:until) result = task.call retries = 0 while retries < max_retries && !(check ? check.call(result) : result) retries += 1 delay = [base_delay * (2**(retries - 1)), max_delay].min sleep(delay) result = task.call end result end |