watchdog

package module
v0.0.0-...-84cf7cb Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2018 License: MIT Imports: 4 Imported by: 5 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Watchdog

GoDoc Build Status Maintainability Test Coverage

Go library for automatic service reconnection.

This library depends on the backoff library, which defines structures for creating backoff interval generator.

Example

First, you must define a type that conforms to the Retry interface. The interface has a single method. In this example, the method attempts to dial a connection to a Riemann server and returns true on success and false on failure.

type RiemannService struct {
	conn      *riemann.Conn
	watcher   *watchdog.Watcher
	connected <-chan struct{}
}

func (rs *RiemannService) Retry() bool {
	conn, err := riemann.Dial("localhost:5555")
	if err != nil {
		return false
	}

	rs.conn = conn
	return true
}

In order to ensure the application maintains a valid connection, create a watcher which will attempt to reconnect to the service in another goroutine until success. In between attempts the watcher will wait a configured interval (one second in this example).

func NewRiemannService() *RiemannService {
	rs := &RiemannService{}

	// Create watcher on Riemann service
	w := NewWatcher(rs, NewConstantBackoff(time.Second))

	connected := w.Start() // Connect initially,
	<-connected            // Block until success

	rs.watcher = w
	rs.connected = connected
	return rs
}

If you're using the service and detect a connection issue, you can alert the watcher so that it attempts to reconnect. On successful reconnect, the watcher sends a value on a channel returned by the Start method. You can block on this read (to ensure a valid connection), or you can read this value in a goroutine (but must be read to prevent deadlocks within the watcher's background reconnect goroutine).

func (rs *RiemannService) Write() {
	if conn == nil {
		rs.watcher.Check() // Alert watcher of failure
		<-rs.connected     // Block until success
	}

	// Write omitted
}

Once you're done with the service, you should stop the watcher so that the background reconnect goroutines can be shut down.

func (rs *RiemannService) Close() {
	rs.watcher.Stop()
	rs.conn.Close()
}

License

Copyright (c) 2015 Eric Fritz

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockUntilSuccess

func BlockUntilSuccess(ctx context.Context, retry Retry, backoff backoff.Backoff) bool

BlockUntilSuccess creates a transient watcher that fires the given retry function until success. This method takes a context object which will, if canceled, will stop the watcher. Returns if the function succeeds and false if the method was canceled.

Types

type Retry

type Retry interface {
	// Some critical action, which should return true on success.
	Retry() bool
}

Retry is the interface to something which are invoked until success.

type RetryFunc

type RetryFunc func() bool

RetryFunc is a function that can be applied as a Retry.

func (RetryFunc) Retry

func (f RetryFunc) Retry() bool

Retry will execute the RetryFunc.

type Watcher

type Watcher interface {
	// Start will *immediately* attempt to invoke the retry function. The
	// watcher will re-invoke the retry function on failure, with a delay
	// in between tries. On success, the watcher waits for a retry signal,
	// at which point the process repeats. The channel returned will receive
	// a value after the retry function returns true. The user should read a
	// value from this channel after a retry request signal is sent, as this
	// channel is un-buffered.
	Start() <-chan struct{}

	// Stop updates the watcher so that no future calls to the retry function
	// are attempted.
	Stop()

	// Check requests watcher to re-invoke the retry function until success.
	// If the watcher is already in a retry cycle, then this function should
	// have no observable effect. This method must not be called after Stop.
	Check()
}

Watcher invokes a Retry function until success.

func NewWatcher

func NewWatcher(retry Retry, backoff backoff.Backoff) Watcher

NewWatcher creates a new watcher with the given retry function and interval generator.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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