Add per-listener connection limits
Configurable maximum concurrent connections per listener. When the limit is reached, new connections are closed immediately after accept. 0 means unlimited (default, preserving existing behavior). Config: Listener gains max_connections field, validated non-negative. DB: Migration 3 adds listeners.max_connections column. UpdateListenerMaxConns method for runtime changes via gRPC. CreateListener updated to persist max_connections on seed. Server: ListenerState/ListenerData gain MaxConnections. Limit checked in serve() after Accept but before handleConn — if ActiveConnections >= MaxConnections, connection is closed and the accept loop continues. SetMaxConnections method for runtime updates. Proto: SetListenerMaxConnections RPC added. ListenerStatus gains max_connections field. Generated code regenerated. gRPC server: SetListenerMaxConnections implements write-through (DB first, then in-memory update). GetStatus includes max_connections. Client: SetListenerMaxConnections method, MaxConnections in ListenerStatus. Tests: DB CRUD and UpdateListenerMaxConns, server connection limit enforcement (accept 2, reject 3rd, close one, accept again), gRPC SetListenerMaxConnections round-trip with DB persistence, not-found error handling. Also updates PROJECT_PLAN.md with phases 6-8 and PROGRESS.md with tracking for the new features. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -34,6 +34,7 @@ type ListenerState struct {
|
||||
ID int64 // database primary key
|
||||
Addr string
|
||||
ProxyProtocol bool
|
||||
MaxConnections int64 // 0 = unlimited
|
||||
routes map[string]RouteInfo // lowercase hostname → route info
|
||||
mu sync.RWMutex
|
||||
ActiveConnections atomic.Int64
|
||||
@@ -41,6 +42,13 @@ type ListenerState struct {
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
// SetMaxConnections updates the connection limit at runtime.
|
||||
func (ls *ListenerState) SetMaxConnections(n int64) {
|
||||
ls.mu.Lock()
|
||||
defer ls.mu.Unlock()
|
||||
ls.MaxConnections = n
|
||||
}
|
||||
|
||||
// Routes returns a snapshot of the listener's route table.
|
||||
func (ls *ListenerState) Routes() map[string]RouteInfo {
|
||||
ls.mu.RLock()
|
||||
@@ -93,10 +101,11 @@ func (ls *ListenerState) lookupRoute(hostname string) (RouteInfo, bool) {
|
||||
|
||||
// ListenerData holds the data needed to construct a ListenerState.
|
||||
type ListenerData struct {
|
||||
ID int64
|
||||
Addr string
|
||||
ProxyProtocol bool
|
||||
Routes map[string]RouteInfo // lowercase hostname → route info
|
||||
ID int64
|
||||
Addr string
|
||||
ProxyProtocol bool
|
||||
MaxConnections int64
|
||||
Routes map[string]RouteInfo // lowercase hostname → route info
|
||||
}
|
||||
|
||||
// Server is the mc-proxy server. It manages listeners, firewall evaluation,
|
||||
@@ -116,11 +125,12 @@ func New(cfg *config.Config, fw *firewall.Firewall, listenerData []ListenerData,
|
||||
var listeners []*ListenerState
|
||||
for _, ld := range listenerData {
|
||||
listeners = append(listeners, &ListenerState{
|
||||
ID: ld.ID,
|
||||
Addr: ld.Addr,
|
||||
ProxyProtocol: ld.ProxyProtocol,
|
||||
routes: ld.Routes,
|
||||
activeConns: make(map[net.Conn]struct{}),
|
||||
ID: ld.ID,
|
||||
Addr: ld.Addr,
|
||||
ProxyProtocol: ld.ProxyProtocol,
|
||||
MaxConnections: ld.MaxConnections,
|
||||
routes: ld.Routes,
|
||||
activeConns: make(map[net.Conn]struct{}),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -229,6 +239,13 @@ func (s *Server) serve(ctx context.Context, ln net.Listener, ls *ListenerState)
|
||||
continue
|
||||
}
|
||||
|
||||
// Enforce per-listener connection limit.
|
||||
if ls.MaxConnections > 0 && ls.ActiveConnections.Load() >= ls.MaxConnections {
|
||||
conn.Close()
|
||||
s.logger.Debug("connection limit reached", "addr", ls.Addr, "limit", ls.MaxConnections)
|
||||
continue
|
||||
}
|
||||
|
||||
s.wg.Add(1)
|
||||
ls.ActiveConnections.Add(1)
|
||||
go s.handleConn(ctx, conn, ls)
|
||||
|
||||
Reference in New Issue
Block a user