Skip to content

Commit 186fd3f

Browse files
tomberganbradfitz
authored andcommitted
[release-branch.go1.8] http2: fix lock contention slowdown due to gracefulShutdownCh
gracefulShutdownCh is shared by all connections in a server. When a server accumulates many connections (e.g., 5000 in the kubemark-5000 benchmark), we have 5000 serverConn.serve goroutines selecting on this channel. This means 5000 goroutines hammer the channel's lock, which causes severe lock contention. The fix in this CL is to make a local proxy for gracefulShutdownCh in each connection so that each connection selects on gracefulShutdownCh at most once per connection rather than once per serverConn.serve loop iteration. This fix is intended to be backported quickly into Go 1.8.2. The downside of this fix is 2KB extra stack usage per connection. A better fix will be implemented in Go 1.9. Unfortunately, I have been unable to reproduce this problem locally. This fix was verified by the kubernetes team. See: kubernetes/kubernetes#45216 (comment) Updates golang/go#20302 Change-Id: I19ab19268a6ccab9b6e9dffa0cfbc89b8c7d0f19 Reviewed-on: https://go-review.googlesource.com/43455 Run-TryBot: Tom Bergan <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> (cherry picked from commit d3ede01) Reviewed-on: https://go-review.googlesource.com/43459 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Chris Broadfoot <[email protected]>
1 parent 242b6b3 commit 186fd3f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

http2/server.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,13 @@ func (sc *serverConn) serve() {
722722
sc.idleTimerCh = sc.idleTimer.C
723723
}
724724

725-
var gracefulShutdownCh <-chan struct{}
725+
var gracefulShutdownCh chan struct{}
726726
if sc.hs != nil {
727-
gracefulShutdownCh = h1ServerShutdownChan(sc.hs)
727+
ch := h1ServerShutdownChan(sc.hs)
728+
if ch != nil {
729+
gracefulShutdownCh = make(chan struct{})
730+
go sc.awaitGracefulShutdown(ch, gracefulShutdownCh)
731+
}
728732
}
729733

730734
go sc.readFrames() // closed by defer sc.conn.Close above
@@ -773,6 +777,14 @@ func (sc *serverConn) serve() {
773777
}
774778
}
775779

780+
func (sc *serverConn) awaitGracefulShutdown(sharedCh <-chan struct{}, privateCh chan struct{}) {
781+
select {
782+
case <-sc.doneServing:
783+
case <-sharedCh:
784+
close(privateCh)
785+
}
786+
}
787+
776788
// readPreface reads the ClientPreface greeting from the peer
777789
// or returns an error on timeout or an invalid greeting.
778790
func (sc *serverConn) readPreface() error {

0 commit comments

Comments
 (0)