github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/metrics/healthcheck.go (about)

     1  package metrics
     2  
     3  type Healthcheck interface {
     4  	Check()
     5  	Error() error
     6  	Healthy()
     7  	Unhealthy(error)
     8  }
     9  
    10  func NewHealthcheck(f func(Healthcheck)) Healthcheck {
    11  	if !Enabled {
    12  		return NilHealthcheck{}
    13  	}
    14  	return &StandardHealthcheck{nil, f}
    15  }
    16  
    17  type NilHealthcheck struct{}
    18  
    19  func (NilHealthcheck) Check() {}
    20  
    21  func (NilHealthcheck) Error() error { return nil }
    22  
    23  func (NilHealthcheck) Healthy() {}
    24  
    25  func (NilHealthcheck) Unhealthy(error) {}
    26  
    27  type StandardHealthcheck struct {
    28  	err error
    29  	f   func(Healthcheck)
    30  }
    31  
    32  func (h *StandardHealthcheck) Check() {
    33  	h.f(h)
    34  }
    35  
    36  func (h *StandardHealthcheck) Error() error {
    37  	return h.err
    38  }
    39  
    40  func (h *StandardHealthcheck) Healthy() {
    41  	h.err = nil
    42  }
    43  
    44  func (h *StandardHealthcheck) Unhealthy(err error) {
    45  	h.err = err
    46  }