reactor

package
v0.0.0-...-5e451f0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrTypeHandlerCanceled = "reactor.handle_canceled"
)

Variables

View Source
var (
	ErrHandlerCanceled = errors.New("reactor handle not run for canceling, the Reactor has been stopped")
)

Functions

This section is empty.

Types

type Handler

type Handler func(context.Context)

type Option

type Option func(*Reactor)

func WithContext

func WithContext(c context.Context) Option

WithContext defines a context for controlling the Reactor in another way except Start/Stop method.

type Reactor

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

Reactor provides a "reactor" design mode for function called. It's similar to event loop, each function submit to the Reactor is an event, and Reactor calls the function as event process. If you need to process many functions calls having concurrent or sync lock scenes, Reactor will help you make each function call into an ordering queue and calls them one by one, which would not required to consider the sync lock or concurrent question. It's like a goroutine pool with single goroutine.See example for more usages and details.

func NewReactor

func NewReactor(options ...Option) *Reactor

func (*Reactor) Push

func (r *Reactor) Push(handler Handler) error

Push will insert the handler to Reactor's queue and return immediately. If the Reactor has benn stopped, it will return ErrInvalidStatus error with typed ErrTypeInvalidStatus.

func (*Reactor) PushPriority

func (r *Reactor) PushPriority(handler Handler) error

PushPriority is the same as Push, but the Handler is higher priority than Push.

func (*Reactor) Send

func (r *Reactor) Send(handler Handler) error

Send will insert the handler to Reactor's queue and wait for the Handler run completed. If the Reactor has benn stopped, it will return ErrInvalidStatus error with typed ErrTypeInvalidStatus. If the Handler inserted to the queue and waiting for run, but the Reactor is Stop, it will return ErrHandlerCanceled error with ErrTypeHandlerCanceled.

func (*Reactor) SendPriority

func (r *Reactor) SendPriority(handler Handler) error

SendPriority is the same as Send, but the Handler is higher priority than Send.

func (*Reactor) Start

func (r *Reactor) Start()

Start is required to call before Push or Send Handler to the Reactor. It will be called with Stop in pair.

func (*Reactor) Stop

func (r *Reactor) Stop()

Stop is called for shutdown the Reactor. The Handlers which are not will return an ErrHandlerCanceled error typed with ErrTypeHandlerCanceled. When Stop returned, every Handler Push or Send to the Reactor will be run completed or canceled.

func (*Reactor) Waiting

func (r *Reactor) Waiting() int

Waiting return the count of Handlers which are in the queue and waiting for run.

Jump to

Keyboard shortcuts

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