-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nobug: implement a safe timer function
This PR adds libtime.SafeTimer function, which creates a timer that is more safe to use in select statements, but without the overhead of creating a context.Context object. Instead it returns a StopFunc along with the time.Timer, forcing the caller to deal with stopping the timer.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package libtime | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// StopFunc is used to stop a time.Timer. | ||
// | ||
// Calling StopFunc prevents its time.Timer from firing. Returns true if the call | ||
// stops the timer, false if the timer has already expired. or has been stopped. | ||
// | ||
// https://pkg.go.dev/time#Timer.Stop | ||
type StopFunc func() bool | ||
|
||
// SafeTimer creates a time.Timer and a StopFunc, forcing the caller to deal | ||
// with the otherwise potential resource leak. Encourages safe use of a time.Timer | ||
// in a select statement, but without the overhead of a context.Context. | ||
// | ||
// Typical usage: | ||
// | ||
// t, stop := libtime.SafeTimer(interval) | ||
// defer stop() | ||
// for { | ||
// select { | ||
// case <- t.C: | ||
// foo() | ||
// case <- otherC : | ||
// return | ||
// } | ||
// } | ||
// | ||
// Does not panic if duration is <= 0, instead assuming the smallest positive value. | ||
func SafeTimer(duration time.Duration) (*time.Timer, StopFunc) { | ||
t := time.NewTimer(duration) | ||
return t, t.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,20 @@ | ||
package libtime | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_SafeTimer(t *testing.T) { | ||
delay := 3 * time.Millisecond | ||
start := time.Now() | ||
|
||
timer, stop := SafeTimer(delay) | ||
defer stop() | ||
<-timer.C | ||
|
||
elapsed := time.Since(start) | ||
require.GreaterOrEqual(t, int64(elapsed), int64(delay)) | ||
} |