mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-13 21:31:23 +00:00
No more blocking on multiple Stop()
This commit is contained in:
@ -16,8 +16,9 @@ type RepeatTimer struct {
|
|||||||
output chan<- time.Time
|
output chan<- time.Time
|
||||||
input chan repeatCommand
|
input chan repeatCommand
|
||||||
|
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
|
stopped bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type repeatCommand int32
|
type repeatCommand int32
|
||||||
@ -50,43 +51,42 @@ func (t *RepeatTimer) Reset() {
|
|||||||
// For ease of .Stop()'ing services before .Start()'ing them,
|
// For ease of .Stop()'ing services before .Start()'ing them,
|
||||||
// we ignore .Stop()'s on nil RepeatTimers.
|
// we ignore .Stop()'s on nil RepeatTimers.
|
||||||
func (t *RepeatTimer) Stop() bool {
|
func (t *RepeatTimer) Stop() bool {
|
||||||
if t == nil {
|
if t == nil || t.stopped {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
t.input <- RQuit
|
t.input <- RQuit
|
||||||
|
t.stopped = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *RepeatTimer) run() {
|
func (t *RepeatTimer) run() {
|
||||||
for {
|
done := false
|
||||||
fmt.Println("for")
|
for !done {
|
||||||
select {
|
select {
|
||||||
case cmd := <-t.input:
|
case cmd := <-t.input:
|
||||||
// stop goroutine if the input says so
|
// stop goroutine if the input says so
|
||||||
// don't close channels, as closed channels mess up select reads
|
// don't close channels, as closed channels mess up select reads
|
||||||
if t.processInput(cmd) {
|
done = t.processInput(cmd)
|
||||||
t.timer.Stop()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-t.timer.C:
|
case <-t.timer.C:
|
||||||
fmt.Println("tick")
|
|
||||||
// send if not blocked, then start the next tick
|
// send if not blocked, then start the next tick
|
||||||
// for blocking send, just
|
|
||||||
// t.output <- time.Now()
|
|
||||||
t.trySend()
|
t.trySend()
|
||||||
t.timer.Reset(t.dur)
|
t.timer.Reset(t.dur)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println("end run")
|
||||||
}
|
}
|
||||||
|
|
||||||
// trySend performs non-blocking send on t.Ch
|
// trySend performs non-blocking send on t.Ch
|
||||||
func (t *RepeatTimer) trySend() {
|
func (t *RepeatTimer) trySend() {
|
||||||
// TODO: this was blocking in previous version (t.Ch <- t_)
|
// TODO: this was blocking in previous version (t.Ch <- t_)
|
||||||
// should I use that behavior unstead of unblocking as per throttle?
|
// should I use that behavior unstead of unblocking as per throttle?
|
||||||
select {
|
|
||||||
case t.output <- time.Now():
|
// select {
|
||||||
default:
|
// case t.output <- time.Now():
|
||||||
}
|
// default:
|
||||||
|
// }
|
||||||
|
|
||||||
|
t.output <- time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
// all modifications of the internal state of ThrottleTimer
|
// all modifications of the internal state of ThrottleTimer
|
||||||
@ -98,6 +98,7 @@ func (t *RepeatTimer) processInput(cmd repeatCommand) (shutdown bool) {
|
|||||||
case Reset:
|
case Reset:
|
||||||
t.timer.Reset(t.dur)
|
t.timer.Reset(t.dur)
|
||||||
case RQuit:
|
case RQuit:
|
||||||
|
fmt.Println("got quit")
|
||||||
t.timer.Stop()
|
t.timer.Stop()
|
||||||
shutdown = true
|
shutdown = true
|
||||||
default:
|
default:
|
||||||
|
@ -73,6 +73,6 @@ func TestRepeat(test *testing.T) {
|
|||||||
time.Sleep(delay(7))
|
time.Sleep(delay(7))
|
||||||
assert.Equal(6, c.Count())
|
assert.Equal(6, c.Count())
|
||||||
|
|
||||||
// close channel to stop counter
|
// extra calls to stop don't block
|
||||||
t.Stop()
|
t.Stop()
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,9 @@ type ThrottleTimer struct {
|
|||||||
output chan<- struct{}
|
output chan<- struct{}
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
|
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
isSet bool
|
isSet bool
|
||||||
|
stopped bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type throttleCommand int32
|
type throttleCommand int32
|
||||||
@ -82,6 +83,7 @@ func (t *ThrottleTimer) processInput(cmd throttleCommand) (shutdown bool) {
|
|||||||
t.timer.Reset(t.dur)
|
t.timer.Reset(t.dur)
|
||||||
}
|
}
|
||||||
case TQuit:
|
case TQuit:
|
||||||
|
t.stopped = true
|
||||||
shutdown = true
|
shutdown = true
|
||||||
fallthrough
|
fallthrough
|
||||||
case Unset:
|
case Unset:
|
||||||
@ -119,7 +121,7 @@ func (t *ThrottleTimer) Unset() {
|
|||||||
// For ease of stopping services before starting them, we ignore Stop on nil
|
// For ease of stopping services before starting them, we ignore Stop on nil
|
||||||
// ThrottleTimers.
|
// ThrottleTimers.
|
||||||
func (t *ThrottleTimer) Stop() bool {
|
func (t *ThrottleTimer) Stop() bool {
|
||||||
if t == nil {
|
if t == nil || t.stopped {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
t.input <- TQuit
|
t.input <- TQuit
|
||||||
|
@ -95,4 +95,9 @@ func TestThrottle(test *testing.T) {
|
|||||||
|
|
||||||
stopped := t.Stop()
|
stopped := t.Stop()
|
||||||
assert.True(stopped)
|
assert.True(stopped)
|
||||||
|
time.Sleep(longwait)
|
||||||
|
assert.Equal(5, c.Count())
|
||||||
|
|
||||||
|
// extra calls to stop don't block
|
||||||
|
t.Stop()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user