From a63e4a8ca1b2ba4fc6acce5ce5eb49a8ae699d6d Mon Sep 17 00:00:00 2001 From: Sean Date: Wed, 6 Apr 2016 10:34:11 -0700 Subject: [PATCH] Add threadsafety notes This is a comments-only change to note the threadsafety properties. It may be the case that ForAttempt is always threadsafe, but the Go race detector complains about the default setting on lines 44-52. And the workaround is quite easy. --- backoff.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/backoff.go b/backoff.go index 233c9a5..c8ea03a 100644 --- a/backoff.go +++ b/backoff.go @@ -10,6 +10,10 @@ import ( //After every call to Duration() it is multiplied by Factor. //It is capped at Max. It returns to Min on every call to Reset(). //Used in conjunction with the time package. +// +// Backoff is not threadsafe, but the ForAttempt method can be +// used concurrently if non-zero values for Factor, Max, and Min +// are set on the Backoff shared among threads. type Backoff struct { //Factor is the multiplying factor for each increment step attempts, Factor float64 @@ -31,6 +35,9 @@ func (b *Backoff) Duration() time.Duration { // you have a large number of independent Backoffs, but don't want use // unnecessary memory storing the Backoff parameters per Backoff. The first // attempt should be 0. +// +// ForAttempt is threadsafe iff non-zero values for Factor, Max, and Min +// are set before any calls to ForAttempt are made. func (b *Backoff) ForAttempt(attempt float64) time.Duration { //Zero-values are nonsensical, so we use //them to apply defaults