refactor: add idle watchgod to transport

This commit is contained in:
Jay
2026-05-20 08:10:02 -04:00
parent b8a2d47846
commit 09257e39b4
2 changed files with 186 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
package transport
import (
"context"
"time"
)
func IdleWatchdog(
ctx context.Context,
activity <-chan struct{},
timeout time.Duration,
onTimeout func(),
) {
// disable watchdog timeout if not configured
if timeout <= 0 {
for {
select {
case <-activity:
case <-ctx.Done():
return
}
}
}
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return
case <-activity:
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
timer.Reset(timeout)
case <-timer.C:
onTimeout()
return
}
}
}