Documentation ¶
Overview ¶
Package ticker provides a dynamic ticker that can change its interval at runtime. The ticker can be stopped gracefully and handles context-based termination.
This package is useful for scenarios where periodic execution of a function is needed and the interval might need to change dynamically based on runtime conditions.
It also invokes a first tick immediately after the ticker starts. It's safe to use it concurrently.
It also terminates gracefully when the context is done (return ctx.Err()) or when the stop signal is received.
Example usage:
ticker := New(time.Second, func(ctx context.Context, t *Ticker) error { resp, err := client.GetPrice(ctx) if err != nil { logger.Err(err).Error().Msg("failed to get price") return nil } observer.SetPrice(resp.GasPrice) t.SetInterval(resp.GasPriceInterval) return nil }) err := ticker.Run(ctx)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SecondsFromUint64 ¶
SecondsFromUint64 converts uint64 to time.Duration in seconds.
Types ¶
type Ticker ¶
type Ticker struct {
// contains filtered or unexported fields
}
Ticker represents a ticker that will run a function periodically. It also invokes BEFORE ticker starts.
func (*Ticker) Run ¶
Run runs the ticker by blocking current goroutine. It also invokes BEFORE ticker starts. Stops when (if any): - context is done (returns ctx.Err()) - task returns an error or panics - shutdown signal is received
func (*Ticker) SetInterval ¶
SetInterval updates the interval of the ticker.