polling

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 30 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package polling is the change source that polls a store: it lists the places that hold flag set files, reads the objects whose version marker moved, and delivers what it found as flagsource.Event values.

A pass lists every source once (a page at a time), reads the flag set files and the flag files and segments files they name, and sends events: a changed flag set file with the bytes of everything it names, a vanished one, an unreadable one, and at the end of the pass a flagsource.KindPass. A pass that fails sends nothing and keeps what it read, so the next one does not read it again; a listing that a request limit refused resumes from the last page it got.

The package knows the store: listings, version markers, conditional reads, the size and count limits, the request limit of a connection, one client for each connection. It knows nothing about flag set names, API keys, generations or the engine.

Index

Constants

View Source
const (
	// DefaultConcurrency is the number of objects that one source reads at the same time.
	DefaultConcurrency = 8
	// DefaultPollInterval is the time between two passes.
	DefaultPollInterval = 60 * time.Second
	// MinPollInterval is the shortest time between two passes.
	MinPollInterval = time.Second
	// DefaultJitter is the spread of the poll interval: plus or minus 10 percent.
	DefaultJitter = 0.1
	// DefaultBackoffMax caps the wait after failed passes.
	DefaultBackoffMax = 5 * time.Minute
	// DefaultMaxSetFileBytes is the largest flag set file: they are small documents.
	DefaultMaxSetFileBytes = 1 << 20
	// MaxEntries is the number of flag files and segments files one flag set file may name, and of files a prefix of a
	// flag set file may match.
	MaxEntries = 1000
	// MaxPrefixEntries is the number of prefix entries one flag set file may have: each costs one listing per pass.
	MaxPrefixEntries = 16
	// MaxKeyBytes is the longest key a flag set file may name.
	MaxKeyBytes = 1024
)

Defaults and limits of a Config.

Variables

View Source
var (
	// ErrInvalidSpec is returned for sources that cannot be used.
	ErrInvalidSpec = errors.New("polling: invalid sources")
	// ErrNoLister is returned when a source names a prefix of a connection whose kind cannot list (it takes a key).
	ErrNoLister = errors.New("polling: the connection cannot list keys")
	// ErrUnknownConnection is returned for a connection that is not one of the backends.
	ErrUnknownConnection = errors.New("polling: unknown connection")
	// ErrRunning is returned by a [Source.Run] while another one runs.
	ErrRunning = errors.New("polling: the source runs already")
	// ErrStopped is returned by [Source.Refresh] when [Source.Run] stops before the pass that answers the call.
	ErrStopped = errors.New("polling: the source stopped")
	// ErrPass is the cause of every failed pass: the previous state stays.
	ErrPass = errors.New("polling: pass failed")
	// ErrTooManyKeys is the cause of a listing that holds more keys than the limit allows.
	ErrTooManyKeys = errors.New("polling: too many keys under the prefix")
)

Errors of a Config or of a pass. Test them with errors.Is.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	// Store reads one object.
	Store objectstore.ObjectStore
	// Lister lists a prefix; nil when the kind cannot list.
	Lister objectstore.Lister
	// ReadTwice makes a pass read an object that changed twice and compare the two reads: a writer that is still
	// writing (a local file) changes the bytes between them. A remote store replaces an object atomically and does not
	// need it.
	ReadTwice bool
}

Backend is what a pass needs from one connection of the bootstrap file. The connection layer builds it from the kind of the connection: a kind that cannot list a prefix has no Lister, so a source on it is refused when the sources are set, never found out by a type assertion.

type Config

type Config struct {
	// Tracing are the OpenTelemetry providers of the source: with them enabled every pass has a span of its own
	// ("flagsource.pass"), the parent of the requests that the pass makes. The zero value records nothing and costs
	// nothing.
	Tracing tracing.Providers
	// Scheduler waits between passes; nil is a timer.
	Scheduler Scheduler
	// Backends are the connections the sources may name, by name.
	Backends map[string]Backend
	// Logger receives one line per distinct failure; nil discards.
	Logger *slog.Logger
	// Jitter returns a value in [0, 1) that spreads the waits; nil is no spread.
	Jitter func() float64
	// Now reads the time; nil is the clock of the process.
	Now func() time.Time
	// Default is the flag set that the bootstrap file defines itself (the `flags` section): its name is
	// [flagsource.DefaultFlagSetName] and its entries name their connection. Nil when the file has none.
	Default *flagsource.SetFile
	// Sources say where to look.
	Sources []SourceSpec
	// PollInterval is the time between two passes (default [DefaultPollInterval]).
	PollInterval time.Duration
	// JitterFraction is the spread of the poll interval, in [0, 1). Zero with DisableJitter false means
	// [DefaultJitter].
	JitterFraction float64
	// MaxObjects is the largest number of objects of a pass (default controlplane.DefaultMaxObjects).
	MaxObjects int
	// MaxObjectBytes is the largest object (default controlplane.DefaultMaxObjectBytes).
	MaxObjectBytes int64
	// MaxTotalBytes is the largest sum of the distinct objects (default controlplane.DefaultMaxTotalBytes).
	MaxTotalBytes int64
	// MaxSetFileBytes is the largest flag set file (default [DefaultMaxSetFileBytes]).
	MaxSetFileBytes int64
	// Concurrency is the number of objects that one source reads at the same time (default [DefaultConcurrency]). Every
	// source has its own lane, so the total is the number of sources times this value; the limit of each connection still
	// caps it.
	Concurrency int
	// BackoffMax caps the wait after failed passes (default [DefaultBackoffMax]).
	BackoffMax time.Duration
	// DisableJitter turns the spread of the interval and the start phase off.
	DisableJitter bool
}

