Refactor checkForStaleness to take time.Duration

This commit is contained in:
Cecylia Bocovich 2021-06-24 11:20:44 -04:00
parent e3351cb08a
commit 10b6075eaa
2 changed files with 6 additions and 6 deletions

View file

@ -269,8 +269,8 @@ func TestWebRTCPeer(t *testing.T) {
Convey("WebRTCPeer", t, func(c C) { Convey("WebRTCPeer", t, func(c C) {
p := &WebRTCPeer{closed: make(chan struct{})} p := &WebRTCPeer{closed: make(chan struct{})}
Convey("checks for staleness", func() { Convey("checks for staleness", func() {
go p.checkForStaleness() go p.checkForStaleness(time.Second)
<-time.After(2 * SnowflakeTimeout) <-time.After(2 * time.Second)
So(p.Closed(), ShouldEqual, true) So(p.Closed(), ShouldEqual, true)
}) })
}) })

View file

@ -101,7 +101,7 @@ func (c *WebRTCPeer) Close() error {
// Prevent long-lived broken remotes. // Prevent long-lived broken remotes.
// Should also update the DataChannel in underlying go-webrtc's to make Closes // Should also update the DataChannel in underlying go-webrtc's to make Closes
// more immediate / responsive. // more immediate / responsive.
func (c *WebRTCPeer) checkForStaleness() { func (c *WebRTCPeer) checkForStaleness(timeout time.Duration) {
c.mu.Lock() c.mu.Lock()
c.lastReceive = time.Now() c.lastReceive = time.Now()
c.mu.Unlock() c.mu.Unlock()
@ -109,9 +109,9 @@ func (c *WebRTCPeer) checkForStaleness() {
c.mu.Lock() c.mu.Lock()
lastReceive := c.lastReceive lastReceive := c.lastReceive
c.mu.Unlock() c.mu.Unlock()
if time.Since(lastReceive) > SnowflakeTimeout { if time.Since(lastReceive) > timeout {
log.Printf("WebRTC: No messages received for %v -- closing stale connection.", log.Printf("WebRTC: No messages received for %v -- closing stale connection.",
SnowflakeTimeout) timeout)
c.Close() c.Close()
return return
} }
@ -147,7 +147,7 @@ func (c *WebRTCPeer) connect(config *webrtc.Configuration, broker *BrokerChannel
return errors.New("timeout waiting for DataChannel.OnOpen") return errors.New("timeout waiting for DataChannel.OnOpen")
} }
go c.checkForStaleness() go c.checkForStaleness(SnowflakeTimeout)
return nil return nil
} }