Skip to content

function

Helpers around functions: composition, memoization, retrying and rate limiting.

Nothing here sleeps or reads a clock. retry and backoff leave the waiting to the caller, and TokenBucket takes the current time as an argument, so all of it is deterministic to test.

Cargo feature function (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features function

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["function"] }

Import path: helpers4::function.

ItemWhat it does
backoffThe delay before retry number attempt, doubling each time up to max: exponential backoff.
composeCombines two functions into one that applies inner first and outer to its result: compose(outer, inner)(x) is outer(inner(x)), like the mathematical outer ∘ inner.
MemoizeA function whose results are remembered: calling it again with the same argument returns the stored result instead of computing it again.
pipeCombines two functions into one that applies first and then second to its result: pipe(first, second)(x) is second(first(x)).
retryRuns operation until it succeeds, at most attempts times, and returns the first success or the last error.
TokenBucketA token-bucket rate limiter with the clock passed in.