Commit f459660c authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: keep idle conns sorted by usage

Addressing feedback from Alan Su in https://golang.org/cl/22655

Change-Id: Ie0724efea2b4da67503c074e265ec7f8d7de7791
Reviewed-on: https://go-review.googlesource.com/22709Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 6b019e21
...@@ -68,8 +68,8 @@ const DefaultMaxIdleConnsPerHost = 2 ...@@ -68,8 +68,8 @@ const DefaultMaxIdleConnsPerHost = 2
// See the package docs for more about HTTP/2. // See the package docs for more about HTTP/2.
type Transport struct { type Transport struct {
idleMu sync.Mutex idleMu sync.Mutex
wantIdle bool // user has requested to close all idle conns wantIdle bool // user has requested to close all idle conns
idleConn map[connectMethodKey][]*persistConn idleConn map[connectMethodKey][]*persistConn // most recently used at end
idleConnCh map[connectMethodKey]chan *persistConn idleConnCh map[connectMethodKey]chan *persistConn
idleLRU connLRU idleLRU connLRU
...@@ -690,7 +690,7 @@ func (t *Transport) getIdleConn(cm connectMethod) (pconn *persistConn, idleSince ...@@ -690,7 +690,7 @@ func (t *Transport) getIdleConn(cm connectMethod) (pconn *persistConn, idleSince
delete(t.idleConn, key) delete(t.idleConn, key)
} else { } else {
// 2 or more cached connections; use the most // 2 or more cached connections; use the most
// recently used one. // recently used one at the end.
pconn = pconns[len(pconns)-1] pconn = pconns[len(pconns)-1]
t.idleConn[key] = pconns[:len(pconns)-1] t.idleConn[key] = pconns[:len(pconns)-1]
} }
...@@ -740,7 +740,9 @@ func (t *Transport) removeIdleConnLocked(pconn *persistConn) { ...@@ -740,7 +740,9 @@ func (t *Transport) removeIdleConnLocked(pconn *persistConn) {
if v != pconn { if v != pconn {
continue continue
} }
pconns[i] = pconns[len(pconns)-1] // Slide down, keeping most recently-used
// conns at the end.
copy(pconns[i:], pconns[i+1:])
t.idleConn[key] = pconns[:len(pconns)-1] t.idleConn[key] = pconns[:len(pconns)-1]
break break
} }
......
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