http_health
haiyux 2 years ago
parent 592f569517
commit c5292a62a1
  1. 13
      health/health.go

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"net/http" "net/http"
"sync"
) )
type Status string type Status string
@ -24,12 +25,14 @@ func (f CheckerFunc) Check(ctx context.Context) error {
} }
type Health struct { type Health struct {
lock sync.RWMutex
status Status status Status
checkers map[string]Checker checkers map[string]Checker
} }
func New() *Health { func New() *Health {
h := &Health{ h := &Health{
lock: sync.RWMutex{},
status: Down, status: Down,
checkers: make(map[string]Checker), checkers: make(map[string]Checker),
} }
@ -37,20 +40,28 @@ func New() *Health {
} }
func (h *Health) Register(name string, checker CheckerFunc) { func (h *Health) Register(name string, checker CheckerFunc) {
h.lock.Lock()
defer h.lock.Unlock()
h.checkers[name] = checker h.checkers[name] = checker
} }
func (h *Health) Start(_ context.Context) error { func (h *Health) Start(_ context.Context) error {
h.lock.Lock()
defer h.lock.Unlock()
h.status = Up h.status = Up
return nil return nil
} }
func (h *Health) Stop(_ context.Context) error { func (h *Health) Stop(_ context.Context) error {
h.lock.Lock()
defer h.lock.Unlock()
h.status = Down h.status = Down
return nil return nil
} }
func (h *Health) Check(ctx context.Context) Result { func (h *Health) Check(ctx context.Context) Result {
h.lock.RLock()
defer h.lock.RUnlock()
res := Result{Status: h.status, Details: make(map[string]Detail, len(h.checkers))} res := Result{Status: h.status, Details: make(map[string]Detail, len(h.checkers))}
for n, c := range h.checkers { for n, c := range h.checkers {
if err := c.Check(ctx); err != nil { if err := c.Check(ctx); err != nil {
@ -64,7 +75,9 @@ func (h *Health) Check(ctx context.Context) Result {
} }
func (h *Health) CheckService(ctx context.Context, svc string) Detail { func (h *Health) CheckService(ctx context.Context, svc string) Detail {
h.lock.RLock()
c, ok := h.checkers[svc] c, ok := h.checkers[svc]
h.lock.RUnlock()
if !ok { if !ok {
return Detail{Status: Down, Error: "service not find"} return Detail{Status: Down, Error: "service not find"}
} }

Loading…
Cancel
Save