ServerConn: return immediately when calling Close()

This commit is contained in:
aler9
2020-12-19 17:33:42 +01:00
parent 9a602f77f7
commit fda8690465
2 changed files with 17 additions and 6 deletions

View File

@@ -36,5 +36,6 @@ func (s *Server) Accept() (*ServerConn, error) {
nconn: nconn,
br: bufio.NewReaderSize(conn, serverReadBufferSize),
bw: bufio.NewWriterSize(conn, serverWriteBufferSize),
terminate: make(chan struct{}),
}, nil
}

View File

@@ -38,11 +38,16 @@ type ServerConn struct {
nextFramesEnabled bool
framesEnabled bool
readTimeoutEnabled bool
// in
terminate chan struct{}
}
// Close closes all the connection resources.
func (sc *ServerConn) Close() error {
return sc.nconn.Close()
err := sc.nconn.Close()
close(sc.terminate)
return err
}
// NetConn returns the underlying net.Conn.
@@ -201,7 +206,12 @@ func (sc *ServerConn) backgroundRead(handlers ServerConnReadHandlers, done chan
// this was causing problems during unit tests.
if ua, ok := req.Header["User-Agent"]; ok && len(ua) == 1 &&
strings.HasPrefix(ua[0], "GStreamer") {
time.Sleep(1 * time.Second)
t := time.NewTimer(1 * time.Second)
defer t.Stop()
select {
case <-t.C:
case <-sc.terminate:
}
}
return handlers.OnSetup(req, th)