lcm

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildFunc added in v0.1.18

type BuildFunc[Key comparable, Object any] func(Key, Status) (Object, error)

BuildFunc can build a managed object, or fail immediately.

type GroupTimer

type GroupTimer interface {
	// Join returns true if a change was made, or false if the timer had already expired.
	Join(context.Context) bool
	// Done returns a channel which is closed when the the last context is done and the timer has expired.
	Done() <-chan struct{}
}

func NewGroupTimer

func NewGroupTimer(t time.Duration, ctx context.Context) GroupTimer

NewGroupTimer builds a timer that expires in the passed duration after the last context joined is done.

type Manager

type Manager[Key comparable, Object any] interface {
	// Run creates/joins the lifecycle of an object with the given key.
	// Returns an error if it could not be created - this is "sticky" while interested.
	// Otherwise, returns the object and its run context (detached).
	Run(context.Context, Key) (Object, context.Context, error)

	// SetTimeout sets the future timeout, once no callers are interested, for objects created here.
	SetTimeout(time.Duration)
}

Manager is a "life-cycle manager", which allows keyed creation -> use -> shutdown.

func New

func New[Key comparable, Object any](build BuildFunc[Key, Object]) Manager[Key, Object]

New returns a new Manager that manages the lifecycle of lazily-created objects.

func NewWithContext added in v0.1.18

func NewWithContext[Key comparable, Object any](ctx context.Context, build BuildFunc[Key, Object]) Manager[Key, Object]

New returns a new Manager that manages the lifecycle of lazily-created objects. The passed initial context should normally be context.Background, as it is used as the parent of all lazily-created objects. It could be something else if you wanted to be able to cancel all objects at once.

type Status added in v0.1.18

type Status interface {
	Context() context.Context

	// After registers a shutdown function to run after this Status completes.
	// These functions will only be called in normal shutdown; if the runnable [context.Context] is cancelled, they will not be called.
	// Additionally, if any function returns a non-nil error, no further shutdown functions will run.
	// The returned stop function has the same semantics as [context.AfterFunc].
	// The passed function will block a manager recreating the managed object; be sure not to deadlock.
	After(func() error) (stop func() bool)

	// Check passes through the given error. If it is non-nil, it cancels this managed object.
	Check(error) error

	// CheckWrap passes through the given error. If it is non-nil, it cancels this managed object with a wrapped error ("%s: %w").
	CheckWrap(string, error) error
}

Jump to

Keyboard shortcuts

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