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:
@@ -676,3 +676,62 @@ func TestGetStatusProxyProtocol(t *testing.T) {
|
||||
t.Fatal("expected proxy_protocol = false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetListenerMaxConnections(t *testing.T) {
|
||||
env := setup(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// Set max connections.
|
||||
_, err := env.client.SetListenerMaxConnections(ctx, &pb.SetListenerMaxConnectionsRequest{
|
||||
ListenerAddr: ":443",
|
||||
MaxConnections: 5000,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SetListenerMaxConnections: %v", err)
|
||||
}
|
||||
|
||||
// Verify via GetStatus.
|
||||
resp, err := env.client.GetStatus(ctx, &pb.GetStatusRequest{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetStatus: %v", err)
|
||||
}
|
||||
if resp.Listeners[0].MaxConnections != 5000 {
|
||||
t.Fatalf("max_connections = %d, want 5000", resp.Listeners[0].MaxConnections)
|
||||
}
|
||||
|
||||
// Verify DB persistence.
|
||||
l, _ := env.store.GetListenerByAddr(":443")
|
||||
if l.MaxConnections != 5000 {
|
||||
t.Fatalf("DB max_connections = %d, want 5000", l.MaxConnections)
|
||||
}
|
||||
|
||||
// Set to 0 (unlimited).
|
||||
_, err = env.client.SetListenerMaxConnections(ctx, &pb.SetListenerMaxConnectionsRequest{
|
||||
ListenerAddr: ":443",
|
||||
MaxConnections: 0,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SetListenerMaxConnections to 0: %v", err)
|
||||
}
|
||||
|
||||
resp, _ = env.client.GetStatus(ctx, &pb.GetStatusRequest{})
|
||||
if resp.Listeners[0].MaxConnections != 0 {
|
||||
t.Fatalf("max_connections = %d, want 0", resp.Listeners[0].MaxConnections)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetListenerMaxConnectionsNotFound(t *testing.T) {
|
||||
env := setup(t)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := env.client.SetListenerMaxConnections(ctx, &pb.SetListenerMaxConnectionsRequest{
|
||||
ListenerAddr: ":9999",
|
||||
MaxConnections: 100,
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent listener")
|
||||
}
|
||||
if s, ok := status.FromError(err); !ok || s.Code() != codes.NotFound {
|
||||
t.Fatalf("expected NotFound, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user