Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4367 from nvanbenschoten/nvanbenschoten/timers
Allocate timers outside of loops to avoid repeat allocations
- Loading branch information
Showing
10 changed files
with
254 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2016 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Author: Nathan VanBenschoten (nvanbenschoten@gmail.com) | ||
|
||
package util | ||
|
||
import "time" | ||
|
||
// The Timer type represents a single event. When the Timer expires, | ||
// the current time will be sent on Timer.C. | ||
// | ||
// This timer implementation is an abstraction around the standard | ||
// library's time.Timer that provides a temporary workaround for the | ||
// issue described in https://github.com/golang/go/issues/14038. As | ||
// such, this timer should only be used when Reset is planned to | ||
// be called continually in a loop. For this Reset pattern to work, | ||
// Timer.Read must be set to true whenever a timestamp is read from | ||
// the Timer.C channel. If Timer.Read is not set to true when the | ||
// channel is read from, the next call to Timer.Reset will deadlock. | ||
// This pattern looks something like: | ||
// | ||
// var timer util.Timer | ||
// defer timer.Stop() | ||
// for { | ||
// timer.Reset(wait) | ||
// switch { | ||
// case <-timer.C: | ||
// timer.Read = true | ||
// ... | ||
// } | ||
// } | ||
// | ||
// Note that unlike the standard library's Timer type, this Timer will | ||
// not begin counting down until Reset is called for the first time, as | ||
// there is no constructor function. | ||
type Timer struct { | ||
*time.Timer | ||
Read bool | ||
} | ||
|
||
// Reset changes the timer to expire after duration d and returns | ||
// the new value of the timer. This method includes the fix proposed | ||
// in https://github.com/golang/go/issues/11513#issuecomment-157062583, | ||
// but requires users of Timer to set Timer.Read to true whenever | ||
// they successfully read from the Timer's channel. Reset operates on | ||
// and returns a value so that Timer can be stack allocated. | ||
func (t *Timer) Reset(d time.Duration) { | ||
if t.Timer == nil { | ||
t.Timer = time.NewTimer(d) | ||
return | ||
} | ||
if !t.Timer.Reset(d) && !t.Read { | ||
<-t.C | ||
} | ||
t.Read = false | ||
} | ||
|
||
// Stop prevents the Timer from firing. It returns true if the call stops | ||
// the timer, false if the timer has already expired, been stopped previously, | ||
// or had never been initialized with a call to Timer.Reset. Stop does not | ||
// close the channel, to prevent a read from succeeding incorrectly. | ||
func (t *Timer) Stop() bool { | ||
if t.Timer == nil { | ||
return false | ||
} | ||
return t.Timer.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2016 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Author: Nathan VanBenschoten (nvanbenschoten@gmail.com) | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
const timeStep = 10 * time.Millisecond | ||
|
||
func TestTimerTimeout(t *testing.T) { | ||
var timer Timer | ||
defer func() { | ||
if stopped := timer.Stop(); stopped { | ||
t.Errorf("expected Stop to return false, got true") | ||
} | ||
}() | ||
timer.Reset(timeStep) | ||
|
||
<-timer.C | ||
timer.Read = true | ||
|
||
select { | ||
case <-timer.C: | ||
t.Errorf("expected timer to only timeout once after Reset; got two timeouts") | ||
case <-time.After(5 * timeStep): | ||
} | ||
} | ||
|
||
func TestTimerStop(t *testing.T) { | ||
var timer Timer | ||
timer.Reset(timeStep) | ||
if stopped := timer.Stop(); !stopped { | ||
t.Errorf("expected Stop to return true, got false") | ||
} | ||
|
||
select { | ||
case <-timer.C: | ||
t.Errorf("expected timer to stop after call to Stop; got timer that was not stopped") | ||
case <-time.After(5 * timeStep): | ||
} | ||
} | ||
|
||
func TestTimerUninitializedStopNoop(t *testing.T) { | ||
var timer Timer | ||
if stopped := timer.Stop(); stopped { | ||
t.Errorf("expected Stop to return false when the timer was never reset, got true") | ||
} | ||
} | ||
|
||
func TestTimerResetBeforeTimeout(t *testing.T) { | ||
var timer Timer | ||
defer timer.Stop() | ||
timer.Reset(timeStep) | ||
|
||
timer.Reset(timeStep) | ||
<-timer.C | ||
timer.Read = true | ||
|
||
select { | ||
case <-timer.C: | ||
t.Errorf("expected timer to only timeout once after Reset; got two timeouts") | ||
case <-time.After(5 * timeStep): | ||
} | ||
} | ||
|
||
func TestTimerResetAfterTimeoutAndNoRead(t *testing.T) { | ||
var timer Timer | ||
defer timer.Stop() | ||
timer.Reset(timeStep) | ||
|
||
time.Sleep(2 * timeStep) | ||
|
||
timer.Reset(timeStep) | ||
<-timer.C | ||
timer.Read = true | ||
|
||
select { | ||
case <-timer.C: | ||
t.Errorf("expected timer to only timeout once after Reset; got two timeouts") | ||
case <-time.After(5 * timeStep): | ||
} | ||
} | ||
|
||
func TestTimerResetAfterTimeoutAndRead(t *testing.T) { | ||
var timer Timer | ||
defer timer.Stop() | ||
timer.Reset(timeStep) | ||
|
||
<-timer.C | ||
timer.Read = true | ||
|
||
timer.Reset(timeStep) | ||
<-timer.C | ||
timer.Read = true | ||
|
||
select { | ||
case <-timer.C: | ||
t.Errorf("expected timer to only timeout once after Reset; got two timeouts") | ||
case <-time.After(5 * timeStep): | ||
} | ||
} | ||
|
||
func TestTimerMakesProgressInLoop(t *testing.T) { | ||
var timer Timer | ||
defer timer.Stop() | ||
for i := 0; i < 5; i++ { | ||
timer.Reset(timeStep) | ||
<-timer.C | ||
timer.Read = true | ||
} | ||
} |