It is difficult to achieve a spirit of genuine cooperation as long as people remain indifferent to the feelings and happiness of others.
— Dalai Lama
Hello, here is Wesley, Today’s article is about timer changes in Go 1.23. Without further ado, let’s get started.💪
Go 1.23 includes a new implementation of channel-based timers created by time.NewTimer, time.After, time.NewTicker, and time.Tick.
Enhanced Garbage Collection
- In previous Go versions (before 1.23), if a program did not explicitly stop using the
Stop
method, unstoppedtime.Timers
andtime.Tickers
would remain in memory. This could lead to resource leaks, especially in long-running programs. - Go 1.23 resolves this issue by allowing unreferenced
time.Timers
andtime.Tickers
to be garbage-collected immediately. This ensures efficient memory management and prevents unnecessary resource usage.
It’s a good habit to use defer
or other methods to call Stop
explicitly, but if you forget to do so, it may lead to irreversible consequences.
Go has further strengthened the robustness of code by making this change, which is definitely commendable. If you call the NewTicker
method in Go 1.23, you’ll see a comment:
1 | // Before Go 1.23, the garbage collector did not recover |
Source: src/time/tick.go
- Go at Google
Channel Synchronization
- In previous Go versions (before 1.23), the timer channels for
time.Timer
andtime.Ticker
had a buffer capacity of 1. This could lead to challenges when usingReset
orStop
methods, as calling them might pass outdated values. - Go 1.23 introduces synchronized timer channels (unbuffered, with a capacity of 0). This effectively prevents further sending or receiving signals on the channel after calling
Reset
orStop
. This simplifies reasoning about timer behavior and eliminates potential issues with outdated values.
Example:
1 | package main |
Impact on Existing Code
Compatibility
- The new timer behavior will only be enabled when the program’s
go.mod
file specifies Go 1.23.0 or a later version. If you’re building older code with Go 1.23, the previous timer behavior will still apply. - To restore asynchronous channel behavior (even if using Go 1.23 or later), you can set GODEBUG:
GODEBUG=asynctimerchan=1
.
Channel Length
If your code relies on checking the length of a channel (using len
) to determine whether a receive operation is successful, you’ll need to modify it to use non-blocking receives. Since channels are now unbuffered, len
and cap
will always return 0.
For example, in Go 1.23, you would need to change the following logic:
1 | func main() { |
to:
1 | func main() { |
Scheduling Delay
Before Go 1.23, when creating a timer, there was a scheduling delay involved. The latest timer implementation no longer takes into account this scheduling delay, so this change may also affect existing code.
For example:
1 | package main |
One output:
- Go 1.22:
done
(9 times) - Go 1.23:
done
,timeout
,done
,done
,done
,timeout
,timeout
,timeout
,timeout
Summary
- Improved garbage collection for channel-based timers that are not referenced.
- Asynchronous timer channels have been changed to synchronous timer channels.
- Scheduling delay when creating channel-based timers has been eliminated.
In summary, these changes will improve the behavior of timers in Go 1.23, promoting better resource management and clearer Reset and Stop semantics.
However, they may also introduce potential issues if you encounter compatibility problems with old code; consider using GODEBUG settings or updating your code to handle unbuffered (blocking) channels properly.
References
Go Wiki: Go 1.23 Timer Channel Changes - The Go Programming Language
More Series Articles about Golang Forward-looking:
https://wesley-wei.medium.com/list/golang-forwardlooking-12998e22f7eb
And I’m Wesley, delighted to share knowledge from the world of programming.
Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.
See you in the next article. 👋
中文文章: https://programmerscareer.com/zh-cn/golang-timer-changes/
Author: Wesley Wei – Twitter Wesley Wei – Medium
Note: Originally written at https://programmerscareer.com/golang-timer-changes/ at 2024-07-06 23:39. If you choose to repost or use this article, please cite the original source.
Comments