mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-12-06 11:03:52 +00:00
fix(mcp): stop the progress watchdog cutting idle MCP event streams (#14)
MCP GET event streams (the server-to-client channel) now run without the progress watchdog and are ended by the client, the upstream or Gateway.DrainIdleStreams. The standalone binary drains them on shutdown and when a config reload retires a gateway, so they do not hold either open.
This commit is contained in:
@@ -307,6 +307,11 @@ func run() error {
|
||||
MaxHeaderBytes: defaultMaxHeaderBytes,
|
||||
}
|
||||
|
||||
// Shutdown waits for open handlers, and an idle MCP event stream never
|
||||
// ends by itself; ending those once the listener closes lets their
|
||||
// clients reconnect to another replica.
|
||||
srv.RegisterOnShutdown(sw.drainIdleStreams)
|
||||
|
||||
ready.Store(true)
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- srv.ListenAndServe() }()
|
||||
|
||||
@@ -51,6 +51,9 @@ func (g *generation) retire(maxWait time.Duration) {
|
||||
if !g.markRetired() {
|
||||
return
|
||||
}
|
||||
// Idle MCP event streams have no end of their own; ending them lets their
|
||||
// clients reconnect to the new generation and this one close.
|
||||
drainIdleStreams(g.h)
|
||||
g.timer.Store(time.AfterFunc(maxWait, g.shut))
|
||||
// A shut that ran before the Store could not stop the timer, so stop it here.
|
||||
if g.shutDone.Load() {
|
||||
@@ -113,6 +116,22 @@ func (s *swapHandler) swap(next *generation, drain time.Duration) {
|
||||
s.cur.Swap(next).retire(drain)
|
||||
}
|
||||
|
||||
// drainIdleStreams ends the serving gateway's idle MCP event streams. run
|
||||
// calls it when the server shuts down, so those streams do not hold the
|
||||
// shutdown for its whole timeout.
|
||||
func (s *swapHandler) drainIdleStreams() {
|
||||
if g := s.cur.Load(); g != nil {
|
||||
drainIdleStreams(g.h)
|
||||
}
|
||||
}
|
||||
|
||||
// drainIdleStreams calls DrainIdleStreams when h is a gateway that has it.
|
||||
func drainIdleStreams(h http.Handler) {
|
||||
if d, ok := h.(interface{ DrainIdleStreams() }); ok {
|
||||
d.DrainIdleStreams()
|
||||
}
|
||||
}
|
||||
|
||||
// closeCurrent closes the serving generation and turns away any request that arrives later.
|
||||
// Call it only after the server stopped serving.
|
||||
func (s *swapHandler) closeCurrent() {
|
||||
|
||||
@@ -372,3 +372,31 @@ func TestGatewayBuilderRejectsUnknownField(t *testing.T) {
|
||||
}
|
||||
|
||||
var _ http.Handler = (*traefikllmgateway.Gateway)(nil)
|
||||
|
||||
// drainCounter is a gateway stand-in that counts DrainIdleStreams calls.
|
||||
type drainCounter struct {
|
||||
http.Handler
|
||||
drains atomic.Int32
|
||||
}
|
||||
|
||||
func (d *drainCounter) DrainIdleStreams() { d.drains.Add(1) }
|
||||
|
||||
func TestIdleStreamsDrainedOnRetireAndShutdown(t *testing.T) {
|
||||
old := &drainCounter{Handler: okHandler()}
|
||||
sw := &swapHandler{}
|
||||
sw.cur.Store(newGeneration(old, func() {}))
|
||||
g := sw.cur.Load()
|
||||
g.enter() // a request is still running, so the close waits for it
|
||||
sw.swap(newGeneration(okHandler(), func() {}), time.Hour)
|
||||
if old.drains.Load() != 1 {
|
||||
t.Errorf("retired gateway drains = %d, want 1: its idle MCP streams would hold it open", old.drains.Load())
|
||||
}
|
||||
g.leave()
|
||||
|
||||
cur := &drainCounter{Handler: okHandler()}
|
||||
sw.cur.Store(newGeneration(cur, func() {}))
|
||||
sw.drainIdleStreams()
|
||||
if cur.drains.Load() != 1 {
|
||||
t.Errorf("serving gateway drains = %d, want 1 on shutdown", cur.drains.Load())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user