Skip to content

Commit

Permalink
Add read deadline to hbConn.recvLoop() to prevent blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyech committed Feb 13, 2024
1 parent 0f92b17 commit 0b6aa3c
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/dtls/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *hbConn) recvLoop() {
for {
buffer := make([]byte, c.maxMessageSize)

c.stream.SetReadDeadline(time.Now().Add(c.timeout))

Check failure on line 78 in pkg/dtls/heartbeat.go

View workflow job for this annotation

GitHub Actions / Format and Lint with golangci-lint

Error return value of `c.stream.SetReadDeadline` is not checked (errcheck)
n, err := c.stream.Read(buffer)

if bytes.Equal(c.hb, buffer[:n]) {
Expand All @@ -87,7 +88,15 @@ func (c *hbConn) recvLoop() {
return
}

c.recvCh <- errBytes{buffer[:n], err}
timer := time.NewTimer(c.timeout)
select {
case c.recvCh <- errBytes{buffer[:n], err}:
timer.Stop()
continue
case <-timer.C:
c.Close()
return
}
}

}
Expand Down

0 comments on commit 0b6aa3c

Please sign in to comment.