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

View File

@@ -249,6 +249,10 @@ def main() -> None:
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
assert backend.stdin is not None 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.write('"stop"\n')
backend.stdin.flush() backend.stdin.flush()
exit_code = backend.wait(timeout=5) exit_code = backend.wait(timeout=5)