Grav v1.14.0

Retry

import "std/concurrency" (included in import "std") · namespace std.concurrency

Trying something again after it fails, waiting longer each time (exponential backoff), with random jitter so many clients failing at once do not all come back at once, and a deadline for the whole.

grav
import "std"

fn main() {
    let waits = new Array<long>()
    let r = new Retry(4, 10, 1000)             // 4 tries; waits from 10 ms, doubling, at most 1 s
        .noJitter()                             // (exact waits, for this example)
        .when(fn(e: Exception) -> bool { return e is IOError })
        .notify(fn(attempt: int, e: Exception, wait: long) { waits.push(wait) })
    let body = r.call::<string>(fn(attempt: int) -> string {
        if (attempt < 3) { throw new IOError("connection reset") }
        return "fetched on try ${attempt}"
    })
    print(body)
    print(waits)
    try {
        r.run(fn(attempt: int) { throw new ValueError("bad request") })   // not an IOError: no second try
    } catch (e: ValueError) {
        print(e.message())
    }
}
Output
fetched on try 3
[10, 20]
bad request
  • call::<T>(work) runs work(attempt) until it returns, and throws the last failure when no attempt is left, when when(...) says not to try again, or when the next wait would end past deadline(ms). run(work) is the same for work that returns nothing.
  • Waits. After failure n the delay is baseMs doubled n − 1 times, at most maxMs. With jitter (the default) the wait is random between 0 and that delay ("full jitter"), which spreads clients out best. delayFor(n) gives the wait alone, for code that schedules the next try instead of sleeping (a queue).
  • notify(fn(attempt, error, wait)) is told before each wait (to log it).

API

class Retry

Constructors

  • new Retry(attempts: int = 3, baseMs: long = 100, maxMs: long = 10000)

Fields

  • attempts: int — Tries in all (the first included).
  • baseMs: long — The first wait, and the longest one (milliseconds).
  • maxMs: long
  • deadlineMs: long = 0 — Total time allowed (0: none): no wait is begun that would end past it.
  • jitter: bool = true
  • retryIf: fn(Exception) -> bool = null — Which failures to try again after (null: every Exception).
  • onRetry: fn(int, Exception, long) = null — Told before each wait: the attempt that failed, its error, the wait.

Methods

  • fn deadline(ms: long) -> Retry
  • fn when(f: fn(Exception) -> bool) -> Retry
  • fn notify(f: fn(int, Exception, long)) -> Retry
  • fn noJitter() -> Retry — Waits of exactly the doubled delay (tests, or a server's own schedule).
  • fn delayFor(failures: int) -> long — The wait after failures failed attempts (1 for the first): the base doubled per failure, at most maxMs; with jitter, random up to that.
  • fn call<T>(work: fn(int) -> T) -> T — Runs work (given the attempt number, from 1) until it returns; the last failure is thrown when no attempt or time is left, or when retryIf says not to try again.
  • fn run(work: fn(int)) — call for work that returns nothing.

Edit this page on GitHub