1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-16 20:15:40 -05:00
mumble-discord-bridge/sleepct.go
2021-04-18 00:30:27 -04:00

42 lines
617 B
Go

package main
import (
"fmt"
"sync"
"time"
)
// SleepCT - Sleep constant time step crates a sleep based ticker
// designed maintain a sleep/tick interval
type SleepCT struct {
sync.Mutex
d time.Duration // duration
t time.Time // last time target
}
func (s *SleepCT) SleepNextTarget() {
s.Lock()
now := time.Now()
var last time.Time
if s.t.IsZero() {
fmt.Println("SleepCT reset")
last = now.Add(-s.d)
} else {
last = s.t
}
// Next Target
s.t = last.Add(s.d)
d := s.t.Sub(now)
time.Sleep(d)
// delta := now.Sub(s.t)
// fmt.Println("delta", delta, d, time.Since(s.t))
s.Unlock()
}