Config configures a Source.

type Scheduler

type Scheduler interface {
	// Wait blocks for d, until wake delivers (a nil channel never does), or until ctx is done. It returns nil when d
	// passed or wake delivered, and the error of ctx otherwise.
	Wait(ctx context.Context, d time.Duration, wake <-chan struct{}) error
}

Scheduler is the clock seam of the polling loop: the one place that waits.

type Source

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

Source is the polling change source. It implements flagsource.Source.

Source.Run gives each place to look at (each entry of the sources, and the flag set of the bootstrap file) a loop of its own, a lane: one pass at a time, on its own schedule and backoff. A place that hangs or fails holds only its own lane, never the flag sets of the other places.

func New

func New(cfg *Config) (*Source, error)

New returns a source. It makes no request.

func (*Source) Nudge

func (s *Source) Nudge() bool

Nudge implements flagsource.Source: it asks for a pass now, of every place that is not waiting after a failed pass.

func (*Source) Poll

func (s *Source) Poll(ctx context.Context, out chan<- flagsource.Event) error

Poll runs one pass over every place and sends its events on out, in one batch. It is what a caller uses when it wants one pass of its own: Source.Run runs a lane for each place instead. A failed pass sends nothing and returns an error that wraps ErrPass.

func (*Source) Refresh

func (s *Source) Refresh(ctx context.Context) error

Refresh implements flagsource.Source: it runs a pass of every place that starts after the call, and returns when they all ended, with the result of that pass of each place. When Source.Run stops before they ended it returns an error that wraps ErrStopped.

A lane keeps the results of its last 64 passes: a call that waited so long for another place that its own pass of this lane is older than that gets the error of the lane's last pass instead.

A place that hangs holds the call until its pass ends or ctx does: the other places are not held back in their own schedule. Before Source.Run has made its lanes it waits for the pass of Source.Poll or for the first pass of the lanes of Run, as it always did. It waits for the sources that exist when it is called: the lanes are made by Source.Run only, so a source that Source.SetSources just added is read by the next pass (Source.Nudge), and a Refresh at once may return before it.

func (*Source) Run

func (s *Source) Run(ctx context.Context, out chan<- flagsource.Event) error

Run implements flagsource.Source: it runs a lane for each place to look at until ctx is done, and returns nil then. The places can change while it runs (Source.SetSources): the lanes follow. A second Run while one runs returns ErrRunning; a schedule the poller refuses (a negative interval) is returned, never dropped: a loop that does not run must not be silent.

func (*Source) SetDefault

func (s *Source) SetDefault(def *flagsource.SetFile) bool

SetDefault replaces the flag set of the `flags` section of the bootstrap file (nil removes it). It tells whether it differs from the one the source had.

func (*Source) SetSources

func (s *Source) SetSources(sources []SourceSpec) (bool, error)

SetSources replaces the places to look. The next pass uses them. Sources that name a connection the source does not have, or a connection that cannot list, are refused and nothing changes. It tells whether the places are not the ones it had: a caller that asks for a pass because of a change asks only then.

func (*Source) Stats

func (s *Source) Stats() flagsource.Stats

Stats implements flagsource.Source.

type SourceSpec

type SourceSpec struct {
	Connection string
	// Prefix is the key prefix; empty means the whole connection. A prefix that does not end with a slash gets one. It is
	// empty when Key is set.
	Prefix string
	// Key is the key of one flag set file; empty for a source that is listed.
	Key string
}

SourceSpec is a place to look: the flag set files found under a prefix of a connection, or the one flag set file that Key names. Key is for a connection that cannot list (an http one): it is read, never listed.

Jump to

Keyboard shortcuts

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