pollsource

package
v1.4.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 9 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 pollsource provides a reusable polling loop with bounded exponential backoff and jitter for pull-based catalog collector sources.

A pull source supplies a collection callback and receives polling, backoff, cancellation, and collection-attempt notification behavior without coupling that behavior to the shared pipeline. Process readiness is not managed here; it is owned by the healthcheck extension.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffConfig

type BackoffConfig struct {
	// InitialInterval is the base delay after the first failed cycle.
	InitialInterval util.Duration `json:"initialInterval,omitempty"`

	// MaxInterval is the maximum actual retry delay, including jitter.
	MaxInterval util.Duration `json:"maxInterval,omitempty"`

	// Multiplier advances the base interval after each consecutive failure.
	Multiplier float64 `json:"multiplier,omitempty"`

	// RandomizationFactor applies symmetric jitter around the base interval.
	// It must be within [0, 1].
	RandomizationFactor float64 `json:"randomizationFactor,omitempty"`
}

BackoffConfig configures bounded exponential backoff with symmetric jitter.

func DefaultBackoffConfig

func DefaultBackoffConfig() BackoffConfig

DefaultBackoffConfig returns the default bounded-backoff configuration.

func (*BackoffConfig) Validate

func (c *BackoffConfig) Validate() error

Validate verifies that the backoff configuration can safely drive the polling loop.

type CollectFunc

type CollectFunc func(
	ctx context.Context,
) (*catalogcollector.CatalogSnapshot, error)

CollectFunc performs one complete collection cycle.

A successful call returns one complete desired-state snapshot. An error must return no usable partial snapshot; the helper does not forward a snapshot when collection fails.

type Helper

type Helper struct {

	// OnCollect is called exactly once for every collection attempt, after
	// collection finishes and before the snapshot is handed to the downstream
	// consumer. elapsed measures collection alone and therefore excludes
	// downstream consumption. err is the collection error, or nil when the
	// attempt produced a usable snapshot.
	//
	// Because the callback runs before consumption, a downstream failure or a
	// cancellation observed during consumption cannot change the recorded
	// collection outcome.
	OnCollect func(elapsed time.Duration, err error)
	// contains filtered or unexported fields
}

Helper drives a periodic complete-snapshot polling loop.

The first cycle starts immediately. After collection and downstream consumption both succeed, the helper resets backoff and waits pollInterval. Any collection or downstream failure advances backoff.

Collection cycles never overlap. Context cancellation interrupts collection, downstream processing when supported by the consumer, and inter-cycle waits.

func NewHelper

func NewHelper(
	id string,
	pollInterval time.Duration,
	backoff BackoffConfig,
	log *logrus.Entry,
	now NowFunc,
	jitter JitterFunc,
) *Helper

NewHelper constructs a polling helper.

now and jitter may be nil. Production clock and random-jitter implementations are used when they are not supplied.

func (*Helper) Run

func (h *Helper) Run(
	ctx context.Context,
	collect CollectFunc,
	next catalogcollector.Consumer,
) error

Run executes polling cycles until the context is cancelled or invalid helper configuration is detected.

Backoff resets only after both collection and downstream consumption succeed.

type JitterFunc

type JitterFunc func(max time.Duration) time.Duration

JitterFunc returns a random duration in [0, max). Tests may inject a deterministic implementation.

type NowFunc

type NowFunc func() time.Time

NowFunc returns the current time. It is used to measure collection-cycle duration and can be replaced by tests.

Jump to

Keyboard shortcuts

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