-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time): Use [time.Timer] instead since go1.23. Timer.Stop documen…
…tation example no longer leads to deadlocks, as Timer/Ticker channels not receivable with old values after Stop or Reset returns. golang/go#27169 golang/go#37196 golang/go#14383
- Loading branch information
Showing
2 changed files
with
44 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
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,42 @@ | ||
// Copyright 2020 The searKing Author. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build go1.23 | ||
|
||
package time | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Timer to fix time: Timer.Stop documentation example easily leads to deadlocks | ||
// https://github.com/golang/go/issues/27169 | ||
// | ||
// Deprecated: Use [time.Timer] instead since go1.23. | ||
// https://github.com/golang/go/issues/37196 | ||
// https://github.com/golang/go/issues/14383 | ||
type Timer = time.Timer | ||
|
||
// Deprecated: Use [time.NewTimer] instead since go1.23. | ||
func NewTimer(d time.Duration) *Timer { | ||
return time.NewTimer(d) | ||
} | ||
|
||
// Deprecated: Use [time.Timer] instead since go1.23. | ||
func WrapTimer(t *time.Timer) *Timer { | ||
return t | ||
} | ||
|
||
// Deprecated: Use [time.After] instead since go1.23. | ||
func After(d time.Duration) <-chan time.Time { | ||
return NewTimer(d).C | ||
} | ||
|
||
// Deprecated: Use [time.AfterFunc] instead since go1.23. | ||
func AfterFunc(d time.Duration, f func()) *Timer { | ||
t := time.AfterFunc(d, func() { | ||
f() | ||
}) | ||
return t | ||
} |