1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-22 21:35:44 -05:00
mumble-discord-bridge/pkg/sleepct/sleepct.go
Tyler Stiene 6658c8534c add prometheus metrics
add grafana and promethus docker-container
add example grafana dashboard
sleepct now returns delta time from target
2021-08-23 00:00:39 -04:00

53 lines
805 B
Go

package sleepct
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) Start(d time.Duration) {
if s.t.IsZero() {
s.d = d
s.t = time.Now()
} else {
panic("SleepCT already started")
}
}
func (s *SleepCT) SleepNextTarget() int64 {
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()
return now.Sub(s.t).Microseconds()
}