Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LifeCycle ¶
type LifeCycle struct {
// contains filtered or unexported fields
}
A state machine for services which have lifecycles where they initialize, run, and stop. Provides the ability to wait for lifecycle events, such as "service has finished initializing" or "service has finished shutting down."
func NewLifeCycle ¶
func NewLifeCycle() *LifeCycle
func (*LifeCycle) GetState ¶
Get the current state in the life cycle (for example, "still initializing").
func (*LifeCycle) Transition ¶
Change to the given state in this life cycle. For example, when the service is done initializing you should probably change it's state to STATE_RUNNING. This includes unblocking any goroutines that might be waiting for the state transition.
Life cycle states must happen in order. You can skip states, but never go backward. For example, a service that is RUNNING may never go back to NEW, and a service that is STOPPED may never go back to RUNNING.
func (*LifeCycle) WaitForState ¶
Wait until the service enters the given state. If the service enters another state that makes it impossible that the requested state will ever be entered, this function will also return in that case. For example, if you're waiting for STATE_RUNNING but there's an error during initialization and the service jumps to STATE_STOPPED, then this function will return STATE_STOPPED instead of blocking forever.
type ShutdownRequest ¶
type ShutdownRequest struct {
// contains filtered or unexported fields
}
ShutdownRequest solves the problem of quickly and cleanly shutting down a goroutine. When a goroutine is reading incoming work items from a channel and executing them, using the quit channel approach may not immediately shut down the worker:
keepLooping := true
for keepLooping {
select {
workItem := <-workChan:
doWork(workItem)
<-quitChan:
keepLooping = false
}
}
In the above example, the worker may continue processing workChan for many iterations until it happens to read from quitChan. Using ShutdownRequest you can guarantee termination within one iteration:
for !shutdownRequest.IsShutdownRequested() {
select {
case toAdd := <-onesChan:
atomic.AddInt32(&sum, int32(toAdd))
case <-shutdownRequest.GetShutdownRequestChan():
break
}
}
func NewShutdownRequest ¶
func NewShutdownRequest() *ShutdownRequest
func (*ShutdownRequest) GetShutdownRequestChan ¶
func (this *ShutdownRequest) GetShutdownRequestChan() chan interface{}
Get the channel that will be closed when shutdown is requested. Use the returned channel in the server's main loop select statement. Use the returned channel in a select statement, all inside a for loop whose predicate checks IsShutdownRequested() (see above).
func (*ShutdownRequest) IsShutdownRequested ¶
func (this *ShutdownRequest) IsShutdownRequested() bool
Call this in the server main loop to check whether it should shut down. Use this together with the shutdown request channel.
func (*ShutdownRequest) RequestShutdown ¶
func (this *ShutdownRequest) RequestShutdown()
Call this function to asynchronously indicate that the owning service should shut down.
type State ¶
type State int
const ( // The service is freshly created and has not yet finished initializing. STATE_NEW State = 0 // The service has finished initializing and is operating normally. STATE_RUNNING State = 5 // The service is shutting down and may not be accepting any new requests. STATE_STOPPING State = 10 // The service has finished shutting down and is completely unavailable. STATE_STOPPED State = 15 )