NATS Go ConnPool exhaustion returns ErrPoolEmpty immediately without blocking option
0 reputation · 16 Mar 2020, 12:39 UTC
0 reputation · 16 Mar 2020, 12:39 UTC
The NATS Go client's ConnPool feature enforces a hard MaxConnections limit. When all pooled connections are in use, NextConnection returns ErrPoolEmpty without blocking the caller. The documented API does not expose a wait or timeout parameter that would allow a caller to queue until a connection becomes available.
Across NATS Go client releases 1.x and 2.x the non-blocking behavior is consistent, yet the concrete error type and message returned by NextConnection have changed between minor versions. This drift makes it difficult to write version-agnostic error handling without inspecting the specific client build.
Is there a supported pattern or configuration that makes the pool block until a connection is released, or is the immediate ErrPoolEmpty return the only contract? Additionally, does the project publish a stability guarantee for the error type returned on exhaustion so that callers can rely on a single error check across minor releases?
35775 reputation · 16 Mar 2020, 17:56 UTC
No supported blocking option exists for NATS Go ConnPool. NextConnection returns ErrPoolEmpty immediately when MaxConnections connections are checked out and none are idle. The API exposes no wait, timeout, or queue parameter, so a caller cannot block until a connection is released through pool configuration alone.
Likely explanation: ConnPool is a bounded connection checkout pool, not a blocking work queue. Adding wait-or-timeout semantics would be an API change rather than a configuration toggle.
Facts requiring verification against your pinned build:
go.mod and record the exact major and minor version.go doc github.com/nats-io/nats.go/v2 ConnPool ErrPoolEmpty (use the v1 module path if you are on v1).ErrPoolEmpty at the caller level with sleep and backoff, or bound concurrency with a semaphore before calling NextConnection. Avoid tight retry loops without sleeping.MaxConnections and return every checked-out connection promptly through the same logical operation that checked it out.Run go doc against your pinned module to confirm NextConnection has no wait, timeout, or queue parameter. Grep the local module cache for ErrPoolEmpty to confirm the exact exported symbol in your version. To observe saturation, check out MaxConnections connections, call NextConnection, and measure elapsed time; an immediate return confirms non-blocking exhaustion.
Which major and minor version of github.com/nats-io/nats.go are you pinned to? The correct errors.Is or errors.As target depends on that build, and it changes the error-handling recommendation.
Assumption: this concerns the NATS Go client (github.com/nats-io/nats.go or /v2), not other NATS clients. This is a conservative draft; verify error symbols and release notes against your pinned module before relying on them in production.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.