Documentation
¶
Overview ¶
Package shot provides concurrency primitives for managing the execution of instances.
Index ¶
- Variables
- type G
- type Group
- type Many
- func (s *Many) Cancel() (prev State)
- func (s *Many) Close(ctx context.Context) error
- func (s *Many) Close2(ctx context.Context) (prev State, err error)
- func (s *Many) Context() context.Context
- func (s *Many) Done() <-chan struct{}
- func (s *Many) Halt() (prev State, err error)
- func (s *Many) Start() (stop func(), err error)
- func (s *Many) State() State
- func (s *Many) Stop(ctx context.Context) error
- func (s *Many) Stop2(ctx context.Context) (prev State, err error)
- type One
- func (s *One) Cancel() (prev State)
- func (s *One) Close(ctx context.Context) error
- func (s *One) Close2(ctx context.Context) (prev State, err error)
- func (s *One) Context() context.Context
- func (s *One) Done() <-chan struct{}
- func (s *One) Start() (stop func(), err error)
- func (s *One) State() State
- type State
Constants ¶
This section is empty.
Variables ¶
var ( ErrRunning = errors.New("already running") ErrClosed = errors.New("closed") )
Functions ¶
This section is empty.
Types ¶
type G ¶
type G struct {
// contains filtered or unexported fields
}
G allows to manage execution of a goroutine and get the error returned from the goroutine.
func Go ¶
Go runs a function in a goroutine with a managed context.
Returns G, which can be used to manage the goroutine execution and get the error returned from the goroutine.
func GoCtx ¶
GoCtx runs a function in a goroutine with a managed context derived from the parent context ctx.
Returns G, which can be used to manage the goroutine execution and get the error returned from the goroutine.
func (*G) Cancel ¶ added in v0.3.0
func (g *G) Cancel()
Cancel stops the goroutine, but doesn't wait for it to exit.
func (*G) Close ¶
Close stops the goroutine and waits for it to exit with the given context.
Context passed to this method can be canceled to pass control back to the caller if waiting for the goroutine to exit takes too much time. Context cancellation only affects the wait time, not the goroutine itself.
type Group ¶ added in v0.2.0
type Group struct {
// contains filtered or unexported fields
}
Group represents a group of goroutines with a managed context.
func (*Group) Close ¶ added in v0.2.0
func (g *Group) Close()
Close cancels the context and waits for all goroutines to finish.
type Many ¶
type Many struct {
// contains filtered or unexported fields
}
Many ensures that only one instance runs at a time, but allows restarting the instance.
func (*Many) Cancel ¶ added in v0.3.0
Cancel acts the same as Close2, but doesn't wait for completion.
func (*Many) Close ¶
Close shuts down the instance and cancels its context.
If not started, prevents instance from being started at all. If running, waits for completion with the given context.
If the provided context expires before the shutdown is complete, Close returns the context's error.
func (*Many) Close2 ¶ added in v0.2.0
Close2 acts the same as Close, but also returns the state of the instance as it was in before the call.
func (*Many) Context ¶
Context returns the instance's current execution context. The context changes whenever the instance restarts.
The context is cancelled when:
- The instance exits normally
- Close is called
- Stop is called
- The parent context is cancelled
Instance must exit when the context is canceled.
func (*Many) Done ¶
func (s *Many) Done() <-chan struct{}
Done returns a channel that is closed when the instance's current execution ends. The channel changes whenever the instance restarts.
func (*Many) Start ¶
Start starts the instance and returns a stop function. Call stop to signal completion and allow restarting. The stop function must be called. Returns an error if already running or closed.
func (*Many) Stop ¶ added in v0.4.0
Stop shuts down the instance, allowing future restarts, and cancels its context.
If running, waits for completion with the given context.
If the provided context expires before the shutdown is complete, Stop returns the context's error.
Returns ErrClosed if already closed.
type One ¶
type One struct {
// contains filtered or unexported fields
}
One ensures that only one instance runs at a time and runs only once.
func (*One) Cancel ¶ added in v0.3.0
Cancel acts the same as Close2, but doesn't wait for completion.
func (*One) Close ¶
Close shuts down the instance and cancels its context.
If not started, prevents instance from being started at all. If running, waits for completion with the given context.
If the provided context expires before the shutdown is complete, Close returns the context's error.
func (*One) Close2 ¶ added in v0.2.0
Close2 acts the same as Close, but also returns the state of the instance as it was in before the call.
func (*One) Context ¶
Context returns the instance's context.
The context is cancelled when:
- The instance exits normally
- Close is called
- The parent context is cancelled
Instance must exit when the context is canceled.
func (*One) Done ¶
func (s *One) Done() <-chan struct{}
Done returns a channel that is closed when the instance's execution ends.