healthcheck.go 1.1 KB

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