timeutil

package
v0.55.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2025 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JitterAvg = iota // Average jitter - tries to keep the average interval.
	JitterAdd        // Additive jitter - adds random jitter to the interval.
	JitterSub        // Subtractive jitter - subtracts random jitter from the interval.
)

Variables

This section is empty.

Functions

func AfterFunc

func AfterFunc(ctx context.Context, d time.Duration, f func()) *time.Timer

AfterFunc waits for the duration to elapse and then calls f in its own goroutine.

It works like time.AfterFunc but stops the timer if the context is done.

func Sleep

func Sleep(ctx context.Context, d time.Duration) bool

Sleep sleeps for the given duration or until the context is canceled.

It returns false if the context is canceled before the duration is over.

Types

type IntervalTicker

type IntervalTicker struct {
	// contains filtered or unexported fields
}

IntervalTicker ticks at the specified interval.

Interval ticker can be shared between multiple Scheduler instances.

func NewIntervalTicker

func NewIntervalTicker(interval time.Duration) *IntervalTicker

NewIntervalTicker returns a new IntervalTicker.

func (IntervalTicker) MarshalJSON

func (t IntervalTicker) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (IntervalTicker) MarshalText

func (t IntervalTicker) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*IntervalTicker) Next

func (t *IntervalTicker) Next(at time.Time) time.Time

Next implements the NextTick interface.

func (*IntervalTicker) String

func (t *IntervalTicker) String() string

String returns the string representation of the interval.

func (*IntervalTicker) UnmarshalJSON

func (t *IntervalTicker) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*IntervalTicker) UnmarshalText

func (t *IntervalTicker) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

The duration is parsed using time.ParseDuration.

If a number is provided, it's assumed to be in seconds.

type JitterTicker

type JitterTicker struct {
	// contains filtered or unexported fields
}

JitterTicker adds a jitter to the next tick time.

func NewJitterTicker

func NewJitterTicker(interval NextTick, jitter time.Duration, seed int64, mode int) *JitterTicker

NewJitterTicker creates a new JitterTicker instance.

The mode specifies how the jitter is applied:

  • JitterAvg: The jitter is uniformly distributed between -jitter/2 and jitter/2.
  • JitterAdd: The random jitter is added to the interval.
  • JitterSub: The random jitter is subtracted from the interval.

The next tick time is guaranteed to be at least 1 nanosecond later. The seed initializes the random number generator.

Note: If the interval is a ScheduledTicker, the mode MUST be set to JitterAdd.

func (*JitterTicker) Next

func (t *JitterTicker) Next(at time.Time) time.Time

type ManualTicker

type ManualTicker struct {
	// contains filtered or unexported fields
}

ManualTicker ticks only when requested.

Manual ticker CAN NOT be shared between multiple Scheduler instances.

func NewManualTicker

func NewManualTicker() *ManualTicker

NewManualTicker returns a new ManualTicker.

func (*ManualTicker) Next

func (t *ManualTicker) Next(_ time.Time) time.Time

Next implements the NextTick interface.

func (*ManualTicker) Tick

func (t *ManualTicker) Tick()

Tick sends a tick to the ticker channel.

Tick is blocking until the tick is consumed.

func (*ManualTicker) TickAt

func (t *ManualTicker) TickAt(at time.Time)

TickAt sends a tick to the ticker channel with the given time.

TickAt is blocking until the tick is consumed.

type NextTick

type NextTick interface {
	// Next returns the next tick time after the given time.
	Next(at time.Time) time.Time
}

NextTick provides the next tick time for the Scheduler.

type ScheduledTicker

type ScheduledTicker struct {
	// contains filtered or unexported fields
}

ScheduledTicker ticks at the specified schedule.

Scheduled ticker can be shared between multiple Scheduler instances.

func NewScheduledTicker

func NewScheduledTicker(spec string) (*ScheduledTicker, error)

NewScheduledTicker returns a new ScheduledTicker.

The spec is a string representing a schedule in the following format:

second minute hour day month weekday

