watchdog

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 10 Imported by: 0

README

watchdog

go get code.uint32.ru/tiny/watchdog

This package is a dead simple monitoring system which may be used to check availability of online resources.

It is a work in progress, but also a working code with decent test coverage. API might change in later versions as well as more features might be added.

Documentation

Index

Constants

View Source
const DefaultTimeout = time.Second * 10

DefaultTimeout is used to limit checks duration

Variables

View Source
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

type CheckFunc func(context.Context) error

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

func DialTCP(addr string, timeout time.Duration) (CheckFunc, error)

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

func GetHTTP(addr string, timeout time.Duration) (CheckFunc, error)

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

func HeadHTTP(addr string, timeout time.Duration) (CheckFunc, error)

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 CheckResult struct {
	Name  string // identifier of check
	Error error  // error returned by CheckFunc
}

type Watchdog

type Watchdog struct {
	// contains filtered or unexported fields
}

Watchdog keeps checks to run either periodically or on demand.

func New

func New(checks ...Check) *Watchdog

New creates instance of Watchdog with provided checks.

func (*Watchdog) AddChecks

func (w *Watchdog) AddChecks(checks ...Check)

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 (w *Watchdog) ListChecks() []Check

func (*Watchdog) RemoveChecks

func (w *Watchdog) RemoveChecks(names ...string)

RemoveChecks removes the named checks.

func (*Watchdog) RunImmediately

func (w *Watchdog) RunImmediately(ctx context.Context, concurrency int) ([]CheckResult, error)

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

func (w *Watchdog) SetTimeout(d time.Duration)

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).

func (*Watchdog) Stop

func (w *Watchdog) Stop() error

Stop stops execution of checks. Subsequent calls return ErrNotRunning.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL