Browse Source

proxy: Fix connection leak when client disconnect

established connections were leaked when client disconnected before
proxyreq completes. This happens all time for wait=true requests.
Shota Fukumori (sora_h) 10 years ago
parent
commit
a68efe7d1e
1 changed files with 19 additions and 0 deletions
  1. 19 0
      proxy/reverse.go

+ 19 - 0
proxy/reverse.go

@@ -73,6 +73,25 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request
 		return
 	}
 
+	completeCh := make(chan bool, 1)
+	closeNotifier, ok := rw.(http.CloseNotifier)
+	if ok {
+		go func() {
+			select {
+			case <-closeNotifier.CloseNotify():
+				tp, ok := p.transport.(*http.Transport)
+				if ok {
+					tp.CancelRequest(proxyreq)
+				}
+			case <-completeCh:
+			}
+		}()
+
+		defer func() {
+			completeCh <- true
+		}()
+	}
+
 	var res *http.Response
 	var err error