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
// 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
}
Click to show internal directories.
Click to hide internal directories.