lcm

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: Apache-2.0 Imports: 8 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, Init any] func(Key, Status[Init]) (Object, error)

BuildFunc can build a managed object, or fail immediately.

type Manager

type Manager[Key comparable, Object, Init 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, Init) (Object, context.Context, error)
}

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

func New

func New[Key comparable, Object, Init any](
	build BuildFunc[Key, Object, Init],
	options ...Option,
) Manager[Key, Object, Init]

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, Init any](
	ctx context.Context,
	build BuildFunc[Key, Object, Init],
	options ...Option,
) Manager[Key, Object, Init]

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 Option added in v0.1.22

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

Option can be applied when building a new Manager.

func TimeoutOption added in v0.1.22

func TimeoutOption(t time.Duration) *Option

TimeoutOption makes the manager shutdown a given duration after the last context has been cancelled.

type Status added in v0.1.18

type Status[Init any] interface {
	Context() context.Context

	// JoinTask registers a join function to handle join lifecycle.
	// This is run in its own goroutine and blocks shutdown until completion; a normal use is to wait on [context.Context.Done] and do cleanup tasks once done.
	// All registered methods may run in parallel.
	// Notably this ensures the managed object stays alive during context shutdown: [context.AfterFunc] doesn't do this on its own.
	JoinTask(func(context.Context, Init) error)

	// Task creates an immediate task that is started in a goroutine after the object is successfully created.
	// It will be deferred until the builder function first returns (or not called if this errors).
	// If/when it returns a non-nil error, this managed object will be cancelled.
	// After cleanups wait for all tasks to return before starting.
	Task(TaskFunc)

	// 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
}

type TaskFunc added in v0.1.19

type TaskFunc func(stop <-chan struct{}) error

TaskFunc runs for the overall runtime of the status object.

Jump to

Keyboard shortcuts

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