From 462a3c44a677923f4fde2ea2b5028d157ea9df8d Mon Sep 17 00:00:00 2001 From: Serhii Mariiekha Date: Fri, 15 Jul 2022 11:16:50 +0200 Subject: [PATCH] Add NopChecker --- hc.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/hc.go b/hc.go index 8433c4b..0b60d52 100644 --- a/hc.go +++ b/hc.go @@ -17,28 +17,21 @@ type HealthChecker interface { // NewMultiChecker takes several health checkers and performs // health checks for each of them concurrently. func NewMultiChecker(hcs ...HealthChecker) *MultiChecker { - c := MultiChecker{ - hcs: make([]HealthChecker, 0, len(hcs)), - } + c := MultiChecker{hcs: make([]HealthChecker, 0, len(hcs))} c.hcs = append(c.hcs, hcs...) return &c } -// MultiChecker takes several health checker and performs +// MultiChecker takes multiple health checker and performs // health checks for each of them concurrently. -type MultiChecker struct { - hcs []HealthChecker -} +type MultiChecker struct{ hcs []HealthChecker } // Health takes the context and performs the health check. // Returns nil in case of success or an error in case // of a failure. func (c *MultiChecker) Health(ctx context.Context) error { hctx, cancel := context.WithCancel(ctx) - - s := Synchronizer{ - cancel: cancel, - } + s := Synchronizer{cancel: cancel} for _, check := range c.hcs { s.Add(1) @@ -64,6 +57,11 @@ func (c *MultiChecker) Health(ctx context.Context) error { // Add appends health HealthChecker to internal slice of HealthCheckers. func (c *MultiChecker) Add(hc HealthChecker) { c.hcs = append(c.hcs, hc) } +// NopChecker represents nop health checker. +type NopChecker struct{} + +func (n NopChecker) Health(context.Context) error { return nil } + // Synchronizer holds synchronization mechanics. type Synchronizer struct { wg sync.WaitGroup @@ -80,7 +78,7 @@ func (s *Synchronizer) Error() string { return s.err.Error() } // Uses sync.Once to set error only once. func (s *Synchronizer) SetError(err error) { s.so.Do(func() { s.err = err }) } -// Do wraps the sync.Once Do method. +// Do wrap the sync.Once Do method. func (s *Synchronizer) Do(f func()) { s.so.Do(f) } // Done wraps the sync.WaitGroup Done method. @@ -92,9 +90,6 @@ func (s *Synchronizer) Add(delta int) { s.wg.Add(delta) } // Wait wraps the sync.WaitGroup Wait method. func (s *Synchronizer) Wait() { s.wg.Wait() } -// Cancel calls underlying cancel function -// to cancel context, which passed to all -// health checks function. -func (s *Synchronizer) Cancel() { - s.cancel() -} +// Cancel calls underlying cancel function to cancel context, +// which passed to all health checks function. +func (s *Synchronizer) Cancel() { s.cancel() }