healthcheck.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. // Force the compiler to check that StandardHealthcheck implements Healthcheck.
  19. var _ Healthcheck = &StandardHealthcheck{}
  20. // Create a new healthcheck, which will use the given function to update
  21. // its status.
  22. func NewHealthcheck(f func(Healthcheck)) *StandardHealthcheck {
  23. return &StandardHealthcheck{nil, f}
  24. }
  25. // Update the healthcheck's status.
  26. func (h *StandardHealthcheck) Check() {
  27. h.f(h)
  28. }
  29. // Return the healthcheck's status, which will be nil if it is healthy.
  30. func (h *StandardHealthcheck) Error() error {
  31. return h.err
  32. }
  33. // Mark the healthcheck as healthy.
  34. func (h *StandardHealthcheck) Healthy() {
  35. h.err = nil
  36. }
  37. // Mark the healthcheck as unhealthy. The error should provide details.
  38. func (h *StandardHealthcheck) Unhealthy(err error) {
  39. h.err = err
  40. }