2015-10-21 12:15:19 -07:00
|
|
|
package common
|
|
|
|
|
2017-03-05 03:24:25 -05:00
|
|
|
import (
|
2017-12-07 10:15:38 +01:00
|
|
|
"fmt"
|
2017-03-05 03:24:25 -05:00
|
|
|
"time"
|
|
|
|
)
|
2015-10-21 12:15:19 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
RepeatTimer repeatedly sends a struct{}{} to .Ch after each "dur" period.
|
|
|
|
It's good for keeping connections alive.
|
|
|
|
A RepeatTimer must be Stop()'d or it will keep a goroutine alive.
|
|
|
|
*/
|
|
|
|
type RepeatTimer struct {
|
2017-12-07 10:15:38 +01:00
|
|
|
Name string
|
|
|
|
Ch <-chan time.Time
|
|
|
|
output chan<- time.Time
|
|
|
|
input chan repeatCommand
|
2015-10-21 12:15:19 -07:00
|
|
|
|
2017-12-07 10:15:38 +01:00
|
|
|
dur time.Duration
|
|
|
|
timer *time.Timer
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
2017-12-07 10:15:38 +01:00
|
|
|
type repeatCommand int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
Reset repeatCommand = iota
|
|
|
|
RQuit
|
|
|
|
)
|
|
|
|
|
2015-10-21 12:15:19 -07:00
|
|
|
func NewRepeatTimer(name string, dur time.Duration) *RepeatTimer {
|
2017-12-07 10:15:38 +01:00
|
|
|
c := make(chan time.Time)
|
2015-10-21 12:15:19 -07:00
|
|
|
var t = &RepeatTimer{
|
2017-12-07 10:15:38 +01:00
|
|
|
Name: name,
|
|
|
|
Ch: c,
|
|
|
|
output: c,
|
|
|
|
input: make(chan repeatCommand),
|
2015-10-21 12:15:19 -07:00
|
|
|
|
2017-12-07 10:15:38 +01:00
|
|
|
timer: time.NewTimer(dur),
|
|
|
|
dur: dur,
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
2017-12-07 10:15:38 +01:00
|
|
|
go t.run()
|
|
|
|
return t
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait the duration again before firing.
|
|
|
|
func (t *RepeatTimer) Reset() {
|
2017-12-07 10:15:38 +01:00
|
|
|
t.input <- Reset
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// For ease of .Stop()'ing services before .Start()'ing them,
|
|
|
|
// we ignore .Stop()'s on nil RepeatTimers.
|
|
|
|
func (t *RepeatTimer) Stop() bool {
|
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-12-07 10:15:38 +01:00
|
|
|
t.input <- RQuit
|
|
|
|
return true
|
|
|
|
}
|
2015-10-21 12:15:19 -07:00
|
|
|
|
2017-12-07 10:15:38 +01:00
|
|
|
func (t *RepeatTimer) run() {
|
|
|
|
for {
|
|
|
|
fmt.Println("for")
|
2017-03-05 03:24:25 -05:00
|
|
|
select {
|
2017-12-07 10:15:38 +01:00
|
|
|
case cmd := <-t.input:
|
|
|
|
// stop goroutine if the input says so
|
|
|
|
// don't close channels, as closed channels mess up select reads
|
|
|
|
if t.processInput(cmd) {
|
|
|
|
t.timer.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-t.timer.C:
|
|
|
|
fmt.Println("tick")
|
|
|
|
// send if not blocked, then start the next tick
|
|
|
|
// for blocking send, just
|
|
|
|
// t.output <- time.Now()
|
|
|
|
t.trySend()
|
|
|
|
t.timer.Reset(t.dur)
|
2017-03-05 03:24:25 -05:00
|
|
|
}
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|
2017-12-07 10:15:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// trySend performs non-blocking send on t.Ch
|
|
|
|
func (t *RepeatTimer) trySend() {
|
|
|
|
// TODO: this was blocking in previous version (t.Ch <- t_)
|
|
|
|
// should I use that behavior unstead of unblocking as per throttle?
|
|
|
|
select {
|
|
|
|
case t.output <- time.Now():
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all modifications of the internal state of ThrottleTimer
|
|
|
|
// happen in this method. It is only called from the run goroutine
|
|
|
|
// so we avoid any race conditions
|
|
|
|
func (t *RepeatTimer) processInput(cmd repeatCommand) (shutdown bool) {
|
|
|
|
fmt.Printf("process: %d\n", cmd)
|
|
|
|
switch cmd {
|
|
|
|
case Reset:
|
|
|
|
t.timer.Reset(t.dur)
|
|
|
|
case RQuit:
|
|
|
|
t.timer.Stop()
|
|
|
|
shutdown = true
|
|
|
|
default:
|
|
|
|
panic("unknown command!")
|
|
|
|
}
|
|
|
|
return shutdown
|
2015-10-21 12:15:19 -07:00
|
|
|
}
|