Documentation
¶
Index ¶
- Constants
- Variables
- type Check
- type CheckFunc
- type CheckResult
- type Watchdog
- func (w *Watchdog) AddChecks(checks ...Check)
- func (w *Watchdog) ListChecks() []Check
- func (w *Watchdog) RemoveChecks(names ...string)
- func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error)
- func (w *Watchdog) SetTimeout(d time.Duration)
- func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error)
- func (w *Watchdog) Stop() error
Constants ¶
const DefaultTimeout = time.Second * 10
DefaultTimeout is used to limit checks duration
Variables ¶
var ( ErrNotConfigured = errors.New("no checks configured") ErrNotRunning = errors.New("watchdog is not running") )
Functions ¶
This section is empty.
Types ¶
type Check ¶
type Check struct {
Name string // indentifier of this check
Interval time.Duration // how often the check must run (if running periodically with Start method)
Check CheckFunc // function to run
}
Check represents a check that must be run by Watchdog.
type CheckFunc ¶
CheckFunc is a function that does the actual work. This package provides a number of check functions but any function matching the signature may be provided. It must obey context dealine and cancellation.
func DialTCP ¶
DialTCP creates a CheckFunc that may be used to check tcp connectivity to a host.
The check tries to net.DialTimeout to the provided addr. If it fails, the returned status is StatusDown.
No validation of addr is made.
If zero timeout is provided then the DefaultTimeout (10 second) is used.
func GetHTTP ¶
GetHTTP creates a CheckFunc that operates as follows. The check makes a request with GET method to provided addr using http.DefaultClient. If request fails within specified timeout the returned status is StatusDown. The function then tries to read response body. If it fails, the returned status is StatusDown.
If request succeeds but reponse code is not 200, the returned status is StatusDown and response body is contained in the returned error.
GetHTTP return an error if addr can not be parsed with url.Parse. If zero timeout is provided then the DefaultTimeout (10 second) is used.
func HeadHTTP ¶
HeadHTTP creates a CheckFunc that operates as follows. The check make a request with HEAD method to provided addr using http.DefaultClient. If request fails within specified timeout the returned status is StatusDown.
If request succeeds but reponse code is not 200, the returned status is StatusDown.
HeadHTTP return an error if addr can not be parsed with url.Parse. If zero timeout is provided then the DefaultTimeout (10 second) is used.
type CheckResult ¶
type Watchdog ¶
type Watchdog struct {
// contains filtered or unexported fields
}
Watchdog keeps checks to run either periodically or on demand.
func (*Watchdog) AddChecks ¶
AddChecks adds checks to the group. If monitoring is in progress then monitoring it started for the newly added check as well. Check may have not have duplicate Name fields. New check with the same hame overwrites the previous one.
func (*Watchdog) ListChecks ¶
func (*Watchdog) RemoveChecks ¶
RemoveChecks removes the named checks.
func (*Watchdog) RunImmediately ¶
RunImmediately runs configured checks concurrently and returns results. Setting concurrency to 0 means that all check of the group are allowed to run simultaneously. Otherwise at most concurrency checks will be allowed to run simultaneously.
func (*Watchdog) SetTimeout ¶
SetTimeout sets timeout for all checks that get started with Start method. Changing this value does not affect running checks. Watchdog does not enforce this timeout, it just passes context.WithTimemout to check functions. If this method is not called the default timeout of 10 seconds is used.
func (*Watchdog) Start ¶
func (w *Watchdog) Start(concurrency int) (<-chan CheckResult, error)
Start starts monitoring. Subsequent calls to start return the SAME channel. If you need to have more that one reader from the channel - fan out on your side. On start Watchdog runs all provided check and pushes current status of checks of to the channel. Subsequently if check func returns different status or if it returns an error the result is pushed to the channel. Concurrency argument limits the number of checks that can run concurrently. 0 means no limit (all checks may run concurrently).