Skip to content
Golang timer for humans.
Branch: master
Clone or download
Latest commit 1a236cb Sep 17, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md Update README Sep 17, 2018
goodtimer.go Update documentation Sep 15, 2018
goodtimer_examples_test.go Add examples of using Stop and Reset Sep 15, 2018
goodtimer_test.go Initial commit Sep 11, 2018

README.md

goodtimer

Golang timer for humans.

goodtimer is a thin wrapper around the standard time.Timer, and it tries to play two roles:

  1. A library that helps you use time.Timer more easily.
  2. As well as a demonstration that shows you how to use time.Timer correctly.

Installation

$ go get -u github.com/RussellLuo/goodtimer

Documentation

For usage and examples see the Godoc.

Why?!

TL;DR: The standard time.Timer is hard to use correctly.

Timer.Stop

Per the documentation of Timer.Stop, to stop the timer created with NewTimer, you need to check the return value and drain the channel if necessary:

if !t.Stop() {
	<-t.C
}

But the draining operation will be blocked if the the program has already received from the Timer's channel before. So someone suggests doing a non-blocking draining:

if !t.Stop() {
	select {
	case <-t.C: // try to drain the channel
	default:
	}
}

However, there is a race condition between draining the channel and sending time into the channel, which may lead to a undrained channel.

Timer.Reset

To reset a timer, is must have expired or be stopped before. So Timer.Reset has almost the same issue with Timer.Stop.

Solutions

Finally, as Russ Cox suggested (here and here), the correct way to use time.Timer is:

  • All the Timer operations (Timer.Stop, Timer.Reset and receiving from or draining the channel) should be done in the same goroutine.
  • The program should manage an extra status showing whether it has received from the Timer's channel or not.
You can’t perform that action at this time.