mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-13 11:34:17 +00:00
naive flushing implemented with a throttler.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package common
|
||||
|
||||
import "time"
|
||||
|
||||
/* RepeatTimer */
|
||||
type RepeatTimer struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
dur time.Duration
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
func NewRepeatTimer(dur time.Duration) *RepeatTimer {
|
||||
var ch = make(chan struct{})
|
||||
var quit = make(chan struct{})
|
||||
var t = &RepeatTimer{Ch: ch, dur: dur, quit: quit}
|
||||
t.timer = time.AfterFunc(dur, t.fireHandler)
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *RepeatTimer) fireHandler() {
|
||||
select {
|
||||
case t.Ch <- struct{}{}:
|
||||
t.timer.Reset(t.dur)
|
||||
case <-t.quit:
|
||||
}
|
||||
}
|
||||
|
||||
func (t *RepeatTimer) Reset() {
|
||||
t.timer.Reset(t.dur)
|
||||
}
|
||||
|
||||
func (t *RepeatTimer) Stop() bool {
|
||||
close(t.quit)
|
||||
return t.timer.Stop()
|
||||
}
|
||||
Reference in New Issue
Block a user