feat: observe HTTP2 reset and goaway frames

This commit is contained in:
2026-09-23 00:20:37 +08:00
parent 73a05b8c3c
commit 1b9215114f
5 changed files with 137 additions and 3 deletions

View File

@@ -59,6 +59,8 @@ type Server struct {
rejectedReqs atomic.Uint64
protocolErrors atomic.Uint64
goAwaySent atomic.Uint64
rstStreams atomic.Uint64
goAwayFrames atomic.Uint64
activeConns atomic.Int64
activeRequests atomic.Int64
wg sync.WaitGroup
@@ -72,6 +74,8 @@ type Stats struct {
RejectedRequests uint64 `json:"rejectedRequests"`
ProtocolErrors uint64 `json:"protocolErrors"`
GoAwaySent uint64 `json:"goAwaySent"`
RSTStreams uint64 `json:"rstStreams"`
GoAwayFrames uint64 `json:"goAwayFrames"`
ActiveConnections int64 `json:"activeConnections"`
ActiveRequests int64 `json:"activeRequests"`
}
@@ -97,6 +101,8 @@ func (s *Server) Stats() Stats {
RejectedRequests: s.rejectedReqs.Load(),
ProtocolErrors: s.protocolErrors.Load(),
GoAwaySent: s.goAwaySent.Load(),
RSTStreams: s.rstStreams.Load(),
GoAwayFrames: s.goAwayFrames.Load(),
ActiveConnections: s.activeConns.Load(),
ActiveRequests: s.activeRequests.Load(),
}
@@ -356,7 +362,23 @@ func (s *Server) serveConn(ctx context.Context, protocolServer *xhttp2.Server, b
}
s.protocolConns[conn] = struct{}{}
s.mu.Unlock()
protocolServer.ServeConn(conn, &xhttp2.ServeConnOpts{
observed := &observedConn{
Conn: conn,
readParser: frameParser{prefaceRemain: 24},
onFrame: func(direction frameDirection, frameType byte) {
if direction == frameRead && frameType == frameTypeRSTStream {
s.rstStreams.Add(1)
}
if direction == frameWrite && frameType == frameTypeGoAway {
s.goAwayFrames.Add(1)
}
},
}
var protocolConn net.Conn = observed
if tlsConn, ok := conn.(*tls.Conn); ok {
protocolConn = &observedTLSConn{observedConn: observed, state: tlsConn.ConnectionState()}
}
protocolServer.ServeConn(protocolConn, &xhttp2.ServeConnOpts{
Context: ctx, Handler: handler, BaseConfig: baseConfig,
})
}