Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Governor ¶
type Governor interface {
// CatchSignals configures Governor to catch SIGINT (Ctrl+C)
// and other interrupt signals (SIGTERM, SIGHUP, SIGQUIT)
// and call Shutdown() when a signal is received.
CatchSignals() Governor
// Restart configures a default restart policy for all services,
// which can be overridden on a per-service basis.
// The restart delay can be zero.
// Services never restart unless restart policy is set.
Restart(after time.Duration) Governor
// Add a service to Governor.
// Returns a ServiceConfig interface to allow configuring and
// starting the service.
Add(name string, svc Service) ServiceConfig
// Start all added services.
// Does not affect services that have already been started.
Start() Governor
// Shutdown stops all services.
// This call blocks until all services have stopped.
Shutdown()
// WaitForShutdown waits for all services to be stopped.
// Caveat: will return immediately if no services have been started.
// This can be useful at the end of main()
WaitForShutdown()
// GlobalContext gets a cancellable context for use outside of any service.
// This context will be canncelled when shutdown is requested.
GlobalContext() context.Context
// Sleep blocks for 'duration', cancelled if GlobalContext() is cancelled.
Sleep(duration time.Duration) (cancelled bool)
// Stopping returns true if GlobalContext() is cancelled.
Stopping() bool
}
Governor oversees all services and manages shutdown.
type Service ¶
type Service interface {
// Run is the main service goroutine.
//
// The Governor calls this on a new goroutine when the service
// is started. When Run() returns, the service will stop
// and will be restarted if configured to do so.
// The Governor wraps Run() calls with recover() and will catch
// any panic() and log the error, then stop/restart the service.
//
// Run() should use the ServiceCtx.Ctx with any blocking
// calls that can take a Context, to support cancellation.
//
// Network connections do not use a Context for reads and writes
// (but they do for Dial calls) so the service should implement
// the Stoppable interface to close any open net.Conn connections.
//
// Use ServiceCtx.Sleep() instead of time.Sleep() to support cancellation.
Run()
}
Service interface are the methods a service must implement. The service should also embed `ServiceCtx` in the service struct.
type ServiceConfig ¶
type ServiceConfig interface {
// Restart the service if it stops.
// The restart delay can be zero.
// This overrides the Governor restart policy.
Restart(after time.Duration) ServiceConfig
// Prevent service restart, to override Governor policy.
// Services never restart unless a restart policy is set.
NoRestart() ServiceConfig
// Start the service immediately.
// No more configuration is possible after calling Start()
Start()
}
ServiceConfig allows you to configure a service when adding it to the Governor.
type ServiceCtx ¶
ServiceCtx is embedded in all service implementation-structs to provide a Context and common helper functions.
func (*ServiceCtx) Log ¶
func (s *ServiceCtx) Log(fmt string, args ...interface{})
Log a message tagged with the service name.
func (*ServiceCtx) Sleep ¶
func (s *ServiceCtx) Sleep(duration time.Duration) bool
Sleep returns true if it was interrupted by Governor asking the service to stop: caller should exit the service goroutine.
func (*ServiceCtx) Stopping ¶
func (s *ServiceCtx) Stopping() bool
Stopping returns true if the servce has been asked to stop: caller should exit the goroutine.
type StopService ¶
type StopService struct{}
StopService is a sentinel value that can be used stop a service from deep within a call stack by calling panic(StopService{}). Services are not stopped until they return from their Run() function; this provides an escape hatch for large, complex services.
type Stoppable ¶
type Stoppable interface {
// Stop is called to stop the service (e.g. during shutdown)
//
// This is called when the Governor is trying to stop the
// service, after the service Context is cancelled.
//
// NOTE: this will be called from a goroutine other than
// the service goroutine (may need a sync.Mutex)
//
// NOTE: be aware that this can be called while your service
// is starting up (i.e. around the time Run() is called!)
//
// Stop can be used to e.g. close network sockets that the
// service might be blocking on (in Go, blocking network
// calls ignore Context cancellation, so this is necessary
// to interrupt those calls.)
Stop()
}