tailscale.comtailscale.com/util/reload Index | Files

package reload

import "tailscale.com/util/reload"

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

Index

Constants

const DefaultInterval = 5 * time.Minute

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

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.

Source Files

reload.go

Version
v1.84.0 (latest)
Published
May 21, 2025
Platform
linux/amd64
Imports
9 packages
Last checked
1 day ago

Tools for package owners.