Terminate timeoutLoop when conn is closed

This commit is contained in:
Cecylia Bocovich 2022-10-19 10:05:18 -04:00
parent 5c23fcf14a
commit b010de5abb
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90

View file

@ -35,12 +35,14 @@ type webRTCConn struct {
inactivityTimeout time.Duration inactivityTimeout time.Duration
activity chan struct{} activity chan struct{}
closed chan struct{}
} }
func newWebRTCConn(pc *webrtc.PeerConnection, dc *webrtc.DataChannel, pr *io.PipeReader, eventLogger event.SnowflakeEventReceiver) *webRTCConn { func newWebRTCConn(pc *webrtc.PeerConnection, dc *webrtc.DataChannel, pr *io.PipeReader, eventLogger event.SnowflakeEventReceiver) *webRTCConn {
conn := &webRTCConn{pc: pc, dc: dc, pr: pr, eventLogger: eventLogger} conn := &webRTCConn{pc: pc, dc: dc, pr: pr, eventLogger: eventLogger}
conn.bytesLogger = newBytesSyncLogger() conn.bytesLogger = newBytesSyncLogger()
conn.activity = make(chan struct{}, 100) conn.activity = make(chan struct{}, 100)
conn.closed = make(chan struct{})
conn.inactivityTimeout = 30 * time.Second conn.inactivityTimeout = 30 * time.Second
go conn.timeoutLoop() go conn.timeoutLoop()
return conn return conn
@ -55,9 +57,10 @@ func (c *webRTCConn) timeoutLoop() {
return return
case <-c.activity: case <-c.activity:
continue continue
case <-c.closed:
return
} }
} }
} }
func (c *webRTCConn) Read(b []byte) (int, error) { func (c *webRTCConn) Read(b []byte) (int, error) {
@ -77,6 +80,7 @@ func (c *webRTCConn) Write(b []byte) (int, error) {
func (c *webRTCConn) Close() (err error) { func (c *webRTCConn) Close() (err error) {
c.once.Do(func() { c.once.Do(func() {
close(c.closed)
err = c.pc.Close() err = c.pc.Close()
}) })
return return