watchops

package
v0.0.0-...-39e3719 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckResult

type CheckResult struct {
	URL string
	// http response code
	Code int
	// total request time
	Duration time.Duration
	// extras
	// time the dns lookup took
	DNS time.Duration
	// time the tls handshake took
	TLS time.Duration
	// how long the http client took to establich a connection
	Conn time.Duration
}

CheckResult models the data that is returned from checking a url

type Watcher

type Watcher struct {
	// the url that the watcher checks for liveness
	URL string
	// how long to allow an http request to take before terminating
	Timeout time.Duration
	// how long to wait in between checks
	Interval time.Duration
	// a channel that lets the manager cancel a watcher service
	Cancel chan bool
	// a tool for ensuring all watchers close before a manager exits
	WaitGroup *sync.WaitGroup
	// the channel where metrics can be sent for serving
	Metrics chan CheckResult
}

Watcher contains the data and methods for an individual url wather service

func NewWatcher

func NewWatcher(url string, interval, timeout time.Duration, wg *sync.WaitGroup, metrics chan CheckResult) *Watcher

NewWatcher returns a new Watcher struct

func (*Watcher) CheckURL

func (w *Watcher) CheckURL(pctx context.Context) (*CheckResult, error)

CheckURL performs a GET request on the given url and returns a response code and time

func (*Watcher) Run

func (w *Watcher) Run(pctx context.Context) error

Run is the loop where the actual work of the watcher takes place

type WatcherManager

type WatcherManager struct {
	Watchers  []*Watcher
	Timeout   time.Duration
	Interval  time.Duration
	WaitGroup *sync.WaitGroup
	Metrics   chan CheckResult
	Port      int
}

WatcherManager handles multiple wathcer services allowing them to be run concurrently

func NewWatcherManager

func NewWatcherManager(interval, timeout string, port int) (*WatcherManager, error)

NewWatcherManager validates inputs and returns a new manager

func (*WatcherManager) AddWatcher

func (wm *WatcherManager) AddWatcher(ctx context.Context, url string) error

AddWatcher creates a new Watcher from a url and registers it

func (*WatcherManager) MetricsCatcher

func (wm *WatcherManager) MetricsCatcher()

MetricsCatcher watches the metrics channel for data sent from watchers. When it receives data it updates the metrics accordingly.

func (*WatcherManager) Stop

func (wm *WatcherManager) Stop()

Stop cancels all the watchers and closes the metrics channel

func (*WatcherManager) WaitAndWatch

func (wm *WatcherManager) WaitAndWatch(ctx context.Context) error

WaitAndWatch starts the watchers that have been registered and waits until they all close.

Jump to

Keyboard shortcuts

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