Files
traefik-llmgateway/cmd/gateway/reload.go
T
lukaszraczyloandGitHub f1b198bce4 fix(security): audit run-4 fixes and run-3 leftovers (#26)
Corrects passthrough metering, the cost gate, credential redaction, reload drain, response headers and the discovery cap.
2026-11-29 10:50:36 +00:00

271 lines
7.9 KiB
Go

package main
import (
"context"
"crypto/sha256"
"fmt"
"net/http"
"os"
"sync"
"sync/atomic"
"time"
traefikllmgateway "github.com/lukaszraczylo/traefik-llmgateway"
)
// genRetired is the retired flag in a generation's state word. The bits below it count the requests running on it.
// The flag and the count share one atomic so that leave sees both in one operation (formal/Gateway/Reload.lean).
const genRetired int64 = 1 << 62
// generation is one constructed gateway and the requests running on it.
type generation struct {
h http.Handler
closeFn func()
// overdue reports a generation that outlived its drain timeout, with the requests still running on it.
overdue func(running int64)
timer atomic.Pointer[time.Timer]
state atomic.Int64
once sync.Once
shutDone atomic.Bool
}
func newGeneration(h http.Handler, closeFn func()) *generation {
return &generation{h: h, closeFn: closeFn}
}
const overdueFormat = "reload: a replaced gateway still has %d running requests after %s; it stays open until they end"
// loggedGeneration wraps h in a generation that closes the gateway and warns when it outlives drain once retired.
func loggedGeneration(h http.Handler, log *logger, drain time.Duration) *generation {
g := newGeneration(h, func() { closeGateway(h, log) })
g.overdue = func(running int64) {
log.warn(overdueFormat, running, drain)
}
return g
}
// enter counts a request in. It returns false when the generation is retired; the caller must not serve on it.
func (g *generation) enter() bool {
if g.state.Add(1)&genRetired != 0 {
g.leave()
return false
}
return true
}
func (g *generation) leave() {
if g.state.Add(-1) == genRetired {
g.shut()
}
}
// retire stops new requests. The gateway closes when the last running request leaves, however long that takes.
// A request is never cancelled: closing the gateway under it would drop its Redis-backed accounting.
// Each maxWait after the retire, a generation that still has requests reports them through overdue.
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.armOverdue(maxWait)
}
// armOverdue schedules the next overdue report in d.
func (g *generation) armOverdue(d time.Duration) {
g.timer.Store(time.AfterFunc(d, func() {
if g.shutDone.Load() {
return
}
if g.overdue != nil {
g.overdue(g.state.Load() &^ genRetired)
}
g.armOverdue(d)
}))
// A shut that ran before the Store could not stop the timer, so stop it here.
if g.shutDone.Load() {
g.stopTimer()
}
}
// markRetired sets the retired flag. It returns true when requests are still running, so the close waits for them.
// It closes an idle generation at once, and returns false when the generation was already retired.
func (g *generation) markRetired() bool {
for {
old := g.state.Load()
if old&genRetired != 0 {
return false
}
if g.state.CompareAndSwap(old, old|genRetired) {
if old == 0 {
g.shut()
return false
}
return true
}
}
}
// shut closes the gateway once and stops the overdue timer, so a closed generation is not held until it fires.
func (g *generation) shut() {
g.once.Do(func() {
g.closeFn()
g.shutDone.Store(true)
g.stopTimer()
})
}
func (g *generation) stopTimer() {
if t := g.timer.Load(); t != nil {
t.Stop()
}
}
// swapHandler serves every request on the current generation.
type swapHandler struct {
cur atomic.Pointer[generation]
}
func (s *swapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for {
g := s.cur.Load()
if g.enter() {
defer g.leave()
g.h.ServeHTTP(w, r)
return
}
// A swap installs the newer generation before it retires the old one, so an unchanged
// pointer means closeCurrent ran and no other generation will come.
if s.cur.Load() == g {
http.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
}
}
// swap installs next and retires the generation it replaces.
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() {
if g := s.cur.Load(); g != nil {
g.markRetired()
g.shut()
}
}
// gatewayBuilder decodes raw as the config at path and constructs a gateway from it.
func gatewayBuilder(ctx context.Context, path, name string, log *logger) func(raw []byte) (http.Handler, error) {
return func(raw []byte) (http.Handler, error) {
cfg, err := decodeConfig(path, raw)
if err != nil {
return nil, err
}
logConfigWarnings(cfg, log)
h, err := traefikllmgateway.New(ctx, terminalHandler(), cfg, name)
if err != nil {
return nil, fmt.Errorf("construct gateway: %w", err)
}
return h, nil
}
}
// reloader rebuilds the gateway when the config file content changes. Only its run goroutine calls check.
type reloader struct {
build func(raw []byte) (http.Handler, error)
swap *swapHandler
log *logger
path string
readErr string
buildErr string
drain time.Duration
last [sha256.Size]byte
}
// newReloader starts from the content the serving gateway was built from.
func newReloader(path string, raw []byte, build func([]byte) (http.Handler, error), swap *swapHandler, drain time.Duration, log *logger) *reloader {
return &reloader{build: build, swap: swap, log: log, path: path, drain: drain, last: sha256.Sum256(raw)}
}
// check rebuilds when the file content differs from the serving config, or always when force is set.
// A rejected config keeps the old gateway serving. It is retried on every check, because the cause can be outside
// the file (a secret file not mounted yet); each distinct error is logged once.
func (r *reloader) check(force bool) {
raw, err := os.ReadFile(r.path)
if err != nil {
r.readErr = r.logOnce(r.readErr, fmt.Sprintf("reload: read %s: %v; still serving the previous config", r.path, err))
return
}
r.readErr = ""
sum := sha256.Sum256(raw)
if sum == r.last && !force {
return
}
h, err := r.build(raw)
if err != nil {
r.buildErr = r.logOnce(r.buildErr, fmt.Sprintf("reload: config %s rejected; still serving the previous config: %v", r.path, err))
return
}
r.buildErr = ""
r.last = sum
g := newGeneration(h, func() { closeGateway(h, r.log) })
g.overdue = func(running int64) {
r.log.warn(overdueFormat, running, r.drain)
}
r.swap.swap(g, r.drain)
r.log.info("reload: config %s applied", r.path)
}
// logOnce logs msg unless it equals prev, and returns msg as the new previous error.
func (r *reloader) logOnce(prev, msg string) string {
if msg != prev {
r.log.error("%s", msg)
}
return msg
}
// run checks the file every interval (never when interval is 0) and on every value from hup, until ctx ends.
func (r *reloader) run(ctx context.Context, interval time.Duration, hup <-chan os.Signal) {
var tick <-chan time.Time
if interval > 0 {
t := time.NewTicker(interval)
defer t.Stop()
tick = t.C
}
for {
select {
case <-ctx.Done():
return
case <-tick:
// select picks at random when ctx and tick are both ready; do not build after a shutdown signal.
if ctx.Err() != nil {
return
}
r.check(false)
case <-hup:
r.log.info("reload: SIGHUP, rebuilding from %s", r.path)
r.check(true)
}
}
}