Documentation ¶
Overview ¶
Package gronos provides a concurrent application management system.
Package gronos provides a concurrent application management system.
Index ¶
- Variables
- func Merge[K comparable](apps ...map[K]RuntimeApplication) map[K]RuntimeApplication
- func New[K comparable](ctx context.Context, init map[K]RuntimeApplication, opts ...Option[K]) (*gronos[K], chan error)
- func UseBus(ctx context.Context) (func(m Message) bool, error)
- func UseBusConfirm(ctx context.Context) (func(fn FnConfirm) <-chan bool, error)
- func UseBusWait(ctx context.Context) (func(fn FnWait) <-chan struct{}, error)
- func WhenState(state StatusState) addOption
- func WithFinalTick() tickerOption
- type AddMessage
- type CancellableStateTask
- type CancellableTask
- type CancelledShutdown
- type CheckAutomaticShutdown
- type Clock
- type ClockOption
- type ConcurrentArray
- type Destroy
- type Envelope
- type ErroredShutdown
- type ExecutionMode
- type Extension
- type FnConfirm
- type FnWait
- type ForceCancelShutdown
- type ForceTerminateShutdown
- type GetListRuntimeApplication
- type GracePeriodExceededMessage
- type InitiateContextCancellation
- type InitiateShutdown
- type IteratorOption
- type IteratorStateOption
- type KeyMessage
- type LoopableIterator
- type LoopableIteratorOption
- func WithAfterLoop(afterLoop func(ctx context.Context) error) LoopableIteratorOption
- func WithBeforeLoop(beforeLoop func(ctx context.Context) error) LoopableIteratorOption
- func WithExtraCancel(cancels ...context.CancelFunc) LoopableIteratorOption
- func WithOnError(handler func(error) error) LoopableIteratorOption
- func WithOnInit(onInit func(ctx context.Context) (context.Context, error)) LoopableIteratorOption
- func WithShouldStop(shouldStop func(error) bool) LoopableIteratorOption
- type LoopableIteratorState
- type LoopableIteratorStateOption
- func WithAfterLoopState[T any](afterLoop func(ctx context.Context, state *T) error) LoopableIteratorStateOption[T]
- func WithBeforeLoopState[T any](beforeLoop func(ctx context.Context, state *T) error) LoopableIteratorStateOption[T]
- func WithExtraCancelState[T any](cancels ...context.CancelFunc) LoopableIteratorStateOption[T]
- func WithOnErrorState[T any](handler func(error) error) LoopableIteratorStateOption[T]
- func WithOnInitState[T any](onInit func(ctx context.Context, state *T) (context.Context, error)) LoopableIteratorStateOption[T]
- func WithShouldStopState[T any](shouldStop func(error) bool) LoopableIteratorStateOption[T]
- type Message
- type MessagePayload
- type Middleware
- type Option
- func WithExtension[K comparable](ext Extension[K]) Option[K]
- func WithGracePeriod[K comparable](period time.Duration) Option[K]
- func WithImmediatePeriod[K comparable](period time.Duration) Option[K]
- func WithMinRuntime[K comparable](duration time.Duration) Option[K]
- func WithShutdownBehavior[K comparable](behavior ShutdownBehavior) Option[K]
- func WithWait[K comparable]() Option[K]
- func WithoutGracePeriod[K comparable]() Option[K]
- func WithoutImmediatePeriod[K comparable]() Option[K]
- func WithoutMinRuntime[K comparable]() Option[K]
- type PanickedShutdown
- type RemoveMessage
- type RequestAlive
- type RequestAllAlive
- type RequestMessage
- type RequestReason
- type RequestStatus
- type RequestStatusAsync
- type RuntimeApplication
- type RuntimeError
- type ShutdownBehavior
- type ShutdownComplete
- type ShutdownKind
- type ShutdownProgress
- type StatusState
- type TerminatedShutdown
- type Ticker
- type TickerSubscriber
- type TickingRuntime
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnhandledMessage = errors.New("unhandled message") ErrUnmanageExtensionMessage = errors.New("unmanage extension message") ErrPanic = errors.New("panic") )
var ErrLoopCritical = errors.New("critical error")
Functions ¶
func Merge ¶
func Merge[K comparable](apps ...map[K]RuntimeApplication) map[K]RuntimeApplication
func New ¶
func New[K comparable](ctx context.Context, init map[K]RuntimeApplication, opts ...Option[K]) (*gronos[K], chan error)
New creates a new gronos instance with the given context and initial applications.
func UseBusConfirm ¶
func WhenState ¶
func WhenState(state StatusState) addOption
func WithFinalTick ¶
func WithFinalTick() tickerOption
Types ¶
type AddMessage ¶
type AddMessage[K comparable] struct { KeyMessage[K] RuntimeApplication RequestMessage[K, struct{}] }
func MsgAdd ¶
func MsgAdd[K comparable](key K, app RuntimeApplication) (<-chan struct{}, *AddMessage[K])
type CancellableStateTask ¶
CancellableStateTask represents a task that can be cancelled and operates on a state
type CancellableTask ¶
type CancelledShutdown ¶
type CancelledShutdown[K comparable] struct { KeyMessage[K] Error error RequestMessage[K, struct{}] }
type CheckAutomaticShutdown ¶
type CheckAutomaticShutdown[K comparable] struct { RequestMessage[K, struct{}] }
func MsgCheckAutomaticShutdown ¶
func MsgCheckAutomaticShutdown[K comparable]() (chan struct{}, *CheckAutomaticShutdown[K])
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock represents a clock that can manage multiple tickers with different execution modes.
func NewClock ¶
func NewClock(opts ...ClockOption) *Clock
NewClock creates a new Clock instance with the given options.
Example usage:
clock := NewClock( WithName("MyClock"), WithInterval(time.Second), )
func (*Clock) Add ¶
func (c *Clock) Add(ticker Ticker, mode ExecutionMode)
Add subscribes a Ticker to the Clock with the specified ExecutionMode.
Example usage:
clock.Add(&MyTicker{}, NonBlocking)
type ClockOption ¶
type ClockOption func(*Clock)
ClockOption is a function type for configuring a Clock instance.
func WithInterval ¶
func WithInterval(interval time.Duration) ClockOption
WithInterval sets the tick interval of the Clock.
type ConcurrentArray ¶
type ConcurrentArray[T comparable] struct { // contains filtered or unexported fields }
ConcurrentArray represents a thread-safe array of comparable elements
func (*ConcurrentArray[T]) Append ¶
func (ca *ConcurrentArray[T]) Append(item T)
Append adds an element to the array
func (*ConcurrentArray[T]) Contains ¶
func (ca *ConcurrentArray[T]) Contains(item T) bool
Contains checks if an item exists in the array
func (*ConcurrentArray[T]) Get ¶
func (ca *ConcurrentArray[T]) Get(index int) (T, bool)
Get retrieves an element at a specific index
func (*ConcurrentArray[T]) Length ¶
func (ca *ConcurrentArray[T]) Length() int
Length returns the current length of the array
func (*ConcurrentArray[T]) Set ¶
func (ca *ConcurrentArray[T]) Set(index int, item T) bool
Set updates an element at a specific index
type Destroy ¶
type Destroy[K comparable] struct{}
func MsgDestroy ¶
func MsgDestroy[K comparable]() *Destroy[K]
type Envelope ¶
type Envelope[K comparable] struct { From K Message }
type ErroredShutdown ¶
type ErroredShutdown[K comparable] struct { KeyMessage[K] Error error RequestMessage[K, struct{}] }
type ExecutionMode ¶
type ExecutionMode int
ExecutionMode defines how a ticker should be executed.
const ( // NonBlocking mode executes the ticker without waiting for completion. NonBlocking ExecutionMode = iota // ManagedTimeline mode ensures tickers are executed in order, potentially delaying subsequent ticks. ManagedTimeline // BestEffort mode attempts to execute tickers on time but may skip ticks if the system is overloaded. BestEffort )
type Extension ¶
type Extension[K comparable] interface { OnStart(ctx context.Context, errChan chan<- error) error OnStop(ctx context.Context, errChan chan<- error) error OnNewRuntime(ctx context.Context) context.Context OnStopRuntime(ctx context.Context) context.Context OnMsg(ctx context.Context, m *MessagePayload) error }
Extension defines the interface for Gronos extensions
type ForceCancelShutdown ¶
type ForceCancelShutdown[K comparable] struct { KeyMessage[K] Error error RequestMessage[K, struct{}] }
global system force cancel
func MsgForceCancelShutdown ¶
func MsgForceCancelShutdown[K comparable](key K, err error) (<-chan struct{}, *ForceCancelShutdown[K])
type ForceTerminateShutdown ¶
type ForceTerminateShutdown[K comparable] struct { KeyMessage[K] RequestMessage[K, struct{}] }
global system force terminate
func MsgForceTerminateShutdown ¶
func MsgForceTerminateShutdown[K comparable](key K) (<-chan struct{}, *ForceTerminateShutdown[K])
type GetListRuntimeApplication ¶
type GetListRuntimeApplication[K comparable] struct { RequestMessage[K, []K] }
func MsgGetListRuntimeApplication ¶
func MsgGetListRuntimeApplication[K comparable]() (<-chan []K, *GetListRuntimeApplication[K])
type GracePeriodExceededMessage ¶
type GracePeriodExceededMessage[K comparable] struct { KeyMessage[K] }
func MsgGracePeriodExceeded ¶
func MsgGracePeriodExceeded[K comparable]() *GracePeriodExceededMessage[K]
type InitiateContextCancellation ¶
type InitiateContextCancellation[K comparable] struct{}
func MsgInitiateContextCancellation ¶
func MsgInitiateContextCancellation[K comparable]() *InitiateContextCancellation[K]
type InitiateShutdown ¶
type InitiateShutdown[K comparable] struct{}
func MsgInitiateShutdown ¶
func MsgInitiateShutdown[K comparable]() *InitiateShutdown[K]
Message creation functions
type IteratorOption ¶
type IteratorOption func(*iteratorConfig)
IteratorOption is a function type for configuring the Iterator middleware.
func WithLoopableIteratorOptions ¶
func WithLoopableIteratorOptions(opts ...LoopableIteratorOption) IteratorOption
WithLoopableIteratorOptions adds LoopableIteratorOptions to the Iterator middleware.
type IteratorStateOption ¶
type IteratorStateOption[T any] func(*iteratorStateConfig[T])
IteratorStateOption is a function type for configuring the IteratorState
func WithInitialState ¶
func WithInitialState[T any](state *T) IteratorStateOption[T]
WithInitialState sets the initial state for the IteratorState
func WithLoopableIteratorStateOptions ¶
func WithLoopableIteratorStateOptions[T any](opts ...LoopableIteratorStateOption[T]) IteratorStateOption[T]
WithLoopableIteratorStateOptions adds LoopableIteratorStateOptions to the IteratorState
type LoopableIterator ¶
type LoopableIterator struct {
// contains filtered or unexported fields
}
func NewLoopableIterator ¶
func NewLoopableIterator(tasks []CancellableTask, opts ...LoopableIteratorOption) *LoopableIterator
func (*LoopableIterator) Cancel ¶
func (li *LoopableIterator) Cancel()
func (*LoopableIterator) Stop ¶
func (li *LoopableIterator) Stop()
func (*LoopableIterator) Wait ¶
func (li *LoopableIterator) Wait()
type LoopableIteratorOption ¶
type LoopableIteratorOption func(*LoopableIterator)
func WithAfterLoop ¶
func WithAfterLoop(afterLoop func(ctx context.Context) error) LoopableIteratorOption
func WithBeforeLoop ¶
func WithBeforeLoop(beforeLoop func(ctx context.Context) error) LoopableIteratorOption
func WithExtraCancel ¶
func WithExtraCancel(cancels ...context.CancelFunc) LoopableIteratorOption
func WithOnError ¶
func WithOnError(handler func(error) error) LoopableIteratorOption
func WithOnInit ¶
func WithShouldStop ¶
func WithShouldStop(shouldStop func(error) bool) LoopableIteratorOption
type LoopableIteratorState ¶
type LoopableIteratorState[T any] struct { // contains filtered or unexported fields }
LoopableIteratorState is similar to LoopableIterator but with a shared state
func NewLoopableIteratorState ¶
func NewLoopableIteratorState[T any](tasks []CancellableStateTask[T], state *T, opts ...LoopableIteratorStateOption[T]) *LoopableIteratorState[T]
func (*LoopableIteratorState[T]) Cancel ¶
func (li *LoopableIteratorState[T]) Cancel()
func (*LoopableIteratorState[T]) Run ¶
func (li *LoopableIteratorState[T]) Run(ctx context.Context) chan error
func (*LoopableIteratorState[T]) Stop ¶
func (li *LoopableIteratorState[T]) Stop()
func (*LoopableIteratorState[T]) Wait ¶
func (li *LoopableIteratorState[T]) Wait()
type LoopableIteratorStateOption ¶
type LoopableIteratorStateOption[T any] func(*LoopableIteratorState[T])
func WithAfterLoopState ¶
func WithAfterLoopState[T any](afterLoop func(ctx context.Context, state *T) error) LoopableIteratorStateOption[T]
func WithBeforeLoopState ¶
func WithBeforeLoopState[T any](beforeLoop func(ctx context.Context, state *T) error) LoopableIteratorStateOption[T]
func WithExtraCancelState ¶
func WithExtraCancelState[T any](cancels ...context.CancelFunc) LoopableIteratorStateOption[T]
func WithOnErrorState ¶
func WithOnErrorState[T any](handler func(error) error) LoopableIteratorStateOption[T]
func WithOnInitState ¶
func WithShouldStopState ¶
func WithShouldStopState[T any](shouldStop func(error) bool) LoopableIteratorStateOption[T]
type Message ¶
type Message interface{}
Message is an interface type for internal communication within gronos.
type MessagePayload ¶
type Option ¶
type Option[K comparable] func(*gronos[K])
func WithExtension ¶
func WithExtension[K comparable](ext Extension[K]) Option[K]
func WithGracePeriod ¶
func WithGracePeriod[K comparable](period time.Duration) Option[K]
func WithImmediatePeriod ¶
func WithImmediatePeriod[K comparable](period time.Duration) Option[K]
func WithMinRuntime ¶
func WithMinRuntime[K comparable](duration time.Duration) Option[K]
func WithShutdownBehavior ¶
func WithShutdownBehavior[K comparable](behavior ShutdownBehavior) Option[K]
func WithWait ¶
func WithWait[K comparable]() Option[K]
func WithoutGracePeriod ¶
func WithoutGracePeriod[K comparable]() Option[K]
func WithoutImmediatePeriod ¶
func WithoutImmediatePeriod[K comparable]() Option[K]
func WithoutMinRuntime ¶
func WithoutMinRuntime[K comparable]() Option[K]
type PanickedShutdown ¶
type PanickedShutdown[K comparable] struct { KeyMessage[K] Error error RequestMessage[K, struct{}] }
type RemoveMessage ¶
type RemoveMessage[K comparable] struct { KeyMessage[K] RequestMessage[K, bool] }
func MsgRemove ¶
func MsgRemove[K comparable](key K) (<-chan bool, *RemoveMessage[K])
type RequestAlive ¶
type RequestAlive[K comparable] struct { KeyMessage[K] RequestMessage[K, bool] }
func MsgRequestAlive ¶
func MsgRequestAlive[K comparable](key K) *RequestAlive[K]
type RequestAllAlive ¶
type RequestAllAlive[K comparable] struct { RequestMessage[K, bool] }
func MsgRequestAllAlive ¶
func MsgRequestAllAlive[K comparable]() (<-chan bool, *RequestAllAlive[K])
type RequestMessage ¶
type RequestMessage[K comparable, Y any] struct { KeyMessage[K] Response chan Y }
Used for generic requests
type RequestReason ¶
type RequestReason[K comparable] struct { KeyMessage[K] RequestMessage[K, error] }
func MsgRequestReason ¶
func MsgRequestReason[K comparable](key K) *RequestReason[K]
type RequestStatus ¶
type RequestStatus[K comparable] struct { KeyMessage[K] RequestMessage[K, StatusState] }
func MsgRequestStatus ¶
func MsgRequestStatus[K comparable](key K) (<-chan StatusState, *RequestStatus[K])
type RequestStatusAsync ¶
type RequestStatusAsync[K comparable] struct { KeyMessage[K] When StatusState RequestMessage[K, struct{}] }
func MsgRequestStatusAsync ¶
func MsgRequestStatusAsync[K comparable](key K, when StatusState) (<-chan struct{}, *RequestStatusAsync[K])
type RuntimeApplication ¶
RuntimeApplication is a function type representing an application that can be run concurrently. It takes a context and a shutdown channel as parameters and returns an error.
func Iterator ¶
func Iterator(tasks []CancellableTask, opts ...IteratorOption) RuntimeApplication
Iterator creates a RuntimeApplication that uses a LoopableIterator to execute tasks.
func IteratorState ¶
func IteratorState[T any](tasks []CancellableStateTask[T], opts ...IteratorStateOption[T]) RuntimeApplication
IteratorState creates a RuntimeApplication that uses a LoopableIteratorState to execute tasks with a shared state
func Worker ¶
func Worker(interval time.Duration, mode ExecutionMode, app TickingRuntime, opts ...tickerOption) RuntimeApplication
Worker creates a RuntimeApplication that executes a TickingRuntime at specified intervals. It takes an interval duration, execution mode, and a TickingRuntime as parameters.
Example usage:
worker := gronos.Worker(time.Second, gronos.NonBlocking, func(ctx context.Context) error { // Periodic task logic here return nil }) g.Add("periodicTask", worker)
type RuntimeError ¶
type RuntimeError[K comparable] struct { KeyMessage[K] Error error }
func MsgRuntimeError ¶
func MsgRuntimeError[K comparable](key K, err error) *RuntimeError[K]
type ShutdownBehavior ¶
type ShutdownBehavior int
const ( ShutdownAutomatic ShutdownBehavior = iota ShutdownManual )
type ShutdownComplete ¶
type ShutdownComplete[K comparable] struct{}
type ShutdownKind ¶
type ShutdownKind string
const ( ShutdownKindTerminate ShutdownKind = "terminate" ShutdownKindCancel ShutdownKind = "cancel" )
type ShutdownProgress ¶
type ShutdownProgress[K comparable] struct { RemainingApps int }
type StatusState ¶
type StatusState string
StatusState represents the possible states of a component
const ( StatusAdded StatusState = "added" // first state StatusRunning StatusState = "running" // when the runtime application is triggered StatusShutingDown StatusState = "shuting_down" // when any of messages related to shutdown is initiated StatusShutdownCancelled StatusState = "shutdown_cancelled" StatusShutdownPanicked StatusState = "shutdown_panicked" StatusShutdownTerminated StatusState = "shutdown_terminated" StatusShutdownError StatusState = "shutdown_error" StatusNotFound StatusState = "shutdown_not_found" )
type TerminatedShutdown ¶
type TerminatedShutdown[K comparable] struct { RequestMessage[K, struct{}] }
type Ticker ¶
type Ticker interface {
// Tick is called when the ticker is triggered.
Tick()
}
Ticker interface represents an object that can be ticked.
type TickerSubscriber ¶
type TickerSubscriber struct { Ticker Ticker Mode ExecutionMode DynamicInterval func(lastInterval time.Duration) time.Duration // contains filtered or unexported fields }
TickerSubscriber represents a subscriber to the clock's ticks.
type TickingRuntime ¶
TickingRuntime is a function type representing an application that performs periodic tasks.