healthcheck.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package metrics
  2. // Healthchecks hold an os.Error value describing an arbitrary up/down status.
  3. //
  4. // This is an interface so as to encourage other structs to implement
  5. // the Healthcheck API as appropriate.
  6. type Healthcheck interface {
  7. Check()
  8. Error() error
  9. Healthy()
  10. Unhealthy(error)
  11. }
  12. // The standard implementation of a Healthcheck stores the status and a
  13. // function to call to update the status.
  14. type StandardHealthcheck struct {
  15. err error
  16. f func(Healthcheck)
  17. }
  18. // Create a new healthcheck, which will use the given function to update
  19. // its status.
  20. func NewHealthcheck(f func(Healthcheck)) *StandardHealthcheck {
  21. return &StandardHealthcheck{nil, f}
  22. }
  23. // Update the healthcheck's status.
  24. func (h *StandardHealthcheck) Check() {
  25. h.f(h)
  26. }
  27. // Return the healthcheck's status, which will be nil if it is healthy.
  28. func (h *StandardHealthcheck) Error() error {
  29. return h.err
  30. }
  31. // Mark the healthcheck as healthy.
  32. func (h *StandardHealthcheck) Healthy() {
  33. h.err = nil
  34. }
  35. // Mark the healthcheck as unhealthy. The error should provide details.
  36. func (h *StandardHealthcheck) Unhealthy(err error) {
  37. h.err = err
  38. }