lockset

package
v0.0.0-...-4dcfcdd Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package lockset contains a utility type that orders access to multiple resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wait

func Wait(ctx context.Context, outcomes []Outcome) error

Wait returns the first non-nil error.

Types

type Callback

type Callback[K any] func(keys []K) error

A Callback is provided to Set.Schedule.

type Outcome

type Outcome = *notify.Var[*Status]

Outcome is a convenience type alias.

func NewOutcome

func NewOutcome() Outcome

NewOutcome is a convenience method to allocate an Outcome.

type RetryAtHeadErr

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

RetryAtHeadErr is returned by RetryAtHead.

func RetryAtHead

func RetryAtHead(cause error) *RetryAtHeadErr

RetryAtHead returns an error that tasks can use to be retried later, once all preceding tasks have completed. If this error is returned when there are no preceding tasks, the causal error will be emitted from Set.Schedule.

func (*RetryAtHeadErr) Error

func (e *RetryAtHeadErr) Error() string

Error returns a message.

func (*RetryAtHeadErr) Or

func (e *RetryAtHeadErr) Or(fn func()) *RetryAtHeadErr

Or sets a fallback function to invoke if the task was already at the head of the global queue. This is used if a cleanup task must be run if the task is not going to be retried. The receiver is returned.

func (*RetryAtHeadErr) Unwrap

func (e *RetryAtHeadErr) Unwrap() error

Unwrap returns the causal error passed to RetryAtHead.

type Runner

type Runner interface {
	// Go should execute the function in a non-blocking fashion.
	Go(func(context.Context)) error
}

A Runner is passed to New to begin the execution of tasks.

func GoRunner

func GoRunner(ctx context.Context) Runner

GoRunner returns a Runner that executes tasks using the go keyword and the specified context.

type Set

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

Set implements an in-order admission queue for actors requiring exclusive access to a set of keys.

A Set is internally synchronized and is safe for concurrent use. A Set should not be copied after it has been created.

func New

func New[K comparable](runner Runner, metricsLabel string) (*Set[K], error)

New construct a Set that executes tasks using the given runner.

See GoRunner

func (*Set[K]) Schedule

func (s *Set[K]) Schedule(keys []K, fn Callback[K]) (outcome Outcome, cancel func())

Schedule executes the Callback once all keys have been locked. The result from the callback is available through the returned variable.

Callbacks that need to be retried may return RetryAtHead. This will execute the callback again when all other callbacks scheduled before it have been completed. A retrying callback will continue to hold its key locks until the retry has taken place.

It is valid to call this method with an empty key slice. The callback will simply be executed in a separate goroutine.

The provided key slice will be deduplicated to avoid a callback from deadlocking with itself.

Callbacks must not schedule new tasks and proceed to wait upon them. This will lead to deadlocks.

The cancel function may be called to asynchronously dequeue and cancel the callback. If the callback has already started executing, the cancel callback will have no effect.

type Status

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

Status is returned by Set.Schedule.

func StatusFor

func StatusFor(err error) *Status

StatusFor constructs a successful status if err is null. Otherwise, it returns a new Status object that returns the error.

func (*Status) Completed

func (s *Status) Completed() bool

Completed returns true if the callback has been called. See also Status.Success.

func (*Status) Err

func (s *Status) Err() error

Err returns any error returned by the Callback.

func (*Status) Executing

func (s *Status) Executing() bool

Executing returns true if the Callback is currently executing.

func (*Status) Queued

func (s *Status) Queued() bool

Queued returns true if the Callback has not been executed yet.

func (*Status) Retrying

func (s *Status) Retrying() bool

Retrying returns true if the callback returned RetryAtHead and it has not yet been re-attempted.

func (*Status) String

func (s *Status) String() string

func (*Status) Success

func (s *Status) Success() bool

Success returns true if the Status represents the successful completion of a scheduled waiter.

Jump to

Keyboard shortcuts

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