test: inspect proxy drain state

This commit is contained in:
2026-09-22 16:16:36 +08:00
parent 3b4d0a3fad
commit 4f97aa0f3d
2 changed files with 13 additions and 1 deletions

View File

@@ -46,6 +46,7 @@ type snapshot struct {
Goroutines int `json:"goroutines"`
Connections int64 `json:"connections"`
Accepted int64 `json:"accepted"`
ActiveRequests int64 `json:"activeRequests,omitempty"`
FileDescriptors int `json:"fileDescriptors,omitempty"`
}
@@ -193,12 +194,14 @@ func serve(adapter string, tlsEnabled bool) error {
})
done := make(chan error, 1)
var shutdown func(context.Context) error
var stats func() native.Stats
switch adapter {
case "native":
server := native.New(application)
server.MaxConnections = 8192
server.MaxConcurrentRequests = 4096
shutdown = server.Shutdown
stats = server.Stats
go func() { done <- server.Serve(context.Background(), serveListener) }()
case "nethttp":
server := &http.Server{
@@ -246,11 +249,16 @@ func serve(adapter string, tlsEnabled bool) error {
}
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
connections, accepted, activeRequests := tracked.active.Load(), tracked.accepted.Load(), int64(0)
if stats != nil {
value := stats()
connections, accepted, activeRequests = value.ActiveConnections, int64(value.AcceptedConnections), value.ActiveRequests
}
if err := encoder.Encode(snapshot{
HeapAlloc: memory.HeapAlloc, HeapInuse: memory.HeapInuse,
StackInuse: memory.StackInuse, TotalAlloc: memory.TotalAlloc,
Mallocs: memory.Mallocs, NumGC: memory.NumGC,
Goroutines: runtime.NumGoroutine(), Connections: tracked.active.Load(), Accepted: tracked.accepted.Load(),
Goroutines: runtime.NumGoroutine(), Connections: connections, Accepted: accepted, ActiveRequests: activeRequests,
FileDescriptors: openFileDescriptorCount(),
}); err != nil {
return err

View File

@@ -249,6 +249,10 @@ def main() -> None:
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
assert backend.stdin is not None
assert backend.stdout is not None
backend.stdin.write('"snapshot"\n')
backend.stdin.flush()
print(f"native after proxy drain: {backend.stdout.readline().strip()}", flush=True)
backend.stdin.write('"stop"\n')
backend.stdin.flush()
exit_code = backend.wait(timeout=5)