Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: time: add API to retrieve next trigger time for Timer and Ticker #66680

Open
ktalg opened this issue Apr 4, 2024 · 1 comment
Open
Labels
Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@ktalg
Copy link

ktalg commented Apr 4, 2024

Proposal Details

The Go time package's Timer and Ticker are fundamental for scheduling future events. However, there's no straightforward way to query their next trigger time.

Currently, developers must resort to indirect methods to estimate the next trigger time. For example:

ticker := time.NewTicker(5 * time.Minute)
// Inaccurate assumption of next tick
nextTick := time.Now().Add(5 * time.Minute)

Estimating the next trigger time for Timer or Ticker is cumbersome and requires the context of their creation. For example, in a function that accepts only a Timer, the absence of creation context complicates accessing the next trigger time, hindering straightforward implementation and usage.

Internally, both Timer and Ticker leverage a structure similar to runtimeTimer, which already contains timing information such as when and nextwhen. This suggests that the necessary data for determining the next trigger time is already being tracked.

Proposal:
Introduce a method, NextTriggerTime(), (or any other aptly named method) be in Timer and Ticker, to expose the next trigger time. Given the internal tracking within runtimeTimer, this addition should be straightforward to implement and maintain.

Example usage:

ticker := time.NewTicker(5 * time.Minute)
nextTick := ticker.NextTriggerTime()

The suggestion to use a method to obtain this information aims to ensure the integrity of the Timer/Ticker's own structural encapsulation and to avoid triggering concurrency access conflicts.

@ktalg ktalg added the Proposal label Apr 4, 2024
@gopherbot gopherbot added this to the Proposal milestone Apr 4, 2024
@ktalg ktalg changed the title proposal: time: Add API to Retrieve Next Trigger Time for Timer/Ticker proposal: time: add API to retrieve next trigger time for Timer and Ticker Apr 4, 2024
@ianlancetaylor
Copy link
Contributor

Can you point to some existing code that would benefit from this change? Thanks.

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
Status: Incoming
Development

No branches or pull requests

4 participants