Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/net/http/http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,17 @@ func (s *Server) setHTTP2Config(conf http2ExternalServerConfig) {
panic("http: HTTP/2 Server already registered")
}
s.h2Config = conf
s.h2Config.ServeConnFunc(s.serveHTTP2Conn)
s.h2Config.ServeConnFunc(func(ctx context.Context, nc net.Conn, h Handler, sawClientPreface bool, upgradeReq *Request, settings []byte) {
s.serveHTTP2Conn(ctx, nc, h, sawClientPreface, upgradeReq, settings, nil)
})
s.configureHTTP2()
}

func (s *Server) serveHTTP2Conn(ctx context.Context, nc net.Conn, h Handler, sawClientPreface bool, upgradeReq *Request, settings []byte) {
// serveHTTP2Conn serves nc with the HTTP/2 server. It may return before the
// connection is done being served (an idle HTTP/2 connection doesn't hold
// onto a goroutine); onClose, if non-nil, runs once the connection is done
// and has been closed.
func (s *Server) serveHTTP2Conn(ctx context.Context, nc net.Conn, h Handler, sawClientPreface bool, upgradeReq *Request, settings []byte, onClose func()) {
s.setupHTTP2_ServeTLS()
var serverUpgradeReq *http2.ServerRequest
if upgradeReq != nil {
Expand All @@ -106,6 +112,7 @@ func (s *Server) serveHTTP2Conn(ctx context.Context, nc net.Conn, h Handler, saw
SawClientPreface: sawClientPreface,
UpgradeRequest: serverUpgradeReq,
Settings: settings,
OnClose: onClose,
})
}

Expand Down
10 changes: 10 additions & 0 deletions src/net/http/internal/http2/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (sc *serverConn) TestFlowControlConsumed() (consumed int32) {

func (sc *serverConn) TestStreamExists(id uint32) bool {
ch := make(chan bool, 1)
sc.beginServeSend()
sc.serveMsgCh <- func(int) {
ch <- (sc.streams[id] != nil)
}
Expand All @@ -90,13 +91,22 @@ func (sc *serverConn) TestStreamExists(id uint32) bool {

func (sc *serverConn) TestStreamState(id uint32) streamState {
ch := make(chan streamState, 1)
sc.beginServeSend()
sc.serveMsgCh <- func(int) {
state, _ := sc.state(id)
ch <- state
}
return <-ch
}

// TestServeParked reports whether the connection's serve goroutine has
// exited because the connection is idle. See serverConn.serveLoop.
func (sc *serverConn) TestServeParked() bool {
sc.parkMu.Lock()
defer sc.parkMu.Unlock()
return sc.parked
}

func (sc *serverConn) StartGracefulShutdown() { sc.startGracefulShutdown() }

func (sc *serverConn) TestHPACKEncoder() *hpack.Encoder {
Expand Down
27 changes: 27 additions & 0 deletions src/net/http/internal/http2/gotrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ func (g goroutineLock) checkNotOn() {
}
}

// A goroutineLocker is a goroutineLock whose owning goroutine can be
// atomically replaced, for values whose owning goroutine changes over
// their lifetime (such as the server's serve loop, which parks when
// the connection is idle and resumes on a new goroutine).
type goroutineLocker struct {
v atomic.Uint64
}

// setOwner records the current goroutine as the owner.
func (g *goroutineLocker) setOwner() {
g.v.Store(uint64(newGoroutineLock()))
}

func (g *goroutineLocker) check() {
if !DebugGoroutines {
return
}
goroutineLock(g.v.Load()).check()
}

func (g *goroutineLocker) checkNotOn() {
if !DebugGoroutines {
return
}
goroutineLock(g.v.Load()).checkNotOn()
}

var goroutineSpace = []byte("goroutine ")

func curGoroutineID() uint64 {
Expand Down
Loading
Loading