-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
204 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package health | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/alexliesenfeld/health" | ||
) | ||
|
||
// Check represents a single health check. | ||
type Check struct { | ||
// The Name must be unique among all checks. Name is a required attribute. | ||
Name string // Required | ||
|
||
// Check is the check function that will be executed to check availability. | ||
// This function must return an error if the checked service is considered | ||
// not available. Check is a required attribute. | ||
Check func(ctx context.Context) error // Required | ||
|
||
// Timeout will override the global timeout value, if it is smaller than | ||
// the global timeout (see WithTimeout). | ||
Timeout time.Duration // Optional | ||
|
||
// MaxTimeInError will set a duration for how long a service must be | ||
// in an error state until it is considered down/unavailable. | ||
MaxTimeInError time.Duration // Optional | ||
|
||
// MaxContiguousFails will set a maximum number of contiguous | ||
// check fails until the service is considered down/unavailable. | ||
MaxContiguousFails uint // Optional | ||
|
||
// StatusListener allows to set a listener that will be called | ||
// whenever the AvailabilityStatus (e.g. from "up" to "down"). | ||
StatusListener func(ctx context.Context, name string, state health.CheckState) // Optional | ||
|
||
RefreshPeriod time.Duration | ||
InitialDelay time.Duration | ||
} | ||
|
||
// CompileHealthCheckerOption takes a list of Check and returns health.CheckerOption. | ||
func CompileHealthCheckerOption(checks ...Check) []health.CheckerOption { | ||
var opts []health.CheckerOption | ||
for i := range checks { | ||
c := &checks[i] | ||
|
||
if c.Name == "" { | ||
continue | ||
} | ||
opts = append(opts, health.WithPeriodicCheck(c.RefreshPeriod, c.InitialDelay, health.Check{ | ||
Name: c.Name, | ||
Timeout: c.Timeout, | ||
MaxTimeInError: c.MaxTimeInError, | ||
MaxContiguousFails: c.MaxContiguousFails, | ||
StatusListener: c.StatusListener, | ||
Check: c.Check, | ||
})) | ||
} | ||
|
||
return opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters