lifetime

package
v0.0.2-0...-993051b Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2024 License: Apache-2.0 Imports: 4 Imported by: 1

Documentation

Overview

package lifetime provides common component lifetime control logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsWorking

func IsWorking(state State) error

func NotStopped

func NotStopped(state State) error

Types

type BiState

type BiState int32

BiState provides pre-defined simple binary state - normal or closed.

const (
	Normal BiState = 0
	Closed BiState = 1
)

type CheckHealth

type CheckHealth[T any] func(T) error

CheckHealth function type for lifetime healthy check.

type Lifetime

type Lifetime[T any] interface {
	SafeChan
	// SetState is the method to change lifetime state.
	SetState(state T)
	// GetState returns current state.
	GetState() T
	// Add records a task is running, returns false if the lifetime is not healthy.
	Add(isHealthy CheckHealth[T]) error
	// Done records a task is done.
	Done()
	// Wait waits until all tasks are done.
	Wait()
}

Lifetime interface for lifetime control.

func NewLifetime

func NewLifetime[T any](initState T) Lifetime[T]

NewLifetime returns a new instance of Lifetime with init state and isHealthy logic.

type SafeChan

type SafeChan interface {
	IsClosed() bool
	CloseCh() <-chan struct{}
	Close()
}

SafeChan is the utility type combining chan struct{} & sync.Once. It provides double close protection internally.

func NewSafeChan

func NewSafeChan() SafeChan

NewSafeChan returns a SafeChan with internal channel initialized

type Signal

type Signal chan struct{}

Singal alias for chan struct{}.

type State

type State int32

State provides pre-defined three stage state.

const (
	Initializing State = iota
	Working
	Stopped
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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