mcp: add configurable keepalive failure threshold (#982)
mcp: add configurable keepalive failure threshold Introduce `KeepAliveFailureThreshold` option in both `ClientOptions` and `ServerOptions` to control how many consecutive keepalive ping failures are tolerated before closing the session. This aligns with the MCP spec's guidance that "multiple failed pings MAY trigger a connection reset," allowing operators to tune resilience against transient network hiccups without immediately tearing down otherwise healthy sessions. A threshold of 0 or 1 (the default) closes on the first failure, preserving existing behavior. Higher values let isolated misses pass while still closing the session once consecutive failures reach the threshold. A successful ping resets the counter. Tolerated failures are logged at WARN level; the final failure that closes the session is logged at ERROR level. This is rework of #979.
This commit is contained in:
parent
dfb45f1e11
commit
8805aa85f7
1 changed files with 131 additions and 14 deletions
|
|
@ -166,6 +166,13 @@ type ClientOptions struct {
|
|||
// If the peer fails to respond to pings originating from the keepalive check,
|
||||
// the session is automatically closed.
|
||||
KeepAlive time.Duration
|
||||
// KeepAliveFailureThreshold is the number of consecutive keepalive ping
|
||||
// failures tolerated before the session is closed. A value of 0 or 1
|
||||
// closes the session on the first failure (the default). Higher values
|
||||
// align with the spec's "multiple failed pings MAY trigger a connection
|
||||
// reset" guidance, letting a transient miss pass without tearing down an
|
||||
// otherwise live session. Has no effect unless KeepAlive is non-zero.
|
||||
KeepAliveFailureThreshold int
|
||||
}
|
||||
|
||||
// toolContextKeyType is the context key type for passing tool definitions
|
||||
|
|
@ -441,7 +448,7 @@ func (cs *ClientSession) registerElicitationWaiter(elicitationID string) (await
|
|||
|
||||
// startKeepalive starts the keepalive mechanism for this client session.
|
||||
func (cs *ClientSession) startKeepalive(interval time.Duration) {
|
||||
startKeepalive(cs, interval, &cs.keepaliveCancel, cs.client.opts.Logger)
|
||||
startKeepalive(cs, interval, cs.client.opts.KeepAliveFailureThreshold, &cs.keepaliveCancel, cs.client.opts.Logger)
|
||||
}
|
||||
|
||||
// AddRoots adds the given roots to the client,
|
||||
|
|
|
|||
Loading…
Reference in a new issue