Help us shape the future of Go! Take the 2026 Go Developer Survey

reload

package
v1.104.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package reload contains functions that allow periodically reloading a value (e.g. a config file) from various sources.

Index

Constants

View Source
const DefaultInterval = 5 * time.Minute

DefaultInterval is the default value for ReloadOpts.Interval if none is provided.

Variables

This section is empty.

Functions

func New

func New[T any](ctx context.Context, opts ReloadOpts[T]) (func() T, error)

New creates and starts reloading the provided value as per opts. It returns a function that, when called, returns the current stored value, or an error that indicates something went wrong.

The value will be present immediately upon return.

Types

type ReloadOpts

type ReloadOpts[T any] struct {
	// Read is called to obtain the data to be unmarshaled; e.g. by reading
	// from a file, or making a network request, etc.
	//
	// An error from this function is fatal when calling New, but only a
	// warning during reload.
	//
	// This value is required.
	Read func(context.Context) ([]byte, error)

	// Unmarshal is called with the data that the Read function returns and
	// should return a parsed form of the given value, or an error.
	//
	// An error from this function is fatal when calling New, but only a
	// warning during reload.
	//
	// This value is required.
	Unmarshal func([]byte) (T, error)

	// Logf is a logger used to print errors that occur on reload. If nil,
	// no messages are printed.
	Logf logger.Logf

	// Interval is the interval at which to reload the given data from the
	// source; if zero, DefaultInterval will be used.
	Interval time.Duration

	// IntervalJitter is the jitter to be added to the given Interval; if
	// provided, a duration between 0 and this value will be added to each
	// Interval when waiting.
	IntervalJitter time.Duration
}

ReloadOpts specifies options for reloading a value. Various helper functions in this package can be used to create one of these specialized for a given use-case.

func FromJSONFile

func FromJSONFile[T any](path string) ReloadOpts[T]

FromJSONFile creates a ReloadOpts describing reloading a value of type T from the given JSON file on-disk.

Jump to

Keyboard shortcuts

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