Ticker Reset Adapts Polling Interval
time.Ticker.Reset changes the interval between ticks, so a poller can start fast and back off to slower cadence without rebuilding the timer.
Code
package main
import (
"fmt"
"time"
)
func main() {
// adaptive polling: Reset adapts the interval between ticks
ticker := time.NewTicker(30 * time.Millisecond)
defer ticker.Stop()
deadline := time.After(250 * time.Millisecond)
polls := 0
for {
select {
case <-ticker.C:
polls++
if polls == 1 {
ticker.Reset(120 * time.Millisecond) // idle workload: slower polling cadence
}
fmt.Println("poll", polls)
case <-deadline:
fmt.Println("stopped after", polls, "polls")
return
}
}
}What the code does
The compressed view: all the work sits in the function main, doing all the work.
One loop does the cycling here: for. defer schedules the cleanup, and the scheduled call fires no matter how the function exits.
What to watch for
- The runtime alone is required here — no packages, no install step.
Applying it
The file’s steps are announced right in the output: poll, stopped after.
Statements that carry the weight
defer ticker.Stop()
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- What does Ticker Reset Adapts Polling Interval print when run?
- No setup needed: run the file and look for labeled lines like `poll`, `stopped after` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.