Skip to content

Commit

Permalink
context, cancel を分ける
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexa committed Nov 26, 2024
1 parent 0b02fef commit eea6b1f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
Msg("NEW-REQUEST")

// リトライ時にこれ以降の処理のみを cancel する
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx1, cancel1 := context.WithCancel(ctx)
defer cancel1()

reader, err := serviceHandler.Handle(ctx, r)
if err != nil {
Expand All @@ -162,11 +162,11 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
serviceHandler.UpdateRetryCount()

// 切断検知のために、クライアントから送られてくるパケットは受信し続ける
ctx, cancelPacketDiscard := context.WithCancel(ctx)
ctx2, cancelPacketDiscard := context.WithCancel(ctx1)
defer cancelPacketDiscard()

errCh := make(chan error)
go discardPacket(ctx, r, errCh)
go discardPacket(ctx2, r, errCh)

// 連続のリトライを避けるために少し待つ
retryTimer := time.NewTimer(time.Duration(s.config.RetryIntervalMs) * time.Millisecond)
Expand Down Expand Up @@ -248,13 +248,7 @@ func (s *Server) createSpeechHandler(serviceType string, onResultFunc func(conte
serviceHandler.UpdateRetryCount()

// TODO: 必要な場合は連続のリトライを避けるために少し待つ処理を追加する
zlog.Debug().Err(err).
Str("channel_id", h.SoraChannelID).
Str("connection_id", h.SoraConnectionID).
Int("retry_count", serviceHandler.GetRetryCount()).
Msg("RETRYING")

cancel()
cancel1()
break
} else {
zlog.Error().
Expand Down

0 comments on commit eea6b1f

Please sign in to comment.