Skip to content

Commit

Permalink
Merge pull request #3 from heartwilltell/v0.1.4
Browse files Browse the repository at this point in the history
Add NopChecker
  • Loading branch information
heartwilltell committed Jul 15, 2022
2 parents 093b41b + 462a3c4 commit e7310e7
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions hc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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() }

0 comments on commit e7310e7

Please sign in to comment.