Skip to content

Commit

Permalink
Handle temporary network errors during connection acceptance
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
oiooj authored and Max committed Jan 27, 2025
1 parent 6f32efc commit 46cb716
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,25 @@ func ServiceConnections(listener net.Listener, p2id participant2ID, l Logger) (<
inMsgs := make(chan InMsg)

go func() {
var tempDelay time.Duration // how long to sleep on accept failure
for atomic.LoadUint32(&stopFlag) == 0 {
conn, err := listener.Accept()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
time.Sleep(tempDelay)
continue
}
return
}
tempDelay = 0

go handleConn(p2id, conn, inMsgs, &stopFlag, l)
}
Expand Down

0 comments on commit 46cb716

Please sign in to comment.