Resource pools for MoonBit: which resource to hand out, when to make another, which to let go, guarded by the breakers, limiters and backoff of moonward. The clock and the randomness are passed in, so every sequence is reproducible and none of it touches I/O.
let pool : @moonpool.Pool[Int32] = @moonpool.Pool::new(
limits=@moonpool.Limits::new(size=20),
ward=@moonpool.Ward::new(
breaker=@moonward.Breaker::new(),
backoff=@moonward.backoff,
),
)
let now = 0L
match pool.take(now, roll=0.42) {
Ready(conn) => ignore(pool.give(conn, now)) // use it, then give it back
Stale(conn) => ignore(conn) // too old to use: close it and take again
Make => pool.made(1, now) // open one, or call pool.unmade(now) if that fails
Wait => () // all in use: queue the caller
Later(wait) => ignore(wait) // making is held back this long
Shed => () // fail fast: the backend is refusing, or this process is overloaded
}| What it answers | |
|---|---|
| Pool[T] | Which resource to hand out, whether to make another, which have sat or lived long enough to let go |
| Ward | What stands in front of the backend; every part optional, the preset ward has none |
| pick | Which of several pools to one service to take from: two drawn at random, the one whose breaker refuses less, then the one with fewer in use |
| Backoff, Bucket, Gate, Sliding | Re-exported from moonward, where they now live |
| Part | When | Answer |
|---|---|---|
| shedder | before lending at all | Shed while the process is hot and has more in flight than it has shown it can finish |
| backoff | before making, after a failed make | Later(span) until the delay has passed; the delay grows with each failure in a row |
| bucket | before making | Later(span) until the bucket has a token, so reconnections do not arrive at once |
| breaker | before making | Shed with the probability the breaker gives a failing backend |
moon add moonbitstack/moonpool| Rigid | the computed delay, unchanged. Reproducible, and it synchronises clients |
| Full | uniform over [0, cap]. The one that spreads clients furthest, and what gRPC sleeps |
| Equal | half the cap plus uniform over [0, cap/2]. Keeps a floor under the wait |
| Decorrelated | uniform over [base, previous × 3]. Grows from where it last landed rather than from the attempt number |
type Pool[T]Install
Download zipResource pools for MoonBit: which resource to hand out, when to make another, which to let go, guarded by the breakers, limiters and backoff of moonward. The clock and the randomness are passed in, so every sequence is reproducible and none of it touches I/O.