cachedfetch

package
v2.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cachedfetch provides a read-through cache for fetched values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fetcher

type Fetcher struct {
	// function that attempts to fetch the value
	Attempt func(context.Context) (string, error)

	// the name of the thing being fetched, used in the default log message.  At
	// least one of Name and LogFailure must be non-nil.
	Name string

	// function to log a fetch failure, given the error and the last successful
	// value.  This function is not called if there is no last successful value.
	// If left at its zero state, a default log message will be generated, using
	// Name.
	LogFailure func(error, interface{})

	// mutex to protect access to lastValue
	sync.Mutex
	// contains filtered or unexported fields
}

Fetcher supports fetching a value, such as from a cloud service API. An attempt is made to fetch the value on each call to Fetch, but if that attempt fails then a cached value from the last successful attempt is returned, if such a value exists. This helps the agent to "ride out" temporary failures in cloud APIs while still fetching fresh data when those APIs are functioning properly. Cached values do not expire.

Callers should instantiate one fetcher per piece of data required.

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(ctx context.Context) (string, error)

Fetch attempts to fetch the value, returning the result or the last successful value, or an error if no attempt has ever been successful. No special handling is included for the Context: both context.Cancelled and context.DeadlineExceeded are handled like any other error by returning the cached value.

This can be called from multiple goroutines, in which case it will call Attempt concurrently.

func (*Fetcher) Reset

func (f *Fetcher) Reset()

Reset resets the cached value (used for testing)

Jump to

Keyboard shortcuts

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