Merge pull request #236 from nghialv/master

Fix goroutine leak
This commit is contained in:
Björn Rabenstein 2016-09-12 12:34:28 +02:00 committed by GitHub
commit 8aae34f3ff
1 changed files with 29 additions and 20 deletions

View File

@ -267,12 +267,7 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
now := time.Now() now := time.Now()
delegate := &responseWriterDelegator{ResponseWriter: w} delegate := &responseWriterDelegator{ResponseWriter: w}
out := make(chan int) out := computeApproximateRequestSize(r)
urlLen := 0
if r.URL != nil {
urlLen = len(r.URL.String())
}
go computeApproximateRequestSize(r, out, urlLen)
_, cn := w.(http.CloseNotifier) _, cn := w.(http.CloseNotifier)
_, fl := w.(http.Flusher) _, fl := w.(http.Flusher)
@ -297,23 +292,37 @@ func InstrumentHandlerFuncWithOpts(opts SummaryOpts, handlerFunc func(http.Respo
}) })
} }
func computeApproximateRequestSize(r *http.Request, out chan int, s int) { func computeApproximateRequestSize(r *http.Request) <-chan int {
s += len(r.Method) // Get URL length in current go routine for avoiding a race condition.
s += len(r.Proto) // HandlerFunc that runs in parallel may modify the URL.
for name, values := range r.Header { s := 0
s += len(name) if r.URL != nil {
for _, value := range values { s += len(r.URL.String())
s += len(value) }
out := make(chan int, 1)
go func() {
s += len(r.Method)
s += len(r.Proto)
for name, values := range r.Header {
s += len(name)
for _, value := range values {
s += len(value)
}
} }
} s += len(r.Host)
s += len(r.Host)
// N.B. r.Form and r.MultipartForm are assumed to be included in r.URL. // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL.
if r.ContentLength != -1 { if r.ContentLength != -1 {
s += int(r.ContentLength) s += int(r.ContentLength)
} }
out <- s out <- s
close(out)
}()
return out
} }
type responseWriterDelegator struct { type responseWriterDelegator struct {