mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 00:30:02 +00:00
Knut Ahlers
932e6907da
as `time.Timer` wasn't really suited for how it was used Signed-off-by: Knut Ahlers <knut@ahlers.me>
39 lines
778 B
Go
39 lines
778 B
Go
package twitch
|
|
|
|
import "time"
|
|
|
|
const keepaliveTrackerCheckInterval = 100 * time.Millisecond
|
|
|
|
type (
|
|
keepaliveTracker struct {
|
|
c chan<- struct{}
|
|
expires time.Time
|
|
renewed time.Time
|
|
}
|
|
)
|
|
|
|
func newKeepaliveTracker(timeout chan<- struct{}, d time.Duration) *keepaliveTracker {
|
|
t := &keepaliveTracker{
|
|
c: timeout,
|
|
expires: time.Now().Add(d),
|
|
}
|
|
|
|
go t.run()
|
|
|
|
return t
|
|
}
|
|
|
|
func (t keepaliveTracker) ExpiresAt() time.Time { return t.expires }
|
|
func (t keepaliveTracker) LastRenew() time.Time { return t.renewed }
|
|
|
|
func (t *keepaliveTracker) Renew(d time.Duration) {
|
|
t.expires = time.Now().Add(d)
|
|
t.renewed = time.Now()
|
|
}
|
|
|
|
func (t *keepaliveTracker) run() {
|
|
for t.expires.After(time.Now()) {
|
|
time.Sleep(keepaliveTrackerCheckInterval)
|
|
}
|
|
t.c <- struct{}{}
|
|
}
|