Documentation
¶
Overview ¶
Package healthcheck provides easy to register and manage health checks for services.
Example ¶
ctx := context.Background() var ( serviceIsUnhealthy bool // If Checker has changing variable, use mutex to handle race condition. mutex sync.RWMutex ) dummyHealthyChecker := func(_ context.Context) error { return nil } dummyUnhealthyChecker := func(_ context.Context) error { mutex.RLock() defer mutex.RUnlock() if serviceIsUnhealthy { return errors.New("not_feeling_good") } return nil } serveMux := http.NewServeMux() h := New(serveMux, "/healthcheck") h.Register("dummy_healthy_checker", dummyHealthyChecker, time.Second) h.Register("dummy_unhealthy_checker_in_background", dummyUnhealthyChecker, time.Second*2, InBackground(time.Millisecond)) h.Register("dummy_unhealthy_checker_with_threshold", dummyUnhealthyChecker, time.Second, WithThreshold(2)) h.Run(ctx) defer h.Close() time.Sleep(time.Millisecond * 10) // check method is not exposed. Don't use it in your codes. fmt.Println(h.check(ctx)) // Lets make unhealthy checkers fail mutex.Lock() serviceIsUnhealthy = true mutex.Unlock() time.Sleep(time.Millisecond * 10) fmt.Println(h.check(ctx)) fmt.Println(h.check(ctx))
Output: map[] map[dummy_unhealthy_checker_in_background:not_feeling_good] map[dummy_unhealthy_checker_in_background:not_feeling_good dummy_unhealthy_checker_with_threshold:not_feeling_good]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckOption ¶
type CheckOption func(c *check)
A CheckOption is a modifier of a check. It can be passed while registering a checker to customize it.
func InBackground ¶
func InBackground(interval time.Duration) CheckOption
InBackground forces a check to run in the background. Returns a CheckOption that can be passed during the Checker registration.
func WithThreshold ¶
func WithThreshold(threshold uint) CheckOption
WithThreshold adds a threshold of errors in the row to show unhealthy state. Returns a CheckOption that can be passed during the Checker registration.
type Checker ¶
A Checker is a function responsible to check the status of a service and return an error if service is unhealthy.
type HealthCheck ¶
type HealthCheck struct {
// contains filtered or unexported fields
}
A HealthCheck holds all details of checkers and manage their executions.
func New ¶
func New(serve *http.ServeMux, handlerPattern string) *HealthCheck
New creates a new HealthCheck.
serve ServeMux to register handler. If not sure, pass http.DefaultServeMux. handlerPattern patten for handler (e.g. "/healthcheck").
func (*HealthCheck) Close ¶
func (h *HealthCheck) Close()
Close stops running of the background checkers and release resources.
func (*HealthCheck) Register ¶
func (h *HealthCheck) Register(name string, c Checker, timeout time.Duration, opts ...CheckOption)
Register will register a Checker for a HealthCheck. Params:
name Name of the check. Will be used in the detailed output. c The check function. timeout Timeout of the check execution. opts Checker options e.g. run in background.
func (*HealthCheck) Run ¶
func (h *HealthCheck) Run(ctx context.Context)
Run executes a goroutine that runs background checkers.