polling

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 26 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 read 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 connection whose kind cannot list a prefix.
	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")
	// 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 read at the same time (default [DefaultConcurrency]).
	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.

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.

func (*Source) Poll

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

Poll runs one pass and sends its events on out. It is what Run calls, exposed so that a caller can run a pass when it wants one. 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 that starts after the call, and returns when it ended.

func (*Source) Run

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

Run implements flagsource.Source: it runs a pass, waits, and runs the next, until ctx is done, and returns nil then. A second Run while one runs returns ErrRunning.

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.
	Prefix string
}

SourceSpec is a place to look: the flag set files found under a prefix of a connection.

Jump to

Keyboard shortcuts

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