Commit 05d0b213 authored by Makeev Ivan's avatar Makeev Ivan Committed by Matt Holt

proxy: HTTP status 499 for 'Context canceled' errors (#2297)

* 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
parent 6f580c6a
...@@ -262,6 +262,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { ...@@ -262,6 +262,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
return http.StatusRequestEntityTooLarge, backendErr return http.StatusRequestEntityTooLarge, backendErr
} }
if backendErr == context.Canceled {
return CustomStatusContextCancelled, backendErr
}
// failover; remember this failure for some time if // failover; remember this failure for some time if
// request failure counting is enabled // request failure counting is enabled
timeout := host.FailTimeout timeout := host.FailTimeout
...@@ -397,3 +401,5 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) ...@@ -397,3 +401,5 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer)
} }
} }
} }
const CustomStatusContextCancelled = 499
...@@ -1369,7 +1369,7 @@ func TestCancelRequest(t *testing.T) { ...@@ -1369,7 +1369,7 @@ func TestCancelRequest(t *testing.T) {
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
status, err := p.ServeHTTP(rec, req) status, err := p.ServeHTTP(rec, req)
expectedStatus, expectErr := http.StatusBadGateway, context.Canceled expectedStatus, expectErr := CustomStatusContextCancelled, context.Canceled
if status != expectedStatus || err != expectErr { if status != expectedStatus || err != expectErr {
t.Errorf("expect proxy handle return status[%d] with error[%v], but got status[%d] with error[%v]", t.Errorf("expect proxy handle return status[%d] with error[%v], but got status[%d] with error[%v]",
expectedStatus, expectErr, status, err) expectedStatus, expectErr, status, err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment