Skip to content

Commit 99c7698

Browse files
authored
handle client disconnection as 499 error: (#1417)
Signed-off-by: Bob Callaway <bcallaway@google.com>
1 parent ba3024d commit 99c7698

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

pkg/api/error.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package api
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"net/http"
2021
"regexp"
@@ -35,6 +36,7 @@ const (
3536
InconsistentDigestLengthTimestampRequest = "Message digest has incorrect length for specified algorithm"
3637
UnacceptedPolicyTimestampRequest = "unaccepted policy: requested TSA policy is not supported by the TSA"
3738
UnacceptedExtensionTimestampRequest = "unaccepted extension: requested extensions are not supported by the TSA"
39+
clientDisconnected = "client disconnected during request"
3840
)
3941

4042
func errorMsg(message string, code int) *models.Error {
@@ -54,6 +56,15 @@ func handleTimestampAPIError(params interface{}, code int, err error, message st
5456
handler := re.FindStringSubmatch(typeStr)[1]
5557

5658
logMsg := func(r *http.Request) {
59+
// If the client disconnected before we could respond, rewrite the
60+
// status to 499 (nginx-style "client closed request") so that the
61+
// response, request log, and metrics all agree that this wasn't a
62+
// server-side error we could have prevented. Also replace the
63+
// client-facing message so the JSON body reflects the true cause.
64+
if code >= http.StatusInternalServerError && r.Context().Err() == context.Canceled {
65+
code = 499
66+
message = clientDisconnected
67+
}
5768
if code < http.StatusInternalServerError {
5869
log.RequestIDLogger(r).Warnw(message, append([]interface{}{"handler", handler, "statusCode", code, "error", err}, fields...)...)
5970
} else {

0 commit comments

Comments
 (0)