Package-Level Type Names (total 5, in which 4 are exported)
/* sort exporteds by: | */
Type Parameters:
K: comparable
V: any
Iterator iterates over key and value pairs in the Table. It follows the
semantics of the standard sql.Rows type and does not necessarily correspond
to any consistent snapshot of the Table’s contents.
Close closes the iterator.
Err returns any error that occurred during iteration.
Get returns the key and value for the current iteration.
Next prepares the value for the next iteration. It returns true on
success, and false if there is no next value. Consult Err to check
whether iterator successfully reached the end or an error occurred.
Iterator : io.Closer
func Table[K, V].Iter(ctx context.Context) (Iterator[K, V], error)
Options is a set of options for supervisor constructor.
Clock is the clock to use. Defaults to system clock.
setDefaults sets default values for unspecified options.
func NewSupervisor[K, P](t Table[K, P], o Options) *Supervisor[K, P]
Type Parameters:
K: comparable
P: process.Runnable
Supervisor is responsible for starting, stopping, and monitoring its child
processes.
clock*clock.Clock
parent is the parent context for all processes. It is set to the
context passed to Run method and is guarded by startMu.
processes is a map of managed processes. It is used to track process
state and allows returning an ErrProcessExists error to guarantee
that at most one processes is active per key.
runLock ensures that at most one Run method is executing at a time.
startbool
startMu guards startProcess and startProcessForKey calls when
Supervisor is not running. It also allows waiting for the ongoing
calls to complete on shutdown.
tableTable[K, P]
wg is the wait group for running processes and watchdogs. It is
indirectly guarded by startMu and start.
Get returns a running process or either a ErrProcessNotFound error if the
process does not exist or ErrProcessNotRunning is the process exists but is
not running.
Keys returns a list of all process keys.
Run starts the supervisor and executes callback on successful initialization.
Start starts the process with the given key from the table. It returns
ErrNotRunning if the supervisor is not running and ErrProcessExists if the
process already exists. Otherwise it returns an error from process
initialization.
Stop stops the process with the given key. It returns ErrProcessNotFound if
the process does not exist, and an error from running the process otherwise.
restart starts processes from the table that are not currently running.
If wait duration is not zero, it waits until background startProcess calls
complete. This allows ensuring that we restore at least partial state in
restoreInitial before invoking Supervisor’s Run callback.
restartInitial restores the last state from the underlying table.
restartLoop runs a loop that calls restart.
restartProcessInBackground starts the process for the given key in the
background. It returns a channel that is closed when startProcess call
completes.
spawnRestartLoop spawns a restartLoop using the given context and returns
a function that stops restart loop and waits completion.
startProcess starts the process for the given key. Unlike startProcessForKey,
it uses the given r Runnable instance instead of getting it from the table.
startProcessForKey starts the process for the given key. An error is returned
if Supervisor’s Run method is not currently running.
startProcessUnlocked starts the given process assuming that the start lock
was acquired and an entry in the processes map exists. It removes this entry
on error.
stopAll stops all the processes in the underlying map. It does not wait for
processes to complete the termination.
stopProcess stops the process with the given key. If the processes does not
exist, it returns ErrProcessNotFound.
watchdog waits for process shutdown and removes it from processes map on such
event.
*Supervisor : go.pact.im/x/process.Runnable
func NewSupervisor[K, P](t Table[K, P], o Options) *Supervisor[K, P]
Type Parameters:
K: comparable
V: any
Table defines a table of key and value pairs that is potentially backed by a
persistent and shared storage.
Get returns the value for the given key.
Iter returns an iterator for key and value pairs in the table.
func NewSupervisor[K, P](t Table[K, P], o Options) *Supervisor[K, P]
Type Parameters:
P: process.Runnable
managedProcess contains a process.Process and associated process.Runnable
managed by Supervisor.
Process*process.Process
proc is the underlying process instance with parametrized P type.
Process.cancelcontext.CancelFuncProcess.donechan struct{}Process.erratomic.ErrorProcess.parentcontext.ContextProcess.procprocess.RunnableProcess.stateprocess.StateProcess.stateMusync.MutexProcess.stopchan struct{}
stopped is used by Supervisor to remove the process instance from
internal map at most once.
Done returns a channel that is closed when process terminates.
Err returns the error from running the process.
Start starts the process or cancels the underlying process context on error.
The startup deadline may be set using the given ctx context. Note that the
context would not be used by process directly so the associated values are
not propagated.
It returns ErrProcessInvalidState if the process is not in the initial state.
State returns the current process state.
Stop stops the process by returning from the Run method callback and waiting
for the termination.
The shutdown deadline may be set using the given ctx context. If the deadline
is exceeded, underlying context is canceled, signaling a forced shutdown to
the process.
It returns ErrProcessInvalidState if the process was not started or has
already been stopped.
transition advances to the next process state. It returns false if there is
not transition from the current to the given next state.
func (*Supervisor)[K, P].startProcess(ctx context.Context, pk K, r P) (*managedProcess[P], error)
func (*Supervisor)[K, P].startProcessForKey(ctx context.Context, pk K) (*managedProcess[P], error)
func (*Supervisor)[K, P].startProcessUnlocked(ctx context.Context, pk K, r P) (*managedProcess[P], error)
func (*Supervisor)[K, P].watchdog(pk K, p *managedProcess[P])
Package-Level Functions (only one, which is exported)
Type Parameters:
K: comparable
P: process.Runnable
NewSupervisor returns a new Supervisor instance. The given table is used to
lookup managed processes and periodically restart failed units (or processes
that were added externally). Managed processes are uniquely identifiable by
key.
A managed process may remove itself from the Supervisor by deleting the
associated entry from the table before terminating. Likewise, to stop a
process, it must be removed from the table prior to Stop call. That is,
processes must be aware of being managed and the removal is tighly coupled
with the table.
As a rule of thumb, to keep the underlying table consistent, processes should
not be re-added to table after being removed from the table. It is possible
to implement re-adding on top of the Supervisor but that requires handling
possible orderings of table removal, addition, re-addition and process
startup, shutdown and self-removal (or a subset of these operations depending
on the use cases).
Package-Level Variables (total 4, all are exported)
ErrNotRunning is an error that is returned when a process
supervisor temporarily unavailable (i.e. it is not running).
ErrProcessExists is an error that is returned if the process already
exists for the given ID.
ErrProcessNotFound is an error that is returned if the requested
process was not found.
ErrProcessNotRunning is an error that is returned when a process is
not running (i.e. exists but is in the starting state).
Package-Level Constants (total 3, none are exported)