Skip to content

Commit

Permalink
proxy: HTTP status 499 for 'Context canceled' errors (#2297)
Browse files Browse the repository at this point in the history
* Adding {when_unix_ms} requests placeholder (unix timestamp with a milliseconds precision)

* Add an 499 HTTP status code on user's cancel request as NGINX doing (instead of 502 Bad Gateway status with 'Context canceled' message)

* 499 HTTP status code was added as constant CustomStatusContextCancelled = 499
  • Loading branch information
Ranger-X authored and mholt committed Nov 16, 2018
1 parent 6f580c6 commit 05d0b21
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions caddyhttp/proxy/proxy.go
Expand Up @@ -262,6 +262,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusRequestEntityTooLarge, backendErr
}

if backendErr == context.Canceled {
return CustomStatusContextCancelled, backendErr
}

// failover; remember this failure for some time if
// request failure counting is enabled
timeout := host.FailTimeout
Expand Down Expand Up @@ -397,3 +401,5 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer)
}
}
}

const CustomStatusContextCancelled = 499
2 changes: 1 addition & 1 deletion caddyhttp/proxy/proxy_test.go
Expand Up @@ -1369,7 +1369,7 @@ func TestCancelRequest(t *testing.T) {

rec := httptest.NewRecorder()
status, err := p.ServeHTTP(rec, req)
expectedStatus, expectErr := http.StatusBadGateway, context.Canceled
expectedStatus, expectErr := CustomStatusContextCancelled, context.Canceled
if status != expectedStatus || err != expectErr {
t.Errorf("expect proxy handle return status[%d] with error[%v], but got status[%d] with error[%v]",
expectedStatus, expectErr, status, err)
Expand Down

0 comments on commit 05d0b21

Please sign in to comment.