Files
homelab-frontend/internal/server/server.go
T
30e0a83a50
CI / CI (push) Successful in 3m35s
feat(network): SSE optimization for local LLM streaming (#31 #32 #33) (#26)
Addresses three critical network issues for LLM streaming performance:

**#33 Disable proxy buffering for SSE**
- Add X-Accel-Buffering: no header to response
- Tells nginx/Ingress to stream events immediately instead of buffering

**#32 HTTP/2 multiplexing for concurrent streams**
- Enable HTTP/2 in server config via http2.ConfigureServer()
- Increase MaxConnsPerHost to 10 for better concurrency
- Allows multiple concurrent LLM requests without blocking

**#31 TCP backpressure for streaming LLM responses**
- Set TCP_NODELAY on dialer to disable Nagle's algorithm
- Reduces latency by sending small packets immediately
- Critical for low TTFT (time-to-first-token) under load

**Tests added:**
- TestTCPBackpressure: Verifies TCP backpressure handling with slow client
- TestConcurrentSSEStreams: Confirms HTTP/2 multiplexing works correctly

---------

Co-authored-by: poison <[email protected]>
Reviewed-on: #26
Co-authored-by: poimen <[email protected]>
2026-09-13 23:37:33 +00:00

102 lines
3.1 KiB
Go

package server
import (
"context"
"net"
"net/http"
"sync"
"time"
"golang.org/x/net/http2"
)
// Server wraps an HTTP server with graceful shutdown support.
type Server struct {
httpServer *http.Server
shutdownTimeout time.Duration
listener net.Listener
listenerMu sync.RWMutex
healthChecker *HealthChecker
}
// New creates a new Server with the given configuration.
func New(listenAddr string, shutdownTimeout time.Duration, handler http.Handler) *Server {
httpServer := &http.Server{
Addr: listenAddr,
Handler: handler,
// ReadHeaderTimeout (not ReadTimeout) and a long WriteTimeout: both
// ReadTimeout and WriteTimeout are absolute deadlines covering the
// whole request/response body, not inactivity timeouts -- a 15s
// WriteTimeout here was killing in-progress LLM SSE streams (proxy.go's
// outbound transport deliberately avoids this same mistake). Mirrors
// the edge nginx Ingress's proxy-read/send-timeout of 3600s.
ReadHeaderTimeout: 15 * time.Second,
WriteTimeout: 1 * time.Hour,
IdleTimeout: 60 * time.Second,
}
// Issue #32: Enable HTTP/2 for multiplexing concurrent streams.
// This allows multiple LLM requests over a single connection,
// improving throughput and reducing latency for concurrent clients.
if err := http2.ConfigureServer(httpServer, nil); err != nil {
// Silently fail HTTP/2 config (shouldn't happen, but gracefully degrade)
// Server will still work with HTTP/1.1
}
return &Server{
httpServer: httpServer,
shutdownTimeout: shutdownTimeout,
healthChecker: NewHealthChecker(false, false),
}
}
// ListenAndServe starts the HTTP server and blocks until it exits.
// It returns the error from the server (if any), which will be
// http.ErrServerClosed if Shutdown was called.
func (s *Server) ListenAndServe() error {
listener, err := net.Listen("tcp", s.httpServer.Addr)
if err != nil {
return err
}
s.listenerMu.Lock()
s.listener = listener
s.listenerMu.Unlock()
return s.httpServer.Serve(listener)
}
// Shutdown gracefully shuts down the server. It stops accepting new
// connections and waits for in-flight requests to complete, with a
// bounded deadline. If the deadline is exceeded, it returns an error.
func (s *Server) Shutdown(ctx context.Context) error {
// Create a new context with the shutdown timeout
shutdownCtx, cancel := context.WithTimeout(ctx, s.shutdownTimeout)
defer cancel()
return s.httpServer.Shutdown(shutdownCtx)
}
// Addr returns the network address the server is listening on.
func (s *Server) Addr() string {
s.listenerMu.RLock()
defer s.listenerMu.RUnlock()
if s.listener != nil {
return s.listener.Addr().String()
}
return s.httpServer.Addr
}
// HealthChecker returns the server's health checker.
func (s *Server) HealthChecker() *HealthChecker {
return s.healthChecker
}
// SetHealthChecker sets the server's health checker.
func (s *Server) SetHealthChecker(hc *HealthChecker) {
s.healthChecker = hc
}
// SetHandler sets the server's HTTP handler.
func (s *Server) SetHandler(handler http.Handler) {
s.httpServer.Handler = handler
}