stoppable

package
v4.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: BSD-2-Clause, BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMsg = "stoppable %q is not running, exiting %s early " + errKey
)

Error message returned after a comms operations ends and finds that its parent thread is stopping or stopped.

Variables

This section is empty.

Functions

func CheckErr

func CheckErr(err error) bool

CheckErr returns true if the error contains a stoppable error message. This function is used by callers to determine if a sub function quit due to a stoppable closing and tells the caller to exit.

func WaitForStopped

func WaitForStopped(s Stoppable, timeout time.Duration) error

WaitForStopped polls the stoppable and all its children to see if they are stopped. Returns an error if its times out waiting for all children to stop.

Types

type Multi

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

func NewMulti

func NewMulti(name string) *Multi

NewMulti returns a new multi Stoppable.

func (*Multi) Add

func (m *Multi) Add(stoppable Stoppable)

Add adds the given Stoppable to the list of stoppables.

func (*Multi) Close

func (m *Multi) Close() error

Close issues a close signal to all child stoppables and marks the status of the Multi Stoppable as stopping. Returns an error if one or more child stoppables failed to close, but it does not return their specific errors and assumes they print them to the log.

func (*Multi) GetRunningProcesses

func (m *Multi) GetRunningProcesses() []string

GetRunningProcesses returns the names of all running processes at the time of this call. Note that this list may change and is subject to race conditions if multiple threads are in the process of starting or stopping.

func (*Multi) GetStatus

func (m *Multi) GetStatus() Status

GetStatus returns the lowest status of all of the Stoppable children. The status is not the status of all Stoppables, but the status of the Stoppable with the lowest status.

func (*Multi) IsRunning

func (m *Multi) IsRunning() bool

IsRunning returns true if Stoppable is marked as running.

func (*Multi) IsStopped

func (m *Multi) IsStopped() bool

IsStopped returns true if Stoppable is marked as stopped.

func (*Multi) IsStopping

func (m *Multi) IsStopping() bool

IsStopping returns true if Stoppable is marked as stopping.

func (*Multi) Name

func (m *Multi) Name() string

Name returns the name of the Multi Stoppable and the names of all stoppables it contains.

type Single

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

Single allows stopping a single goroutine using a channel. It adheres to the Stoppable interface.

func NewSingle

func NewSingle(name string) *Single

NewSingle returns a new single Stoppable.

func (*Single) Close

func (s *Single) Close() error

Close signals the Single to close via the quit channel. Returns an error if the status of the Single is not Running.

func (*Single) GetStatus

func (s *Single) GetStatus() Status

GetStatus returns the status of the Stoppable.

func (*Single) IsRunning

func (s *Single) IsRunning() bool

IsRunning returns true if Stoppable is marked as running.

func (*Single) IsStopped

func (s *Single) IsStopped() bool

IsStopped returns true if Stoppable is marked as stopped.

func (*Single) IsStopping

func (s *Single) IsStopping() bool

IsStopping returns true if Stoppable is marked as stopping.

func (*Single) Name

func (s *Single) Name() string

Name returns the name of the Single Stoppable.

func (*Single) Quit

func (s *Single) Quit() <-chan struct{}

Quit returns a receive-only channel that will be triggered when the Stoppable quits.

func (*Single) ToStopped

func (s *Single) ToStopped()

ToStopped changes the status from stopping to stopped. Panics if the status is not already set to stopping.

type Status

type Status uint32

Status holds the current status of a Stoppable.

const (
	Running Status = iota
	Stopping
	Stopped
)

func (Status) String

func (s Status) String() string

String prints a string representation of the current Status. This functions satisfies the fmt.Stringer interface.

type Stoppable

type Stoppable interface {
	// Name returns the name of the Stoppable.
	Name() string

	// GetStatus returns the status of the Stoppable.
	GetStatus() Status

	// IsRunning returns true if the Stoppable is running.
	IsRunning() bool

	// IsStopping returns true if Stoppable is marked as stopping.
	IsStopping() bool

	// IsStopped returns true if Stoppable is marked as stopped.
	IsStopped() bool

	// Close marks the Stoppable as stopping and issues a close signal to the
	// Stoppable or any children it may have.
	Close() error
}

Stoppable interface for stopping a goroutine. All functions are thread safe.

Jump to

Keyboard shortcuts

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