Documentation ¶
Overview ¶
Package xync provides goroutine management types and functions.
Index ¶
- type Supervisor
- func (s *Supervisor) ActiveContext() context.Context
- func (s *Supervisor) BaseContext() context.Context
- func (s *Supervisor) Close()
- func (s *Supervisor) CloseAndWait()
- func (s *Supervisor) Go(fn func(context.Context))
- func (s *Supervisor) Stop()
- func (s *Supervisor) StopAndWait()
- func (s *Supervisor) Wait()
- type SupervisorOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor handles goroutine creation and termination.
func NewSupervisor ¶
func NewSupervisor(opts ...SupervisorOption) *Supervisor
NewSupervisor creates a fresh instance of the goroutine supervisor.
func (*Supervisor) ActiveContext ¶
func (s *Supervisor) ActiveContext() context.Context
ActiveContext returns the context that is associated with the supervisor and is cancelled either when Stop or Close is called.
func (*Supervisor) BaseContext ¶
func (s *Supervisor) BaseContext() context.Context
BaseContext returns the context that is associated with the supervisor and is cancelled only when Close is called.
func (*Supervisor) Close ¶
func (s *Supervisor) Close()
Close stops all currently active/idle goroutines and ensures that all new goroutines that are created in the future are stopped as well.
func (*Supervisor) CloseAndWait ¶
func (s *Supervisor) CloseAndWait()
CloseAndWait stops all currently active/idle goroutines, blocks until all of them exit and ensures that all new goroutines that are created in the future are stopped as well.
func (*Supervisor) Go ¶
func (s *Supervisor) Go(fn func(context.Context))
Go executes the provided function on a new goroutine. The provided function must handle all context cancellation events and exit when required. Once closed, the supervisor passes a cancelled context to all new goroutines and their functions.
func (*Supervisor) Stop ¶
func (s *Supervisor) Stop()
Stop stops all currently active/idle goroutines.
func (*Supervisor) StopAndWait ¶
func (s *Supervisor) StopAndWait()
StopAndWait stops all currently active/idle goroutines and blocks until all of them exit.
type SupervisorOption ¶
type SupervisorOption func(*Supervisor)
SupervisorOption is used to configure the supervisor.
func WithSupervisorBaseContext ¶
func WithSupervisorBaseContext(ctx context.Context) SupervisorOption
WithSupervisorBaseContext sets the base context that is passed to all goroutines. Note that the supervisor wraps the provided context in its own internal context that is used to cancel all goroutines.
func WithSupervisorMaxActive ¶
func WithSupervisorMaxActive(max int64) SupervisorOption
WithSupervisorMaxActive sets the maximum number of active goroutines. When the limit is reached, all new goroutines remain idle and wait until their function is executed.
func WithSupervisorRecovery ¶
func WithSupervisorRecovery(fn func(any)) SupervisorOption
WithSupervisorRecovery sets a function that is called on each panic inside the Go() method.