Skip to content

Commit

Permalink
transport: derive transport context from context.Background (#2930)
Browse files Browse the repository at this point in the history
  • Loading branch information
JNProtzman authored and dfawley committed Aug 29, 2019
1 parent d5a36f0 commit 4d39b48
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions internal/transport/http2_server.go
Expand Up @@ -65,8 +65,7 @@ var (
// http2Server implements the ServerTransport interface with HTTP2.
type http2Server struct {
ctx context.Context
ctxDone <-chan struct{} // Cache the context.Done() chan
cancel context.CancelFunc
done chan struct{}
conn net.Conn
loopy *loopyWriter
readerDone chan struct{} // sync point to enable testing.
Expand Down Expand Up @@ -203,11 +202,10 @@ func newHTTP2Server(conn net.Conn, config *ServerConfig) (_ ServerTransport, err
if kep.MinTime == 0 {
kep.MinTime = defaultKeepalivePolicyMinTime
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
t := &http2Server{
ctx: ctx,
cancel: cancel,
ctxDone: ctx.Done(),
ctx: context.Background(),
done: done,
conn: conn,
remoteAddr: conn.RemoteAddr(),
localAddr: conn.LocalAddr(),
Expand All @@ -228,7 +226,7 @@ func newHTTP2Server(conn net.Conn, config *ServerConfig) (_ ServerTransport, err
czData: new(channelzData),
bufferPool: newBufferPool(),
}
t.controlBuf = newControlBuffer(t.ctxDone)
t.controlBuf = newControlBuffer(t.done)
if dynamicWindow {
t.bdpEst = &bdpEstimator{
bdp: initialWindowSize,
Expand Down Expand Up @@ -886,7 +884,7 @@ func (t *http2Server) Write(s *Stream, hdr []byte, data []byte, opts *Options) e
// TODO(mmukhi, dfawley): Should the server write also return io.EOF?
s.cancel()
select {
case <-t.ctx.Done():
case <-t.done:
return ErrConnClosing
default:
}
Expand All @@ -908,7 +906,7 @@ func (t *http2Server) Write(s *Stream, hdr []byte, data []byte, opts *Options) e
}
if err := s.wq.get(int32(len(hdr) + len(data))); err != nil {
select {
case <-t.ctx.Done():
case <-t.done:
return ErrConnClosing
default:
}
Expand Down Expand Up @@ -974,7 +972,7 @@ func (t *http2Server) keepalive() {
t.Close()
// Resetting the timer so that the clean-up doesn't deadlock.
maxAge.Reset(infinity)
case <-t.ctx.Done():
case <-t.done:
}
return
case <-keepalive.C:
Expand All @@ -996,7 +994,7 @@ func (t *http2Server) keepalive() {
}
t.controlBuf.put(p)
keepalive.Reset(t.kp.Timeout)
case <-t.ctx.Done():
case <-t.done:
return
}
}
Expand All @@ -1016,7 +1014,7 @@ func (t *http2Server) Close() error {
t.activeStreams = nil
t.mu.Unlock()
t.controlBuf.finish()
t.cancel()
close(t.done)
err := t.conn.Close()
if channelz.IsOn() {
channelz.RemoveEntry(t.channelzID)
Expand Down Expand Up @@ -1156,7 +1154,7 @@ func (t *http2Server) outgoingGoAwayHandler(g *goAway) (bool, error) {
select {
case <-t.drainChan:
case <-timer.C:
case <-t.ctx.Done():
case <-t.done:
return
}
t.controlBuf.put(&goAway{code: g.code, debugData: g.debugData})
Expand Down Expand Up @@ -1206,7 +1204,7 @@ func (t *http2Server) getOutFlowWindow() int64 {
select {
case sz := <-resp:
return int64(sz)
case <-t.ctxDone:
case <-t.done:
return -1
case <-timer.C:
return -2
Expand Down

0 comments on commit 4d39b48

Please sign in to comment.