health.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Package health provides some utility functions to health-check a server. The implementation
  2. // is based on protobuf. Users need to write their own implementations if other IDLs are used.
  3. //
  4. // See original source: https://github.com/grpc/grpc-go/blob/master/health/health.go
  5. //
  6. // We use our own implementation of grpc server health check to include the authorization
  7. // wrapper necessary for the Managers.
  8. package health
  9. import (
  10. "sync"
  11. "github.com/docker/swarmkit/api"
  12. "golang.org/x/net/context"
  13. "google.golang.org/grpc"
  14. "google.golang.org/grpc/codes"
  15. )
  16. // Server represents a Health Check server to check
  17. // if a service is running or not on some host.
  18. type Server struct {
  19. mu sync.Mutex
  20. // statusMap stores the serving status of the services this HealthServer monitors.
  21. statusMap map[string]api.HealthCheckResponse_ServingStatus
  22. }
  23. // NewHealthServer creates a new health check server for grpc services.
  24. func NewHealthServer() *Server {
  25. return &Server{
  26. statusMap: make(map[string]api.HealthCheckResponse_ServingStatus),
  27. }
  28. }
  29. // Check checks if the grpc server is healthy and running.
  30. func (s *Server) Check(ctx context.Context, in *api.HealthCheckRequest) (*api.HealthCheckResponse, error) {
  31. s.mu.Lock()
  32. defer s.mu.Unlock()
  33. if in.Service == "" {
  34. // check the server overall health status.
  35. return &api.HealthCheckResponse{
  36. Status: api.HealthCheckResponse_SERVING,
  37. }, nil
  38. }
  39. if status, ok := s.statusMap[in.Service]; ok {
  40. return &api.HealthCheckResponse{
  41. Status: status,
  42. }, nil
  43. }
  44. return nil, grpc.Errorf(codes.NotFound, "unknown service")
  45. }
  46. // SetServingStatus is called when need to reset the serving status of a service
  47. // or insert a new service entry into the statusMap.
  48. func (s *Server) SetServingStatus(service string, status api.HealthCheckResponse_ServingStatus) {
  49. s.mu.Lock()
  50. s.statusMap[service] = status
  51. s.mu.Unlock()
  52. }