Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AnonService ¶
type AnonService struct { InitF func(context.Context) error RunF func(context.Context) StopF func() error }
AnonService is a simple struct for building Service instances with lambdas or funcs, instead of creating an interface implementation.
func (AnonService) Run ¶
func (a AnonService) Run(ctx context.Context)
func (AnonService) Stop ¶
func (a AnonService) Stop() error
type Controller ¶
type Controller struct {
// contains filtered or unexported fields
}
A Controller is responsible for initializing a number of registered services, running them all, and stopping them all when requested. Services are registered with |Register(Service)|. When |Start| is called, the services are all initialized, in the order of their registration, and if every service initializes successfully, they are |Run| concurrently. When |Stop| is called, services are stopped in reverse-registration order. |Stop| returns once the corresponding |Stop| method on all successfully |Init|ed services has returned. |Stop| does not explicitly block for the goroutines where the |Run| methods are called to complete. Typically a Service's |Stop| function should ensure that the |Run| method has completed before returning.
Any attempt to register a service after |Start| or |Stop| has been called will return an error.
If an error occurs when initializing the services of a Controller, the Stop functions of any already initialized Services are called in reverse-order. The error which caused the initialization error is returned.
In the case that all Services Init successfully, the error returned from |Start| is the first non-nil error which is returned from the |Stop| functions, in the order they are called.
If |Stop| is called before |Start|, |Start| will return an error. |Register| will also begin returning an error after |Stop| is called, if it is called before |Start|.
|WaitForStart| can be called at any time on a Controller. It will block until |Start| is called. After |Start| is called, if all the services successfully initialize, it will return |nil|. Otherwise it will return the same error |Start| returned.
|WaitForStop| can be called at any time on a Controller. It will block until |Start| is called and initialization fails, or until |Stop| is called. It will return the same error which |Start| returned.
func NewController ¶
func NewController() *Controller
func (*Controller) Register ¶
func (c *Controller) Register(svc Service) error
func (*Controller) Stop ¶
func (c *Controller) Stop()
func (*Controller) WaitForStart ¶
func (c *Controller) WaitForStart() error
func (*Controller) WaitForStop ¶
func (c *Controller) WaitForStop() error
type Service ¶
A Service is a runnable unit of functionality that a Controller can take responsibility for. It has an |Init| function, which can error, and which should do all of the initialization and validation work necessary to bring the service up. It has a |Run| function, which will be called in a separate go-routine and should run and provide the functionality associated with the service until the |Stop| function is called.
type ServiceState ¶
type ServiceState uint32
ServiceState is a small abstraction so that a service implementation can easily track what state it is in and can make decisions about what to do based on what state it is coming from. In particular, it's not rare for a |Close| implementation to need to do something different based on whether the service is only init'd or if it is running. It's also not rare for a service to decide it needs to do nothing in Init, in which case it can leave the service in Off, and the Run and Close methods can check that to ensure they do not do anything either.
const ( ServiceState_Off ServiceState = iota ServiceState_Init ServiceState_Run ServiceState_Stopped )
func (*ServiceState) CompareAndSwap ¶
func (ss *ServiceState) CompareAndSwap(old, new ServiceState) (swapped bool)
func (*ServiceState) Swap ¶
func (ss *ServiceState) Swap(new ServiceState) (old ServiceState)