fix(go): decouple frame publishing from shutdown context

This commit is contained in:
lingniu
2026-07-02 13:10:38 +08:00
parent 924903feb4
commit d10d5d75c3
2 changed files with 87 additions and 3 deletions

View File

@@ -51,6 +51,8 @@ type TCPServerConfig struct {
MaxConnections int
}
const frameOperationTimeout = 30 * time.Second
func NewTCPServer(cfg TCPServerConfig) (*TCPServer, error) {
if cfg.Protocol.Protocol == "" {
return nil, errors.New("protocol is required")
@@ -178,6 +180,9 @@ func (s *TCPServer) handleConnection(ctx context.Context, conn net.Conn) {
}
func (s *TCPServer) handleFrame(ctx context.Context, conn net.Conn, raw []byte, source string) {
frameCtx, cancelFrame := context.WithTimeout(context.WithoutCancel(ctx), frameOperationTimeout)
defer cancelFrame()
receivedAtMS := time.Now().UnixMilli()
env, err := s.protocol.Parse(raw, receivedAtMS, source)
if err != nil {
@@ -192,7 +197,7 @@ func (s *TCPServer) handleFrame(ctx context.Context, conn net.Conn, raw []byte,
}
env.EventID = env.StableEventID()
} else {
resolved, resolveErr := s.resolver.Resolve(ctx, env)
resolved, resolveErr := s.resolver.Resolve(frameCtx, env)
if resolveErr != nil {
s.logger.Warn("identity resolve failed", "protocol", s.protocol.Protocol, "event_id", env.StableEventID(), "error", resolveErr)
if env.Parsed == nil {
@@ -205,14 +210,14 @@ func (s *TCPServer) handleFrame(ctx context.Context, conn net.Conn, raw []byte,
}
}
if err := s.sink.PublishRaw(ctx, env); err != nil {
if err := s.sink.PublishRaw(frameCtx, env); err != nil {
s.logger.Error("publish raw failed", "protocol", s.protocol.Protocol, "event_id", env.StableEventID(), "error", err)
return
}
if env.ParseStatus == envelope.ParseBadFrame {
return
}
if err := s.sink.PublishUnified(ctx, env); err != nil {
if err := s.sink.PublishUnified(frameCtx, env); err != nil {
s.logger.Error("publish unified failed", "protocol", s.protocol.Protocol, "event_id", env.StableEventID(), "error", err)
return
}