Skip to content

Commit

Permalink
Rename variables in timer package (#4067)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshtin authored Mar 21, 2023
1 parent 411c65b commit 4033f21
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 119 deletions.
8 changes: 4 additions & 4 deletions common/timer/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import (
type (
// Gate interface
Gate interface {
// FireChan return the channel which will be fired when time is up
FireChan() <-chan struct{}
// FireCh return the channel which will be fired when time is up
FireCh() <-chan struct{}
// FireAfter check will the timer get fired after a certain time
FireAfter(now time.Time) bool
// Update update the timer gate, return true if update is a success
// success means timer is idle or timer is set with a sooner time to fire
// Update the timer gate, return true if update is a success.
// Success means timer is idle or timer is set with a sooner time to fire
Update(nextTime time.Time) bool
// Close shutdown the timer
Close()
Expand Down
60 changes: 30 additions & 30 deletions common/timer/local_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type (

LocalGateImpl struct {
// the channel which will be used to proxy the fired timer
fireChan chan struct{}
closeChan chan struct{}
fireCh chan struct{}
closeCh chan struct{}

timeSource clock.TimeSource

Expand All @@ -51,73 +51,73 @@ type (

// NewLocalGate create a new timer gate instance
func NewLocalGate(timeSource clock.TimeSource) LocalGate {
timer := &LocalGateImpl{
lg := &LocalGateImpl{
timer: time.NewTimer(0),
nextWakeupTime: time.Time{},
fireChan: make(chan struct{}, 1),
closeChan: make(chan struct{}),
fireCh: make(chan struct{}, 1),
closeCh: make(chan struct{}),
timeSource: timeSource,
}
// the timer should be stopped when initialized
if !timer.timer.Stop() {
if !lg.timer.Stop() {
// drain the existing signal if exist
<-timer.timer.C
<-lg.timer.C
}

go func() {
defer close(timer.fireChan)
defer timer.timer.Stop()
defer close(lg.fireCh)
defer lg.timer.Stop()
loop:
for {
select {
case <-timer.timer.C:
case <-lg.timer.C:
select {
// re-transmit on gateC
case timer.fireChan <- struct{}{}:
case lg.fireCh <- struct{}{}:
default:
}

case <-timer.closeChan:
case <-lg.closeCh:
// closed; cleanup and quit
break loop
}
}
}()

return timer
return lg
}

// FireChan return the channel which will be fired when time is up
func (timerGate *LocalGateImpl) FireChan() <-chan struct{} {
return timerGate.fireChan
// FireCh return the channel which will be fired when time is up
func (lg *LocalGateImpl) FireCh() <-chan struct{} {
return lg.fireCh
}

// FireAfter check will the timer get fired after a certain time
func (timerGate *LocalGateImpl) FireAfter(now time.Time) bool {
return timerGate.nextWakeupTime.After(now)
func (lg *LocalGateImpl) FireAfter(now time.Time) bool {
return lg.nextWakeupTime.After(now)
}

// Update update the timer gate, return true if update is a success
// success means timer is idle or timer is set with a sooner time to fire
func (timerGate *LocalGateImpl) Update(nextTime time.Time) bool {
// Update the timer gate, return true if update is a success.
// Success means timer is idle or timer is set with a sooner time to fire
func (lg *LocalGateImpl) Update(nextTime time.Time) bool {
// NOTE: negative duration will make the timer fire immediately
now := timerGate.timeSource.Now()
now := lg.timeSource.Now()

if timerGate.timer.Stop() && timerGate.nextWakeupTime.Before(nextTime) {
// this means the timer, before stopped, is active && next wake up time do not have to be updated
timerGate.timer.Reset(timerGate.nextWakeupTime.Sub(now))
if lg.timer.Stop() && lg.nextWakeupTime.Before(nextTime) {
// this means the timer, before stopped, is active && next wake-up time do not have to be updated
lg.timer.Reset(lg.nextWakeupTime.Sub(now))
return false
}

// this means the timer, before stopped, is active && next wake up time has to be updated
// this means the timer, before stopped, is active && next wake-up time has to be updated
// or this means the timer, before stopped, is already fired / never active
timerGate.nextWakeupTime = nextTime
timerGate.timer.Reset(nextTime.Sub(now))
lg.nextWakeupTime = nextTime
lg.timer.Reset(nextTime.Sub(now))
// Notifies caller that next notification is reset to fire at passed in 'next' visibility time
return true
}

// Close shutdown the timer
func (timerGate *LocalGateImpl) Close() {
close(timerGate.closeChan)
func (lg *LocalGateImpl) Close() {
close(lg.closeCh)
}
32 changes: 16 additions & 16 deletions common/timer/local_gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ type (
)

func BenchmarkLocalTimer(b *testing.B) {
timer := NewLocalGate(clock.NewRealTimeSource())
lg := NewLocalGate(clock.NewRealTimeSource())

for i := 0; i < b.N; i++ {
timer.Update(time.Now().UTC())
lg.Update(time.Now().UTC())
}
}

func TestLocalTimerGeteSuite(t *testing.T) {
func TestLocalTimerGateSuite(t *testing.T) {
s := new(localGateSuite)
suite.Run(t, s)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s *localGateSuite) TestTimerFire() {
s.localTimerGate.Update(newTimer)

select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -95,14 +95,14 @@ func (s *localGateSuite) TestTimerFireAfterUpdate_Active_Updated_BeforeNow() {

s.localTimerGate.Update(newTimer)
select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
s.Fail("timer should not fire when current time not updated")
case <-time.NewTimer(deadline.Sub(now)).C:
}

s.True(s.localTimerGate.Update(updatedNewTimer))
select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -117,7 +117,7 @@ func (s *localGateSuite) TestTimerFireAfterUpdate_Active_Updated() {
s.True(s.localTimerGate.Update(updatedNewTimer))

select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -132,7 +132,7 @@ func (s *localGateSuite) TestTimerFireAfterUpdate_Active_NotUpdated() {
s.False(s.localTimerGate.Update(updatedNewTimer))

select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -146,12 +146,12 @@ func (s *localGateSuite) TestTimerFireAfterUpdate_NotActive_Updated() {

s.localTimerGate.Update(newTimer)
// this is to drain existing signal
<-s.localTimerGate.FireChan()
<-s.localTimerGate.FireCh()
// test setup up complete

s.True(s.localTimerGate.Update(updatedNewTimer))
select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -165,12 +165,12 @@ func (s *localGateSuite) TestTimerFireAfterUpdate_NotActive_NotUpdated() {

s.localTimerGate.Update(newTimer)
// this is to drain existing signal
<-s.localTimerGate.FireChan()
<-s.localTimerGate.FireCh()
// test setup up complete

s.True(s.localTimerGate.Update(updatedNewTimer))
select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire before test deadline")
}
Expand All @@ -182,7 +182,7 @@ func (s *localGateSuite) TestTimerWillFire_Zero() {
s.False(s.localTimerGate.FireAfter(time.Now().UTC()))

select { // this is to drain existing signal
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(time.Second).C:
}

Expand All @@ -191,13 +191,13 @@ func (s *localGateSuite) TestTimerWillFire_Zero() {
deadline := now.Add(2 * time.Second)
s.localTimerGate.Update(newTimer)
select {
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(deadline.Sub(now)).C:
s.Fail("timer should fire")
}
s.localTimerGate.Update(time.Time{})
select { // this is to drain existing signal
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(time.Second).C:
s.Fail("timer should fire")
}
Expand All @@ -207,7 +207,7 @@ func (s *localGateSuite) TestTimerWillFire_Zero() {
s.localTimerGate.Update(newTimer)
s.localTimerGate.Update(time.Time{})
select { // this is to drain existing signal
case <-s.localTimerGate.FireChan():
case <-s.localTimerGate.FireCh():
case <-time.NewTimer(time.Second).C:
s.Fail("timer should fire")
}
Expand Down
Loading

0 comments on commit 4033f21

Please sign in to comment.