Resource pools, backoff retries and flow control for MoonBit.
let pool : @moonpool.Pool[Conn] = @moonpool.Pool::new(
limits=@moonpool.Limits::new(size=20),
)
let now = 0L
match pool.take(now, roll=0.42) {
Ready(conn) => ignore(pool.give(conn, now)) // lend, then take it back
Stale(conn) => ignore(conn) // too old: close it, ask again
Make => pool.made(1, now) // open one; unmade(now) if that fails
Wait => () // all in use: queue the caller
Later(wait) => ignore(wait) // making is held back this long
Shed => () // fail now: the backend or the host said no
}| Means | What the caller does | |
|---|---|---|
| Ready(conn) | An idle resource, the most recently returned one | Use it, then give it back |
| Stale(conn) | An idle resource past its age | Close it, then take again |
| Make | There is room, and it is reserved | Open one, then made — or unmade if that fails |
| Wait | Every live slot is in use | Queue the caller; the pool has no queue of its own |
| Later(span) | Making is held back | Try again after that span |
| Shed | Refuse | Fail fast |
| Preset | What it decides | |
|---|---|---|
| size | 10 | How many may be live at once: idle, lent, and being made |
| idle | 10 | How many may sit idle; give answers false when that many are already there |
| keep | 30 min | How long an idle resource may sit before it is retired |
| life | forever | How long a resource may live at all, however busy it has been |
moon add moonbitstack/moonpoolmoon run src/examples/sim| Rigid | the computed delay, unchanged. Reproducible, and it synchronises clients |
| Full | uniform over [0, cap]. The one that spreads clients furthest |
| 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 |
| Spread(f) | uniform over [cap × (1 − f), cap × (1 + f)], f held to [0, 1]. So the wait can pass the cap by f |
type Pool[T]Install
Download zipResource pools, backoff retries and flow control for MoonBit.