poller

package
v0.0.0-...-86cb477 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package poller supports periodic polling to load a value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Getter

type Getter func(context.Context) (any, error)

A Getter returns a value.

type Poller

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

A Poller maintains a current value, and refreshes it by periodically polling for a new value.

func New

func New(initial any, getter Getter, onError func(error)) *Poller

New creates a new poller with an initial value. The getter is invoked to obtain updated values. Errors returned from the getter are passed to onError.

func (*Poller) Current

func (p *Poller) Current() any

Current returns the current value. Initially, this is the value passed to New. After each successful poll, the value is updated. If a poll fails, the value remains unchanged.

func (*Poller) Poll

func (p *Poller) Poll(ctx context.Context)

Poll calls p's getter immediately and synchronously.

func (*Poller) Start

func (p *Poller) Start(ctx context.Context, period time.Duration)

Start begins polling in a separate goroutine, at the given period. To stop the goroutine, cancel the context passed to Start.

Jump to

Keyboard shortcuts

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