Files
cyh 3425ba3663 fix(gateway): bound stalled request body reads and classify read failures
Gateway handlers such as Anthropic /v1/messages read the whole request body
before they know the model or account. The HTTP server sets no ReadTimeout
(large bodies may legitimately take long), so a stalled upload held the
handler until the client gave up, and every read failure then surfaced as a
generic 400 "Failed to read request body" with no diagnostics.

- Add server.request_body_idle_timeout (default 60s, 0 disables). A body
  read that receives no bytes for that long fails with 408. Each Read re-arms
  the deadline, so total upload time is not limited and handler pauses are
  not counted. HTTP/2 deadlines are armed only during a Read; HTTP/1 keeps
  one armed while the body is unfinished so net/http's own drain of an
  unread body cannot hang an early error response.
- Classify body read failures in the shared reader: read timeout (408),
  client disconnect (499), truncated body, decode errors and size limit.
  Other failures keep the stable 400 message. Map 408 to DEADLINE_EXCEEDED
  in Google-format errors.
- Log payload-free diagnostics for every gateway and middleware body read
  failure: kind, protocol, declared length, encoding, bytes received and
  read duration.
2026-10-31 11:45:55 +08:00

30 lines
748 B
Go

// Package googleapi provides helpers for Google-style API responses.
package googleapi
import "net/http"
// HTTPStatusToGoogleStatus maps HTTP status codes to Google-style error status strings.
func HTTPStatusToGoogleStatus(status int) string {
switch status {
case http.StatusBadRequest:
return "INVALID_ARGUMENT"
case http.StatusUnauthorized:
return "UNAUTHENTICATED"
case http.StatusForbidden:
return "PERMISSION_DENIED"
case http.StatusNotFound:
return "NOT_FOUND"
case http.StatusRequestTimeout:
return "DEADLINE_EXCEEDED"
case http.StatusTooManyRequests:
return "RESOURCE_EXHAUSTED"
case 499: // client closed request
return "CANCELLED"
default:
if status >= 500 {
return "INTERNAL"
}
return "UNKNOWN"
}
}