Browse Source

Merge pull request #2994 from webner/feature/cancel-proxy-request

proxy: handle canceled proxy request gracefully
Yicheng Qin 10 years ago
parent
commit
028a1d6dd4
1 changed files with 8 additions and 0 deletions
  1. 8 0
      proxy/reverse.go

+ 8 - 0
proxy/reverse.go

@@ -24,6 +24,7 @@ import (
 	"net/http"
 	"net/url"
 	"strings"
+	"sync/atomic"
 
 	"github.com/coreos/etcd/etcdserver/etcdhttp/httptypes"
 )
@@ -90,12 +91,16 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request
 		return
 	}
 
+	var requestClosed int32
 	completeCh := make(chan bool, 1)
 	closeNotifier, ok := rw.(http.CloseNotifier)
 	if ok {
 		go func() {
 			select {
 			case <-closeNotifier.CloseNotify():
+				atomic.StoreInt32(&requestClosed, 1)
+				log.Printf("proxy: client %v closed request prematurely", clientreq.RemoteAddr)
+
 				tp, ok := p.transport.(*http.Transport)
 				if ok {
 					tp.CancelRequest(proxyreq)
@@ -118,6 +123,9 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request
 		redirectRequest(proxyreq, ep.URL)
 
 		res, err = p.transport.RoundTrip(proxyreq)
+		if atomic.LoadInt32(&requestClosed) == 1 {
+			return
+		}
 		if err != nil {
 			log.Printf("proxy: failed to direct request to %s: %v", ep.URL.String(), err)
 			ep.Failed()