mirror of
https://github.com/hackdiaz-dev/free-turn-proxy.git
synced 2026-10-12 09:41:53 +02:00
31 lines
569 B
Go
31 lines
569 B
Go
package wake
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
func Watch(ctx context.Context, tick, threshold time.Duration, onGap func(time.Duration)) {
|
|
t := time.NewTicker(tick)
|
|
defer t.Stop()
|
|
|
|
last := time.Now()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C:
|
|
now := time.Now()
|
|
// Round(0) убирает монотонные часы: их разница с wall clock отражает сон.
|
|
gap := now.Round(0).Sub(last.Round(0)) - now.Sub(last)
|
|
last = now
|
|
if gap < threshold {
|
|
continue
|
|
}
|
|
if onGap != nil {
|
|
onGap(gap)
|
|
}
|
|
}
|
|
}
|
|
}
|