mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-04 15:17:02 +00:00
naive flushing implemented with a throttler.
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
/* Debouncer */
|
||||
type Debouncer struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
dur time.Duration
|
||||
mtx sync.Mutex
|
||||
timer *time.Timer
|
||||
}
|
||||
|
||||
func NewDebouncer(dur time.Duration) *Debouncer {
|
||||
var timer *time.Timer
|
||||
var ch = make(chan struct{})
|
||||
var quit = make(chan struct{})
|
||||
var mtx sync.Mutex
|
||||
fire := func() {
|
||||
go func() {
|
||||
select {
|
||||
case ch <- struct{}{}:
|
||||
case <-quit:
|
||||
}
|
||||
}()
|
||||
mtx.Lock()
|
||||
defer mtx.Unlock()
|
||||
timer.Reset(dur)
|
||||
}
|
||||
timer = time.AfterFunc(dur, fire)
|
||||
return &Debouncer{Ch: ch, dur: dur, quit: quit, mtx: mtx, timer: timer}
|
||||
}
|
||||
|
||||
func (d *Debouncer) Reset() {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
d.timer.Reset(d.dur)
|
||||
}
|
||||
|
||||
func (d *Debouncer) Stop() bool {
|
||||
d.mtx.Lock()
|
||||
defer d.mtx.Unlock()
|
||||
close(d.quit)
|
||||
return d.timer.Stop()
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
/* Throttler */
|
||||
type Throttler struct {
|
||||
Ch chan struct{}
|
||||
quit chan struct{}
|
||||
dur time.Duration
|
||||
timer *time.Timer
|
||||
isSet uint32
|
||||
}
|
||||
|
||||
func NewThrottler(dur time.Duration) *Throttler {
|
||||
var ch = make(chan struct{})
|
||||
var quit = make(chan struct{})
|
||||
var t = &Throttler{Ch: ch, dur: dur, quit: quit}
|
||||
t.timer = time.AfterFunc(dur, t.fireHandler)
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *Throttler) fireHandler() {
|
||||
select {
|
||||
case t.Ch <- struct{}{}:
|
||||
atomic.StoreUint32(&t.isSet, 0)
|
||||
case <-t.quit:
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Throttler) Set() {
|
||||
if atomic.CompareAndSwapUint32(&t.isSet, 0, 1) {
|
||||
t.timer.Reset(t.dur)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Throttler) Stop() bool {
|
||||
close(t.quit)
|
||||
return t.timer.Stop()
|
||||
}
|
||||
Reference in New Issue
Block a user