Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
fix: do not create a new moment for each tick
Browse files Browse the repository at this point in the history
This change avoid creating a new moment for each Ticker’s tick but still
maintains backwards compatibility for tick observeration. It also fixes
an accidentally introduced data race for id field between Reset and do
methods in ticker.
  • Loading branch information
tie committed Dec 27, 2021
1 parent 6047b6d commit ac80857
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ func (t *ticker) do(now time.Time) {

// It is safe to mutate ID without a lock since at most one moment
// exists for the given ticker and moments run under the Time’s lock.
//
// Additionally, while we probably should be resetting the moment with
// the initial ticker’s ID, it is not possible since that would break
// backwards compatibility for users that rely on Time’s Observe method
// to observe ticks.
//
// t.time.resetUnlocked(t.dur, t.id, t.do, nil)
//
t.id = t.time.planUnlocked(now.Add(t.dur), t.do)
t.time.resetUnlocked(t.dur, t.id, t.do, nil)

// Ticker used to create a new moment for each tick and that would close
// the observe channel. Maintain backwards compatibility for users that
// may rely on this behavior.
t.time.observeUnlocked()
}

0 comments on commit ac80857

Please sign in to comment.