Retry waits in retryable-async do not block other Async tasks
- Ruby
- Async
I wrote retryable-async, a small Ruby gem that retries a block of code when it fails. You can use it inside an Async task.
What the gem adds
By default, Retryable::Async.run tries the block up to three times. The wait between attempts grows linearly: 1 second, then 2 seconds. If the third attempt also fails, run raises the error again.
You can change this behavior with these options:
max_attemptssets the number of attempts.onselects the exceptions to retry. By default, it covers everyStandardError.base_delayandbackoffset the delay.backoffis:linearor:exponential.jittermultiplies the delay by a random factor between 0.5 and 1.5.before_retryreceives the number of the failed attempt and its exception, before each retry.
Example from the README
The gem does not depend on async, so you require async yourself.
require 'async'
require 'retryable-async'
Async do
Retryable::Async.run do
puts "Doing something asynchronously..."
raise "fail"
end
endThe block in this example always fails, so there are two waits: 1 second before the second attempt and 2 seconds before the third. After that, Retryable::Async.run raises the error again.
The retry wait does not block other tasks
This section applies to async 2.46.0, which requires Ruby 3.3 or later.
Async runs each task on a fiber. Each retry wait calls a plain sleep. Inside an Async task, Ruby passes that sleep to the Async scheduler.
The scheduler only pauses the fiber that calls sleep. Other Async tasks can keep running during the retry wait.