Where:

  • Second and minute can be a number (0-59) or an asterisk (*). Optionally, the suffix "s" or "m" can be added respectively.
  • Hour can be a number (0-23) or an asterisk (*). Optionally, the suffix "h" can be added.
  • A day can be a number (1-31) or an asterisk (*). Optionally, the suffix "d" can be added.
  • The month can be a number (1-12), three-letter abbreviation (jan, feb, ...) or the full name (january, february, ...).
  • The weekday can be a three-letter abbreviation (sun, mon, ...) or the full name (sunday, monday, ...).
  • It is possible to specify multiple values separated by a comma.
  • Trailing parts can be omitted (except for seconds), in which case the asterisk is assumed.

Examples:

  • *s - every second
  • 0s *m - every minute
  • 0s 0m *h *d * * - every hour
  • 0s 0m 0h *d * * - every day
  • 0s 0m 0h 1d * * - every month at the first day of the month
  • 0s 0m 0h 1d jan * - every year at the first day of January
  • 0s 0m 0h 1d jan wed - every year at the first day of January if it's Wednesday
  • 0s *m *h 1d - every minute on the first day of the month
  • 0s 0,15,30,45m - every 15 minutes

func (ScheduledTicker) MarshalJSON

func (s ScheduledTicker) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (ScheduledTicker) MarshalText

func (s ScheduledTicker) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*ScheduledTicker) Next

func (s *ScheduledTicker) Next(at time.Time) time.Time

Next implements the NextTick interface.

func (*ScheduledTicker) String

func (s *ScheduledTicker) String() string

String returns the string representation of the schedule.

func (*ScheduledTicker) UnmarshalJSON

func (s *ScheduledTicker) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*ScheduledTicker) UnmarshalText

func (s *ScheduledTicker) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler ticks at the specified schedule.

func NewScheduler

func NewScheduler(n NextTick, tz *time.Location) *Scheduler

NewScheduler returns a new Scheduler.

Scheduler guarantees that tick will be sent for every scheduled tick, even if the tick channel is blocked for a long enough to miss the next scheduled tick, unless SkipMissedTicks is called.

Timezone is optional, if nil, the local timezone is used.

func (*Scheduler) ShouldClose

func (s *Scheduler) ShouldClose()

ShouldClose will close the scheduler channel when the context is canceled.

Must be called before starting the scheduler.

func (*Scheduler) SkipMissedTicks

func (s *Scheduler) SkipMissedTicks()

SkipMissedTicks makes the scheduler skip missed ticks that should have been scheduled in the past. This can be useful when the TickCh consumer is not fast enough to process incoming ticks, and we do not want to process them.

Note that for extremely short tick intervals (e.g., <1ms), the scheduler may never catch up with the schedule, even if the consumer is quick, which can cause no ticks to be sent.

Must be called before starting the scheduler.

func (*Scheduler) Start

func (s *Scheduler) Start(ctx context.Context)

Start starts the scheduler.

It panics if the scheduler is already started or the context is nil.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the scheduler.

If the scheduler is not started or already stopped, it does nothing.

func (*Scheduler) Tick

func (s *Scheduler) Tick()

Tick sends a tick to the ticker channel.

Scheduler must be started before calling this method, otherwise it panics.

Note, this method is blocking until the tick is consumed. If the consumer is in the same goroutine, it will deadlock.

func (*Scheduler) TickAt

func (s *Scheduler) TickAt(at time.Time)

TickAt sends a tick to the ticker channel with the given time.

Scheduler must be started before calling this method, otherwise it panics.

Note, this method is blocking until the tick is consumed. If the consumer is in the same goroutine, it will deadlock.

func (*Scheduler) TickCh

func (s *Scheduler) TickCh() <-chan time.Time

TickCh returns the scheduler channel.

Channel is closed when the context is canceled.

type VarTicker

type VarTicker struct {
	// contains filtered or unexported fields
}

VarTicker ticks at the specified intervals.

This ticker CAN NOT be shared between multiple Scheduler instances.

func NewVarTicker

func NewVarTicker(intervals ...time.Duration) *VarTicker

NewVarTicker returns a new VarTicker instance.

func (*VarTicker) Next

func (t *VarTicker) Next(at time.Time) time.Time

Next implements the NextTick interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL