mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 08:01:07 +00:00
fix: enhance request timeout handling to distinguish between cancellation and deadline expiration
This commit is contained in:
1 parent
6ea58d3a89
commit
dbd1a29fda
2 files changed
+42
-10
No files matched your search
@@ -116,25 +116,34 @@ func (h *Handler) proxyVoiceWebSocket(c *gin.Context, pathValue string) {
|
||||
first := <-errCh
|
||||
if !isNormalVoiceWSClose(first.result.err) {
|
||||
outcomeMu.Lock()
|
||||
if (first.upstreamSide && !first.result.writeFailed) || (!first.upstreamSide && first.result.writeFailed) {
|
||||
outcome.ErrorCode = "upstream_stream_interrupted"
|
||||
outcome.UpstreamFailed = true
|
||||
} else {
|
||||
outcome.ErrorCode = "client_stream_interrupted"
|
||||
// A request-context watcher may have closed both sockets first. Keep
|
||||
// its timeout/cancellation outcome instead of replacing it with the
|
||||
// close error produced by our own cleanup.
|
||||
if outcome.ErrorCode == "" {
|
||||
if (first.upstreamSide && !first.result.writeFailed) || (!first.upstreamSide && first.result.writeFailed) {
|
||||
outcome.ErrorCode = "upstream_stream_interrupted"
|
||||
outcome.UpstreamFailed = true
|
||||
} else {
|
||||
outcome.ErrorCode = "client_stream_interrupted"
|
||||
}
|
||||
}
|
||||
outcomeMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// watchRequestTimeout calls closeAll once ctx is done, recording errorCode as
|
||||
// "request_timeout" unless some other outcome was already set. Extracted so
|
||||
// the cancellation-triggers-cleanup behavior can be tested without a real
|
||||
// WebSocket upgrade.
|
||||
// watchRequestTimeout closes the session once its request context is done.
|
||||
// Deadline cancellation is the configured request timeout; ordinary
|
||||
// cancellation is kept distinct so client disconnects and server shutdowns
|
||||
// are not reported as timeouts. Existing transport outcomes always win.
|
||||
func watchRequestTimeout(ctx context.Context, mu *sync.Mutex, errorCode *string, closeAll func()) {
|
||||
<-ctx.Done()
|
||||
mu.Lock()
|
||||
if *errorCode == "" {
|
||||
*errorCode = "request_timeout"
|
||||
if ctx.Err() == context.DeadlineExceeded {
|
||||
*errorCode = "request_timeout"
|
||||
} else {
|
||||
*errorCode = "request_canceled"
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
closeAll()
|
||||
|
||||
@@ -59,3 +59,26 @@ func TestWatchRequestTimeoutDoesNotOverwriteAnExistingOutcome(t *testing.T) {
|
||||
t.Fatalf("errorCode = %q, want the pre-existing outcome preserved", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatchRequestTimeoutDistinguishesRequestCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
var mu sync.Mutex
|
||||
errorCode := ""
|
||||
closed := make(chan struct{})
|
||||
go watchRequestTimeout(ctx, &mu, &errorCode, func() { close(closed) })
|
||||
cancel()
|
||||
|
||||
select {
|
||||
case <-closed:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("watchRequestTimeout did not close the canceled session")
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
got := errorCode
|
||||
mu.Unlock()
|
||||
if got != "request_canceled" {
|
||||
t.Fatalf("errorCode = %q, want request_canceled", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user