scheduler

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package scheduler runs periodic work off one goroutine and one timer.

Forge and its extensions want a lot of periodic work: health check rounds, metric collection, storage cleanup, compression, cache eviction, lease renewal. Written the obvious way, each of those is its own goroutine parked on its own time.Ticker. That cost is per subsystem and it accumulates with every extension an app installs, so an app is never fully idle: each ticker is a runtime timer that wakes a core to send on a channel nobody was waiting on except one parked goroutine.

A Scheduler holds every job in one heap ordered by next run, so the process arms exactly one timer for the earliest of them and sleeps until then.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Runner

type Runner func(ctx context.Context)

Runner is the work a job does on each run. The context is cancelled when the scheduler stops, so a long job can notice and return.

type Scheduler

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

Scheduler runs registered jobs on their intervals.

The zero value is not usable; call New.

func Default

func Default() *Scheduler

Default returns the shared scheduler, starting it on first use.

It has no Stop: it belongs to the process, not to any one app, and it costs one parked goroutine when nothing is registered. Cancel individual jobs with the function Every returns.

func New

func New(name string) *Scheduler

New creates a scheduler. Jobs may be registered before or after Start.

func (*Scheduler) Every

func (s *Scheduler) Every(name string, interval time.Duration, fn Runner) (cancel func())

Every registers fn to run every interval and returns a function that cancels it. A non-positive interval registers nothing and returns a no-op.

The first run happens one interval from now, matching a ticker.

A job never overlaps itself: if a run is still going when the next one is due, that run is skipped rather than queued. Runs happen on their own goroutines, so a slow job delays only itself.

func (*Scheduler) Len

func (s *Scheduler) Len() int

Len reports how many jobs are registered.

func (*Scheduler) Name

func (s *Scheduler) Name() string

Name reports the scheduler's name, for logs and diagnostics.

func (*Scheduler) Start

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

Start begins running jobs. It is idempotent.

func (*Scheduler) Stop

func (s *Scheduler) Stop(ctx context.Context) error

Stop halts the scheduler and waits for any in-flight job runs to return or for ctx to expire. It is idempotent.

Jump to

Keyboard shortcuts

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