Exact, Redis-backed bounded consumption for shared quotas.
EscrowMint Go is for cases where many threads, processes, or services need to consume from the same global quota without letting it go below zero.
Good fits:
- prepaid credits
- inventory reservation
- budget caps
- worker permit pools
- campaign spend controls
EscrowMint is not a generic counter library. It is a quota and reservation library with application-level semantics:
- consistent pool top-up
- exact bounded decrement
- idempotent consume
- reservation with TTL
- commit and cancel flow
- crash recovery via lazy expiry reclaim
go get github.com/biprakanta/escrowmint-go/escrowmintpackage main
import (
"context"
"log"
"github.com/biprakanta/escrowmint-go/escrowmint"
)
func main() {
ctx := context.Background()
client, err := escrowmint.NewClient(ctx, escrowmint.Config{
URL: "redis://localhost:6379/0",
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
result, err := client.TryConsume(ctx, "wallet:123", 5, escrowmint.ConsumeOptions{
IdempotencyKey: "req-001",
})
if err != nil {
log.Fatal(err)
}
log.Println(result.Applied, result.Remaining)
}result, err := client.TopUp(ctx, "wallet:123", 25, escrowmint.TopUpOptions{
IdempotencyKey: "credit-001",
})
if err != nil {
log.Fatal(err)
}
log.Println(result.Added, result.Available)reservation, err := client.Reserve(ctx, "wallet:123", 10, 30000, escrowmint.ReserveOptions{})
if err != nil {
log.Fatal(err)
}
result, err := client.Commit(ctx, "wallet:123", reservation.ReservationID)
if err != nil {
log.Fatal(err)
}
_ = resultIf a worker crashes after Reserve but before Commit, the held quota is released after TTL expiry on the next mutation or GetState call for that same resource.
client.TryConsume(ctx, resource, amount, opts)
client.TopUp(ctx, resource, amount, opts)
client.Reserve(ctx, resource, amount, ttlMS, opts)
client.Commit(ctx, resource, reservationID)
client.Cancel(ctx, resource, reservationID)
client.GetState(ctx, resource)EscrowMint Go also ships an explicit chunk-lease lifecycle for hot resources:
lease, err := client.AllocateChunk(ctx, "wallet:123", 100, escrowmint.AllocateChunkOptions{
OwnerID: "worker-a",
TTLMS: 30000,
})
if err != nil {
log.Fatal(err)
}
result, err := client.ConsumeChunk(ctx, "wallet:123", lease.LeaseID, 5, "worker-a")
if err != nil {
log.Fatal(err)
}
_, _ = result, leaseThis is the authoritative distributed chunk path. It keeps chunk state in Redis and supports expiry reclaim, renew, release, and worker ownership checks.
- Redis remains the source of truth for each resource.
- Lua scripts make each operation atomic.
- Reservations move units from
availabletoreserved. - Top-ups add units back into
availablewithout bypassing expiry reclaim. - Pending reservations are indexed by expiry time in Redis.
- Expired reservations are reclaimed lazily in bounded batches on the next touch of that resource.
- Terminal reservation outcomes are moved into short-lived receipt keys so the hot reservation hash stays small.
EscrowMint currently ships both models.
The direct path is the shared-resource path:
TryConsume,Reserve,Commit, andCancelTopUpfor consistent replenishment of the shared pool- exact bounded updates against the resource's shared state
- the simplest way to get correctness and crash recovery
The chunk lease path adds a worker-owned lease layer on top of that model:
AllocateChunk,ConsumeChunk,RenewChunk,ReleaseChunk, andGetChunk- explicit escrow or chunk allocation per worker
- better control over hot-resource ownership, refill, expiry, and reclaim
- more operational complexity than the direct path
Choose the direct path when you want the simplest exact path.
Choose the chunk lease path when a resource benefits from explicit worker-level quota management.
The current chunk lease implementation is an authoritative Redis-backed lease lifecycle. It improves the state model for hot resources, but it does not automatically become a no-Redis local fast path. If you want fewer Redis round trips than the shipped chunk API provides, you can layer an in-process chunk consumer on top of the authoritative lease lifecycle.
See docs/CHUNK_LEASES.md.
go test ./...
go test ./... -cover
go mod tidy
gofmt -w ./...Notes:
- module path is go.mod
- tests use Docker-backed Redis integration cases
- Go module floor:
1.22+ - Tested in CI:
1.25and1.26 - Redis: intended for modern Redis deployments that support Lua scripting and standard key expiry semantics
- Stability: the module is published and follows SemVer before
1.0; breaking API changes remain possible, but release notes will call them out explicitly
EscrowMint Go uses Conventional Commits and Release Please for semantic versioning and release notes.
fix:-> patch releasefeat:-> minor releasefeat!:orBREAKING CHANGE:-> major release
When releasable commits land on main, Release Please opens or updates a release PR. Merging that PR updates CHANGELOG.md, creates the vX.Y.Z tag, and creates the GitHub release notes. The existing tag workflow then verifies the tagged release commit.
