proxy: use a new context for the outgoing request (#1358)

* proxy: use a new context for the outgoing request

fix issue #1345

Signed-off-by: Tw <tw19881113@gmail.com>

* proxy: add test for canceling the request

Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
Tw
2017-01-24 09:03:42 +08:00
committed by Matt Holt
parent 696b46f075
commit 38c76647c9
2 changed files with 52 additions and 0 deletions
+9
View File
@@ -12,6 +12,7 @@
package proxy
import (
"context"
"crypto/tls"
"io"
"net"
@@ -206,6 +207,14 @@ func (rp *ReverseProxy) ServeHTTP(rw http.ResponseWriter, outreq *http.Request,
rp.Director(outreq)
// Original incoming server request may be canceled by the
// user or by std lib(e.g. too many idle connections).
// Now we issue the new outgoing client request which
// doesn't depend on the original one. (issue 1345)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
outreq = outreq.WithContext(ctx)
res, err := transport.RoundTrip(outreq)
if err != nil {
return err