
Stop calling an asynchronous function that keeps failing
Source:R/async_circuit_breaker.R
async_circuit_breaker.RdAsynchronous version of circuit_breaker(), for functions that return a
promise. It has the same states and arguments. A call
fails when its promise rejects, or the function throws. Once the circuit is
open, calls return a promise already rejected with fnware_circuit_open,
without running the function.
Details
Calls overlap, so a failure is counted when its promise rejects, and calls made before earlier ones settle all go through. Two more rules:
While half-open, only one trial call runs and others are rejected. A trial not settled after
reset_afterseconds is abandoned, so a promise that never settles cannot hold the circuit shut. Putasync_timeout()inside to bound it sooner.A call that started before the circuit opened cannot close it, or be counted again, when it settles.
Stack it outside async_retry(). Needs the promises package and an event
loop.
Examples
if (FALSE) { # \dontrun{
down <- function() promises::promise_reject(simpleError("service down"))
f := async_circuit_breaker(failures = 2, reset_after = 60) %@% down
# Failures are counted when a promise settles, so let each call settle
try_once <- function() {
promises::then(f(), onRejected = function(err) cat(class(err)[[1]], "\n"))
while (!later::loop_empty()) later::run_now(0.1)
}
try_once() # simpleError
try_once() # simpleError, and the circuit opens
try_once() # fnware_circuit_open, down() was not called
fetch := async_circuit_breaker(failures = 3, reset_after = 30) %@%
async_retry(stop = stop_after_attempt(3)) %@%
function(url) down()
} # }