Initial version of migration to new event model

- Begin move away from global timer state.
- Made logging format more consistent
This commit is contained in:
Mathias Hall-Andersen
2018-05-05 02:20:52 +02:00
parent 168ef61a63
commit 6db41d5a26
8 changed files with 203 additions and 183 deletions

44
event.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"sync/atomic"
"time"
)
type Event struct {
guard int32
next time.Time
interval time.Duration
C chan struct{}
}
func newEvent(interval time.Duration) *Event {
return &Event{
guard: 0,
next: time.Now(),
interval: interval,
C: make(chan struct{}, 1),
}
}
func (e *Event) Clear() {
select {
case <-e.C:
default:
}
}
func (e *Event) Fire() {
if e == nil || atomic.SwapInt32(&e.guard, 1) != 0 {
return
}
now := time.Now()
if e.next.After(now) {
select {
case e.C <- struct{}{}:
default:
}
e.next = now.Add(e.interval)
}
atomic.StoreInt32(&e.guard, 0)
}