lifecycle

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 11 Imported by: 4

Documentation

Index

Constants

View Source
const (
	StateClosed = State(iota)
	StateStarting
	StateRunning
	StateClosing
)
View Source
const GroupFieldName = "$group"
View Source
const ListFieldName = "$list"
View Source
const RetryableFieldName = "$retryable"

Variables

View Source
var StateInfos = []StateInfo{
	{State: StateClosed, UpperName: "CLOSED"},
	{State: StateStarting, UpperName: "STARTING"},
	{State: StateRunning, UpperName: "RUNNING"},
	{State: StateClosing, UpperName: "CLOSING"},
}

Functions

func AddClosedFuture

func AddClosedFuture[FUTURE Future[error]](lifecycle Lifecycle, future FUTURE) FUTURE

func AddRunningFuture

func AddRunningFuture[FUTURE Future[error]](lifecycle Lifecycle, future FUTURE) FUTURE

func GetField

func GetField[V any](l Lifecycle, name string) (v V)

func InterrupterHoldRun

func InterrupterHoldRun(_ Lifecycle, interrupter chan struct{}) error

func RemoveField

func RemoveField[V any](l Lifecycle, name string) (v V)

func SetDefaultField

func SetDefaultField[V any](l Lifecycle, name string, defaultValue V) (value V, exist bool)

Types

type CallbackFuture

type CallbackFuture[V any] struct {
	// contains filtered or unexported fields
}

func NewCallbackFuture

func NewCallbackFuture[V any](callback func(value V)) *CallbackFuture[V]

func (CallbackFuture[V]) Complete

func (f CallbackFuture[V]) Complete(value V)

func (CallbackFuture[V]) SetCallback

func (f CallbackFuture[V]) SetCallback(callback func(value V))

type ChanFuture

type ChanFuture[V any] chan V

func (ChanFuture[V]) Complete

func (c ChanFuture[V]) Complete(value V)

func (ChanFuture[V]) Wait

func (c ChanFuture[V]) Wait() V

type CloseFunc

type CloseFunc = func(Lifecycle) error

type DefaultLifecycle

