From 4033f210525a045bf8391fef896f54a71c44a4c8 Mon Sep 17 00:00:00 2001 From: Alex Shtin Date: Tue, 21 Mar 2023 13:05:45 -0700 Subject: [PATCH] Rename variables in timer package (#4067) --- common/timer/gate.go | 8 +- common/timer/local_gate.go | 60 ++++++------- common/timer/local_gate_test.go | 32 +++---- common/timer/remote_gate.go | 84 +++++++++---------- common/timer/remote_gate_test.go | 44 +++++----- service/history/queues/queue_scheduled.go | 2 +- .../history/queues/queue_scheduled_test.go | 6 +- service/history/queues/rescheduler.go | 2 +- 8 files changed, 119 insertions(+), 119 deletions(-) diff --git a/common/timer/gate.go b/common/timer/gate.go index a259fac46e0..f4c9017ccae 100644 --- a/common/timer/gate.go +++ b/common/timer/gate.go @@ -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() diff --git a/common/timer/local_gate.go b/common/timer/local_gate.go index 1f67280d459..4717b28f72b 100644 --- a/common/timer/local_gate.go +++ b/common/timer/local_gate.go @@ -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 @@ -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) } diff --git a/common/timer/local_gate_test.go b/common/timer/local_gate_test.go index 8ef05fb67ec..ac968670b1f 100644 --- a/common/timer/local_gate_test.go +++ b/common/timer/local_gate_test.go @@ -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) } @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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") } @@ -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: } @@ -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") } @@ -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") } diff --git a/common/timer/remote_gate.go b/common/timer/remote_gate.go index 6b180485635..aa6bf1d3712 100644 --- a/common/timer/remote_gate.go +++ b/common/timer/remote_gate.go @@ -40,7 +40,7 @@ type ( RemoteGateImpl struct { // the channel which will be used to proxy the fired timer - fireChan chan struct{} + fireCh chan struct{} // lock for timer and next wakeup time sync.Mutex @@ -53,100 +53,100 @@ type ( // NewRemoteGate create a new timer gate instance func NewRemoteGate() RemoteGate { - timer := &RemoteGateImpl{ + rg := &RemoteGateImpl{ currentTime: time.Time{}, nextWakeupTime: time.Time{}, - fireChan: make(chan struct{}, 1), + fireCh: make(chan struct{}, 1), } - return timer + return rg } -// FireChan return the channel which will be fired when time is up -func (timerGate *RemoteGateImpl) FireChan() <-chan struct{} { - return timerGate.fireChan +// FireCh return the channel which will be fired when time is up +func (rg *RemoteGateImpl) FireCh() <-chan struct{} { + return rg.fireCh } // FireAfter check will the timer get fired after a certain time -func (timerGate *RemoteGateImpl) FireAfter(now time.Time) bool { - timerGate.Lock() - defer timerGate.Unlock() +func (rg *RemoteGateImpl) FireAfter(now time.Time) bool { + rg.Lock() + defer rg.Unlock() - active := timerGate.currentTime.Before(timerGate.nextWakeupTime) - return active && timerGate.nextWakeupTime.After(now) + active := rg.currentTime.Before(rg.nextWakeupTime) + return active && rg.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 *RemoteGateImpl) Update(nextTime time.Time) bool { - timerGate.Lock() - defer timerGate.Unlock() +// 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 (rg *RemoteGateImpl) Update(nextTime time.Time) bool { + rg.Lock() + defer rg.Unlock() - active := timerGate.currentTime.Before(timerGate.nextWakeupTime) + active := rg.currentTime.Before(rg.nextWakeupTime) if active { - if timerGate.nextWakeupTime.Before(nextTime) { + if rg.nextWakeupTime.Before(nextTime) { // current time < next wake up time < next time return false } - if timerGate.currentTime.Before(nextTime) { - // current time < next time <= next wake up time - timerGate.nextWakeupTime = nextTime + if rg.currentTime.Before(nextTime) { + // current time < next time <= next wake-up time + rg.nextWakeupTime = nextTime return true } - // next time <= current time < next wake up time - timerGate.nextWakeupTime = nextTime - timerGate.fire() + // next time <= current time < next wake-up time + rg.nextWakeupTime = nextTime + rg.fire() return true } // this means the timer, before stopped, has already fired / never active - if !timerGate.currentTime.Before(nextTime) { + if !rg.currentTime.Before(nextTime) { // next time is <= current time, need to fire immediately - // whether to update next wake up time or not is irrelevent - timerGate.fire() + // whether to update next wake-up time or not is irrelevant + rg.fire() } else { // next time > current time - timerGate.nextWakeupTime = nextTime + rg.nextWakeupTime = nextTime } return true } // Close shutdown the timer -func (timerGate *RemoteGateImpl) Close() { +func (rg *RemoteGateImpl) Close() { // no op } // SetCurrentTime set the current time, and additionally fire the fire chan -// if new "current" time is after the next wake up time, return true if +// if new "current" time is after the next wake-up time, return true if // "current" is actually updated -func (timerGate *RemoteGateImpl) SetCurrentTime(currentTime time.Time) bool { - timerGate.Lock() - defer timerGate.Unlock() +func (rg *RemoteGateImpl) SetCurrentTime(currentTime time.Time) bool { + rg.Lock() + defer rg.Unlock() - if !timerGate.currentTime.Before(currentTime) { + if !rg.currentTime.Before(currentTime) { // new current time is <= current time return false } // NOTE: do not update the current time now - if !timerGate.currentTime.Before(timerGate.nextWakeupTime) { + if !rg.currentTime.Before(rg.nextWakeupTime) { // current time already >= next wakeup time // avoid duplicate fire - timerGate.currentTime = currentTime + rg.currentTime = currentTime return true } - timerGate.currentTime = currentTime - if !timerGate.currentTime.Before(timerGate.nextWakeupTime) { - timerGate.fire() + rg.currentTime = currentTime + if !rg.currentTime.Before(rg.nextWakeupTime) { + rg.fire() } return true } -func (timerGate *RemoteGateImpl) fire() { +func (rg *RemoteGateImpl) fire() { select { - case timerGate.fireChan <- struct{}{}: + case rg.fireCh <- struct{}{}: // timer successfully triggered default: // timer already triggered, pass diff --git a/common/timer/remote_gate_test.go b/common/timer/remote_gate_test.go index ce4877c3e7d..3e3b3f0492a 100644 --- a/common/timer/remote_gate_test.go +++ b/common/timer/remote_gate_test.go @@ -42,7 +42,7 @@ type ( } ) -func TestRemoteTimerGeteSuite(t *testing.T) { +func TestRemoteTimerGateSuite(t *testing.T) { s := new(remoteGateSuite) suite.Run(t, s) } @@ -74,14 +74,14 @@ func (s *remoteGateSuite) TestTimerFire() { s.remoteTimerGate.Update(newTimer) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire when current time not updated") case <-time.NewTimer(deadline.Sub(now)).C: } s.remoteTimerGate.SetCurrentTime(deadline) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -95,14 +95,14 @@ func (s *remoteGateSuite) TestTimerFireAfterUpdate_Active_Updated_BeforeNow() { s.remoteTimerGate.Update(newTimer) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire when current time not updated") case <-time.NewTimer(deadline.Sub(now)).C: } s.True(s.remoteTimerGate.Update(updatedNewTimer)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -117,14 +117,14 @@ func (s *remoteGateSuite) TestTimerFireAfterUpdate_Active_Updated() { s.True(s.remoteTimerGate.Update(updatedNewTimer)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire when current time not updated") case <-time.NewTimer(deadline.Sub(now)).C: } s.remoteTimerGate.SetCurrentTime(updatedNewTimer) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -139,14 +139,14 @@ func (s *remoteGateSuite) TestTimerFireAfterUpdate_Active_NotUpdated() { s.False(s.remoteTimerGate.Update(updatedNewTimer)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire when current time not updated") case <-time.NewTimer(deadline.Sub(now)).C: } s.remoteTimerGate.SetCurrentTime(updatedNewTimer) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -160,19 +160,19 @@ func (s *remoteGateSuite) TestTimerFireAfterUpdate_NotActive_Updated() { s.remoteTimerGate.Update(newTimer) // this is to drain existing signal - <-s.remoteTimerGate.FireChan() + <-s.remoteTimerGate.FireCh() // test setup up complete s.True(s.remoteTimerGate.Update(updatedNewTimer)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire when current time not updated") case <-time.NewTimer(deadline.Sub(now)).C: } s.remoteTimerGate.SetCurrentTime(updatedNewTimer) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -185,12 +185,12 @@ func (s *remoteGateSuite) TestTimerFireAfterUpdate_NotActive_NotUpdated() { s.remoteTimerGate.Update(newTimer) // this is to drain existing signal - <-s.remoteTimerGate.FireChan() + <-s.remoteTimerGate.FireCh() // test setup up complete s.True(s.remoteTimerGate.Update(updatedNewTimer)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire when new timer is in the past") } @@ -201,7 +201,7 @@ func (s *remoteGateSuite) TestTimerSetCurrentTime_NoUpdate() { newCurrentTime := now.Add(-1 * time.Second) s.False(s.remoteTimerGate.SetCurrentTime(newCurrentTime)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire") default: } @@ -214,12 +214,12 @@ func (s *remoteGateSuite) TestTimerSetCurrentTime_Update_TimerAlreadyFired() { s.remoteTimerGate.Update(newTimer) // this is to drain existing signal - <-s.remoteTimerGate.FireChan() + <-s.remoteTimerGate.FireCh() // test setup up complete s.True(s.remoteTimerGate.SetCurrentTime(newCurrentTime)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire") default: } @@ -233,7 +233,7 @@ func (s *remoteGateSuite) TestTimerSetCurrentTime_Update_TimerNotFired() { s.remoteTimerGate.Update(newTimer) s.True(s.remoteTimerGate.SetCurrentTime(newCurrentTime)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire") default: } @@ -247,7 +247,7 @@ func (s *remoteGateSuite) TestTimerSetCurrentTime_Update_TimerFired() { s.remoteTimerGate.Update(newTimer) s.True(s.remoteTimerGate.SetCurrentTime(newCurrentTime)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): default: s.Fail("timer should fire") } @@ -256,7 +256,7 @@ func (s *remoteGateSuite) TestTimerSetCurrentTime_Update_TimerFired() { newCurrentTime = newCurrentTime.Add(1 * time.Second) s.True(s.remoteTimerGate.SetCurrentTime(newCurrentTime)) select { - case <-s.remoteTimerGate.FireChan(): + case <-s.remoteTimerGate.FireCh(): s.Fail("timer should not fire") default: } @@ -272,10 +272,10 @@ func (s *remoteGateSuite) TestTimerWillFire_Active() { now := s.currentTime newTimer := now.Add(2 * time.Second) timeBeforeNewTimer := now.Add(1 * time.Second) - timeAfterNewimer := now.Add(3 * time.Second) + timeAfterNewTimer := now.Add(3 * time.Second) s.remoteTimerGate.Update(newTimer) s.True(s.remoteTimerGate.FireAfter(timeBeforeNewTimer)) - s.False(s.remoteTimerGate.FireAfter(timeAfterNewimer)) + s.False(s.remoteTimerGate.FireAfter(timeAfterNewTimer)) } func (s *remoteGateSuite) TestTimerWillFire_NotActive() { diff --git a/service/history/queues/queue_scheduled.go b/service/history/queues/queue_scheduled.go index 6f1f410e03c..803643486af 100644 --- a/service/history/queues/queue_scheduled.go +++ b/service/history/queues/queue_scheduled.go @@ -214,7 +214,7 @@ func (p *scheduledQueue) processEventLoop() { p.processNewTime() case <-p.lookAheadCh: p.lookAheadTask() - case <-p.timerGate.FireChan(): + case <-p.timerGate.FireCh(): p.processNewRange() case <-p.checkpointTimer.C: p.checkpoint() diff --git a/service/history/queues/queue_scheduled_test.go b/service/history/queues/queue_scheduled_test.go index deead18c3f4..5bef2f5fd2a 100644 --- a/service/history/queues/queue_scheduled_test.go +++ b/service/history/queues/queue_scheduled_test.go @@ -203,7 +203,7 @@ func (s *scheduledQueueSuite) TestLookAheadTask_HasLookAheadTask() { timerGate.SetCurrentTime(lookAheadTask.GetKey().FireTime) select { - case <-s.scheduledQueue.timerGate.FireChan(): + case <-s.scheduledQueue.timerGate.FireCh(): default: s.Fail("timer gate should fire when look ahead task is due") } @@ -220,7 +220,7 @@ func (s *scheduledQueueSuite) TestLookAheadTask_NoLookAheadTask() { (1 + testQueueOptions.MaxPollIntervalJitterCoefficient()) * float64(testQueueOptions.MaxPollInterval()), ))) select { - case <-s.scheduledQueue.timerGate.FireChan(): + case <-s.scheduledQueue.timerGate.FireCh(): default: s.Fail("timer gate should fire at the end of look ahead window") } @@ -247,7 +247,7 @@ func (s *scheduledQueueSuite) TestLookAheadTask_ErrorLookAhead() { timerGate.SetCurrentTime(s.scheduledQueue.nonReadableScope.Range.InclusiveMin.FireTime) select { - case <-s.scheduledQueue.timerGate.FireChan(): + case <-s.scheduledQueue.timerGate.FireCh(): default: s.Fail("timer gate should fire when time reaches look ahead range") } diff --git a/service/history/queues/rescheduler.go b/service/history/queues/rescheduler.go index 0d197479536..f3bc9ebb57e 100644 --- a/service/history/queues/rescheduler.go +++ b/service/history/queues/rescheduler.go @@ -209,7 +209,7 @@ func (r *reschedulerImpl) rescheduleLoop() { case <-r.shutdownCh: r.drain() return - case <-r.timerGate.FireChan(): + case <-r.timerGate.FireCh(): r.reschedule() case <-cleanupTimer.C: r.cleanupPQ()