shot

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 6 Imported by: 0

README

shot - manage instance execution

Go Reference

This package provides concurrency primitives for managing the execution of instances.

Types

  • One - ensures that only one instance runs at a time and runs only once
  • Many - ensures that only one instance runs at a time, but allows restarting the instance
  • Group - represents a group of goroutines with a managed context

Functions

  • Go - runs a function in a goroutine with a managed context
  • GoCtx - runs a function in a goroutine with a managed context derived from the parent context ctx

Documentation

Overview

Package shot provides concurrency primitives for managing the execution of instances.

Index

Constants

This section is empty.

Variables

View Source
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

func Go(f func(ctx context.Context) error) *G

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

func GoCtx(ctx context.Context, f func(ctx context.Context) error) *G

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

func (g *G) Close(ctx context.Context) error

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.

func (*G) Done

func (g *G) Done() <-chan struct{}

Done returns a channel that is closed when the goroutine has exited.

func (*G) Err

func (g *G) Err() error

Err returns the error returned from the goroutine. If goroutine hasn't yet exited, returns nil. If goroutine exited without error, also returns nil.

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 NewGroup added in v0.2.0

func NewGroup(ctx context.Context) *Group

NewGroup creates Group with the given parent context.

func (*Group) Cancel added in v0.2.0

func (g *Group) Cancel()

Cancel cancels the context.

func (*Group) Close added in v0.2.0

func (g *Group) Close()

Close cancels the context and waits for all goroutines to finish.

func (*Group) Context added in v0.2.0

func (g *Group) Context() context.Context

Context returns the managed context.

func (*Group) Go added in v0.2.0

func (g *Group) Go(fn func(context.Context))

Go runs a function in a goroutine with a managed context.

func (*Group) Wait added in v0.2.0

func (g *Group) Wait()

Wait 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 NewMany

func NewMany(parent context.Context) Many

NewMany creates Many with the given parent context.

func (*Many) Cancel added in v0.3.0

func (s *Many) Cancel() (prev State)

Cancel acts the same as Close2, but doesn't wait for completion.

func (*Many) Close

func (s *Many) Close(ctx context.Context) error

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

func (s *Many) Close2(ctx context.Context) (prev State, err error)

Close2 acts the same as Close, but also returns the state of the instance as it was in before the call.

func (*Many) Context

func (s *Many) Context() context.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) Halt added in v0.4.0

func (s *Many) Halt() (prev State, err error)

Halt acts the same as Stop2, but doesn't wait for completion.

func (*Many) Start

func (s *Many) Start() (stop func(), err error)

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) State

func (s *Many) State() State

State returns the current state of the instance.

func (*Many) Stop added in v0.4.0

func (s *Many) Stop(ctx context.Context) error

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.

func (*Many) Stop2 added in v0.4.0

func (s *Many) Stop2(ctx context.Context) (prev State, err error)

Stop2 acts the same as Stop, but also returns the state of the instance as it was in before the call.

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 NewOne

func NewOne(parent context.Context) One

NewOne creates One with the given parent context.

func (*One) Cancel added in v0.3.0

func (s *One) Cancel() (prev State)

Cancel acts the same as Close2, but doesn't wait for completion.

func (*One) Close

func (s *One) Close(ctx context.Context) error

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

func (s *One) Close2(ctx context.Context) (prev State, err error)

Close2 acts the same as Close, but also returns the state of the instance as it was in before the call.

func (*One) Context

func (s *One) Context() context.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.

func (*One) Start

func (s *One) Start() (stop func(), err error)

Start starts the instance and returns a stop function. The stop function signals completion and must be called. Returns an error if already running or closed.

func (*One) State

func (s *One) State() State

State returns the current state of the instance.

type State

type State int32

State represents the state of an instance.

const (
	// Instance initialized but not yet running.
	StateCreated State = iota
	// Instance is running.
	StateRunning
	// Instance completed and can be restarted.
	StateStopped
	// Instance exited and cannot be reused.
	StateClosed
)

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