type DefaultLifecycle struct {
	State
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(options ...Option) *DefaultLifecycle

func NewWithInterruptedRun

func NewWithInterruptedRun(startFn InterruptedStartFunc, runFn InterruptedRunFunc, options ...Option) *DefaultLifecycle

func NewWithInterruptedRunner

func NewWithInterruptedRunner(runner InterruptedRunner, options ...Option) *DefaultLifecycle

func NewWithInterruptedStart

func NewWithInterruptedStart(starterFn InterruptedStarterFunc, options ...Option) *DefaultLifecycle

func NewWithInterruptedStarter

func NewWithInterruptedStarter(starter InterruptedStarter, options ...Option) *DefaultLifecycle

func NewWithRun

func NewWithRun(startFn StartFunc, runFn RunFunc, closeFn CloseFunc, options ...Option) *DefaultLifecycle

func NewWithRunner

func NewWithRunner(runner Runner, options ...Option) *DefaultLifecycle

func NewWithStart

func NewWithStart(starterFn StarterFunc, options ...Option) *DefaultLifecycle

func NewWithStarter

func NewWithStarter(starter Starter, options ...Option) *DefaultLifecycle

func (*DefaultLifecycle) AddClosedFuture

func (l *DefaultLifecycle) AddClosedFuture(future Future[error]) Future[error]

func (*DefaultLifecycle) AddStartedFuture

func (l *DefaultLifecycle) AddStartedFuture(future Future[error]) Future[error]

func (*DefaultLifecycle) Background

func (l *DefaultLifecycle) Background() error

func (*DefaultLifecycle) Close

func (l *DefaultLifecycle) Close(future Future[error]) error

func (*DefaultLifecycle) ClosedWaiter

func (l *DefaultLifecycle) ClosedWaiter() <-chan error

func (*DefaultLifecycle) DeleteField

func (l *DefaultLifecycle) DeleteField(name string) Lifecycle

func (*DefaultLifecycle) Error

func (l *DefaultLifecycle) Error() error

func (*DefaultLifecycle) Field

func (l *DefaultLifecycle) Field(name string) (value any)

func (*DefaultLifecycle) Fields

func (l *DefaultLifecycle) Fields() map[string]any

func (*DefaultLifecycle) OnClose

func (l *DefaultLifecycle) OnClose(onClose OnCloseFunc) Lifecycle

func (*DefaultLifecycle) OnClosed

func (l *DefaultLifecycle) OnClosed(onClosed OnClosedFunc) Lifecycle

func (*DefaultLifecycle) OnStarted

func (l *DefaultLifecycle) OnStarted(onStarted OnStartedFunc) Lifecycle

func (*DefaultLifecycle) OnStarting

func (l *DefaultLifecycle) OnStarting(onStarting OnStartingFunc) Lifecycle

func (*DefaultLifecycle) RangeField

func (l *DefaultLifecycle) RangeField(f func(name string, value any) bool) Lifecycle

func (*DefaultLifecycle) RemoveField

func (l *DefaultLifecycle) RemoveField(name string) (value any)

func (*DefaultLifecycle) Run

func (l *DefaultLifecycle) Run() error

func (*DefaultLifecycle) SetDefaultField

func (l *DefaultLifecycle) SetDefaultField(name string, defaultValue any) (value any, exist bool)

func (*DefaultLifecycle) SetField

func (l *DefaultLifecycle) SetField(name string, value any) Lifecycle

func (*DefaultLifecycle) SetOnClose

func (l *DefaultLifecycle) SetOnClose(onClose OnCloseFunc) Lifecycle

func (*DefaultLifecycle) SetOnClosed

func (l *DefaultLifecycle) SetOnClosed(onClosed OnClosedFunc) Lifecycle

func (*DefaultLifecycle) SetOnStarted

func (l *DefaultLifecycle) SetOnStarted(onStarted OnStartedFunc) Lifecycle

func (*DefaultLifecycle) SetOnStarting

func (l *DefaultLifecycle) SetOnStarting(onStarting OnStartingFunc) Lifecycle

func (*DefaultLifecycle) Shutdown

func (l *DefaultLifecycle) Shutdown() error

func (*DefaultLifecycle) Start

func (l *DefaultLifecycle) Start() error

func (*DefaultLifecycle) StartedWaiter

func (l *DefaultLifecycle) StartedWaiter() <-chan error

func (*DefaultLifecycle) String

func (l *DefaultLifecycle) String() string

type Future

type Future[V any] interface {
	Complete(value V)
}

type Futures

type Futures[V any] []Future[V]

func (Futures[V]) Complete

func (fs Futures[V]) Complete(value V)

type Group

type Group struct {
	Lifecycle
	// contains filtered or unexported fields
}

func NewGroup

func NewGroup() *Group

func (*Group) Add

func (g *Group) Add(name string, lifecycle Lifecycle) (*GroupLifecycleHolder, error)

func (*Group) MustAdd

func (g *Group) MustAdd(name string, lifecycle Lifecycle) *GroupLifecycleHolder

func (*Group) Remove

func (g *Group) Remove(name string) *GroupLifecycleHolder

type GroupLifecycleHolder

type GroupLifecycleHolder struct {
	Lifecycle
	// contains filtered or unexported fields
}

func (*GroupLifecycleHolder) Group

func (h *GroupLifecycleHolder) Group() *Group

func (*GroupLifecycleHolder) Name

func (h *GroupLifecycleHolder) Name() string

func (*GroupLifecycleHolder) SetCloseAllOnExit

func (h *GroupLifecycleHolder) SetCloseAllOnExit(enable bool) *GroupLifecycleHolder

func (*GroupLifecycleHolder) SetCloseAllOnExitError

func (h *GroupLifecycleHolder) SetCloseAllOnExitError(enable bool) *GroupLifecycleHolder

func (*GroupLifecycleHolder) SetCloseAllOnStartError

func (h *GroupLifecycleHolder) SetCloseAllOnStartError(enable bool) *GroupLifecycleHolder

type InterruptedError

type InterruptedError struct {
	Target string
	Action string
}

func NewInterruptedError

func NewInterruptedError(target string, action string) *InterruptedError

func (*InterruptedError) Error

func (e *InterruptedError) Error() string

type InterruptedRunFunc

type InterruptedRunFunc = func(lifecycle Lifecycle, interrupter chan struct{}) error

type InterruptedRunner

type InterruptedRunner interface {
	DoStart(lifecycle Lifecycle, interrupter chan struct{}) error

	DoRun(lifecycle Lifecycle, interrupter chan struct{}) error
}

func FuncInterruptedRunner

func FuncInterruptedRunner(startFn InterruptedStartFunc, runFn InterruptedRunFunc) InterruptedRunner

type InterruptedStartFunc

type InterruptedStartFunc = func(lifecycle Lifecycle, interrupter chan struct{}) error

type InterruptedStarter

type InterruptedStarter interface {
	DoStart(lifecycle Lifecycle, interrupter chan struct{}) (runFn InterruptedRunFunc, err error)
}

func FuncInterruptedStarter

func FuncInterruptedStarter(starterFn InterruptedStarterFunc) InterruptedStarter

type InterruptedStarterFunc

type InterruptedStarterFunc = func(lifecycle Lifecycle, interrupter chan struct{}) (runFn InterruptedRunFunc, err error)

type Lifecycle

type Lifecycle interface {
	Run() error

	Background() error

	Start() error

	StartedWaiter() <-chan error

	Close(future Future[error]) error

	ClosedWaiter() <-chan error

	Shutdown() error

	AddStartedFuture(future Future[error]) Future[error]

	AddClosedFuture(future Future[error]) Future[error]

	OnStarting(OnStartingFunc) Lifecycle

	OnStarted(OnStartedFunc) Lifecycle

	OnClose(OnCloseFunc) Lifecycle

	OnClosed(OnClosedFunc) Lifecycle

	SetOnStarting(OnStartingFunc) Lifecycle

	SetOnStarted(OnStartedFunc) Lifecycle

	SetOnClose(OnCloseFunc) Lifecycle

	SetOnClosed(OnClosedFunc) Lifecycle

	Field(name string) any

	Fields() map[string]any

	SetField(name string, value any) Lifecycle

	SetDefaultField(name string, defaultValue any) (value any, exist bool)

	DeleteField(name string) Lifecycle

	RemoveField(name string) any

	RangeField(f func(name string, value any) bool) Lifecycle

	Error() error
}

type List

type List struct {
	Lifecycle
	// contains filtered or unexported fields
}

func NewList

func NewList() *List

func (*List) Append

func (l *List) Append(lifecycle Lifecycle) (*ListLifecycleHolder, error)

func (*List) MustAppend

func (l *List) MustAppend(lifecycle Lifecycle) *ListLifecycleHolder

type ListLifecycleHolder

type ListLifecycleHolder struct {
	Lifecycle
	// contains filtered or unexported fields
}

func (*ListLifecycleHolder) List

func (h *ListLifecycleHolder) List() *List

func (*ListLifecycleHolder) SetCloseAllOnExit

func (h *ListLifecycleHolder) SetCloseAllOnExit(enable bool) *ListLifecycleHolder

func (*ListLifecycleHolder) SetCloseAllOnExitError

func (h *ListLifecycleHolder) SetCloseAllOnExitError(enable bool) *ListLifecycleHolder

func (*ListLifecycleHolder) SetCloseAllOnStartError

func (h *ListLifecycleHolder) SetCloseAllOnStartError(enable bool) *ListLifecycleHolder

func (*ListLifecycleHolder) SetCloseBackOnExit

func (h *ListLifecycleHolder) SetCloseBackOnExit(enable bool) *ListLifecycleHolder

func (*ListLifecycleHolder) SetCloseBackOnExitError

func (h *ListLifecycleHolder) SetCloseBackOnExitError(enable bool) *ListLifecycleHolder

func (*ListLifecycleHolder) SetStopStartOnStartError

func (h *ListLifecycleHolder) SetStopStartOnStartError(enable bool) *ListLifecycleHolder

type NopFuture

type NopFuture[V any] struct{}

func (NopFuture[V]) Complete

func (f NopFuture[V]) Complete(value V)

type OnCloseFunc

type OnCloseFunc = func(Lifecycle, error)

type OnClosedFunc

type OnClosedFunc = func(Lifecycle, error)

type OnStartedFunc

type OnStartedFunc = func(Lifecycle, error)

type OnStartingFunc

type OnStartingFunc = func(Lifecycle)

type Option

type Option interface {
	Apply(lifecycle Lifecycle)
}

func WithInterruptedRunner

func WithInterruptedRunner(runner InterruptedRunner) Option

func WithInterruptedStarter

func WithInterruptedStarter(starter InterruptedStarter) Option

func WithRunner

func WithRunner(runner Runner) Option

func WithSelf

func WithSelf(self any) Option

func WithStarter

func WithStarter(starter Starter) Option

type Retryable

type Retryable[LIFECYCLE Lifecycle] struct {
	Lifecycle
	// contains filtered or unexported fields
}

func NewRetryable

func NewRetryable[LIFECYCLE Lifecycle](lifecycle LIFECYCLE) *Retryable[LIFECYCLE]

func (*Retryable[LIFECYCLE]) Get

func (r *Retryable[LIFECYCLE]) Get() LIFECYCLE

func (*Retryable[LIFECYCLE]) SetLazyStart

func (r *Retryable[LIFECYCLE]) SetLazyStart(lazyStart bool) *Retryable[LIFECYCLE]

func (*Retryable[LIFECYCLE]) SetRetryInterval

func (r *Retryable[LIFECYCLE]) SetRetryInterval(interval time.Duration) *Retryable[LIFECYCLE]

type RunFunc

type RunFunc = func(Lifecycle) error

type Runner

type Runner interface {
	DoStart(Lifecycle) error

	DoRun(Lifecycle) error

	DoClose(Lifecycle) error
}

func FuncRunner

func FuncRunner(startFn, runFn, closeFn func(Lifecycle) error) Runner

type StartFunc

type StartFunc = func(Lifecycle) error

type Starter

type Starter interface {
	DoStart(Lifecycle) (runFn RunFunc, closeFn CloseFunc, err error)
}

func FuncStarter

func FuncStarter(starterFn StarterFunc) Starter

type StarterFunc

type StarterFunc func(Lifecycle) (runFn RunFunc, closeFn CloseFunc, err error)

type State

type State int

func (State) Closed

func (s State) Closed() bool

func (State) Closing

func (s State) Closing() bool

func (State) Running

func (s State) Running() bool

func (State) Starting

func (s State) Starting() bool

func (State) String

func (s State) String() string

func (*State) ToClosed

func (s *State) ToClosed()

func (*State) ToClosing

func (s *State) ToClosing()

func (*State) ToRunning

func (s *State) ToRunning()

func (*State) ToStarting

func (s *State) ToStarting()

type StateClosedError

type StateClosedError struct {
	Target string
}

func NewStateClosedError

func NewStateClosedError(target string) *StateClosedError

func (*StateClosedError) Error

func (e *StateClosedError) Error() string

type StateClosingError

type StateClosingError struct {
	Target string
}

func NewStateClosingError

func NewStateClosingError(target string) *StateClosingError

func (*StateClosingError) Error

func (e *StateClosingError) Error() string

type StateConflictError

type StateConflictError struct {
	Target   string
	State    string
	Conflict string
}

func NewStateConflictError

func NewStateConflictError(target string, state string, conflict string) *StateConflictError

func (*StateConflictError) Error

func (e *StateConflictError) Error() string

type StateDependencyError

type StateDependencyError struct {
	Target string
	State  string
	Depend string
}

func NewStateDependencyError

func NewStateDependencyError(target string, state string, depend string) *StateDependencyError

func (*StateDependencyError) Error

func (e *StateDependencyError) Error() string

type StateInfo

type StateInfo struct {
	State     State
	UpperName string
	LowerName string
}

type StateMustSwitchError

type StateMustSwitchError struct {
	Target string
	Form   string
	To     string
}

func NewStateMustSwitchError

func NewStateMustSwitchError(target string, from string, to string) *StateMustSwitchError

func (*StateMustSwitchError) Error

func (e *StateMustSwitchError) Error() string

type StateNotAllowSwitchError

type StateNotAllowSwitchError struct {
	Target string
	Form   string
	To     string
}

func NewStateNotAllowSwitchError

func NewStateNotAllowSwitchError(target string, from string, to string) *StateNotAllowSwitchError

func (*StateNotAllowSwitchError) Error

func (e *StateNotAllowSwitchError) Error() string

type StateNotClosedError

type StateNotClosedError struct {
	Target string
}

func NewStateNotClosedError

func NewStateNotClosedError(target string) *StateNotClosedError

func (*StateNotClosedError) Error

func (e *StateNotClosedError) Error() string

type StateNotRunningError

type StateNotRunningError struct {
	Target string
}

func NewStateNotRunningError

func NewStateNotRunningError(target string) *StateNotRunningError

func (*StateNotRunningError) Error

func (e *StateNotRunningError) Error() string

type StateRunningError

type StateRunningError struct {
	Target string
}

func NewStateRunningError

func NewStateRunningError(target string) *StateRunningError

func (*StateRunningError) Error

func (e *StateRunningError) Error() string

type StateStartingError

type StateStartingError struct {
	Target string
}

func NewStateStartingError

func NewStateStartingError(target string) *StateStartingError

func (*StateStartingError) Error

func (e *StateStartingError) Error() string

type SyncFutures

type SyncFutures[V any] struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewSyncFutures

func NewSyncFutures[V any]() *SyncFutures[V]

func (*SyncFutures[V]) Append

func (f *SyncFutures[V]) Append(future Future[V])

func (*SyncFutures[V]) LoadAndReset

func (f *SyncFutures[V]) LoadAndReset() Futures[V]

type UnknownStateError

type UnknownStateError struct {
	Target string
	State  State
}

func NewUnknownStateError

func NewUnknownStateError(target string, state State) *UnknownStateError

func (*UnknownStateError) Error

func (e *UnknownStateError) Error() string

type WaitableFuture

type WaitableFuture[V any] interface {
	Future[V]

	Wait() V
}

type WaiterFuture

type WaiterFuture[V any] struct {
	// contains filtered or unexported fields
}

func NewWaiterFuture

func NewWaiterFuture[V any]() *WaiterFuture[V]

func (*WaiterFuture[V]) Complete

func (w *WaiterFuture[V]) Complete(value V)

func (*WaiterFuture[V]) Wait

func (w *WaiterFuture[V]) Wait() V

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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