heartbeat

package
v2.5.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2016 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeFormat is a time format string according to the time
	// package and is used to marshal and unmarshall time.Time instances
	// into ISO8601-compliant strings.
	//
	// See: https://www.ietf.org/rfc/rfc3339.txt for more details.
	DefaultTimeFormat string = "2006-01-02T15:04:05"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Detector

type Detector interface {
	// Detect returns all dead items according to the implementation
	// definition of "dead". If an error is encountered while processing, it
	// will be returned, and execution of this function will be halted.
	Detect() (expired []string, err error)

	// Removes a (supposedly dead) worker from the heartbeating data structure.
	Purge(id string) error
}

Detector is an interface to a type responsible for collecting "dead" items in Redis.

func NewDetector

func NewDetector(location string, pool *redis.Pool, strategy Strategy) Detector

NewDetector initializes and returns a new SimpleDetector instance with the given parameters.

type HashExpireyStrategy

type HashExpireyStrategy struct {
	MaxAge time.Duration
}

HashExpireyStrategy is an implementation of Strategy that stores items in a hash.

func (HashExpireyStrategy) Expired

func (s HashExpireyStrategy) Expired(location string,
	pool *redis.Pool) (expired []string, err error)

Expired implements the `func Expired` defined on the Strategy interface. It scans iteratively over the Heart's `location` field to look for items that have expired. An item is marked as expired iff the last update time happened before the instant of the maxAge subtracted from the current time.

func (HashExpireyStrategy) Purge

func (s HashExpireyStrategy) Purge(location, ID string, pool *redis.Pool) error

Purge implements the `func Purge` defined in the Strategy interface. It assumes a HASH type is used in Redis to map the IDs of various Hearts, and removes the record for the specified ID from the hash.

func (HashExpireyStrategy) Touch

func (s HashExpireyStrategy) Touch(location, ID string, pool *redis.Pool) error

Touch implements the `func Touch` defined in the Strategy interface. It assumes a HASH type is used in Redis to map the IDs of various Hearts to the last time that they were updated.

It uses the Heart's `Location` and `ID` fields respectively to determine where to both place, and name the hash as well as the items within it.

Times are marshalled using the `const DefaultTimeFormat` which stores times in the ISO8601 format.

type Heart

type Heart interface {
	// Close stops the update-task from running, and frees up any owned
	// resources.
	Close()

	// Errs returns a read-only `<-chan error` which contains all errors
	// encountered while runnning the update task.
	Errs() <-chan error
}

Heart is a interface representing a process which runs a particular task at a given interval within its own goroutine.

type Heartbeater

type Heartbeater struct {
	// ID is a unique identifier used by the Heart to tick with.
	ID string

	// Location is the absolute path of the keyspace in Redis in which to
	// store all of the heartbeat updates.
	Location string

	// Strategy is the strategy to use both to tick the items in Redis, but
	// also to determine which ones are still alive.
	Strategy Strategy
	// contains filtered or unexported fields
}

Heartbeater serves as a factory-type, in essence, to create both Hearts and Detectors. It maintains information about the ID used to heartbeat with, the Location in which to store ticks, the interval at which to update that location, the pool in which to maintain and recycle Redis connections to, and the strategy to use to tick and recover items in Redis.

func New

func New(id, location string, interval time.Duration, pool *redis.Pool) *Heartbeater

New allocates and returns a pointer to a new instance of a Heartbeater. It takes in the id, location, interval and pool with which to use to create Hearts and Detectors.

func (*Heartbeater) Detector

func (h *Heartbeater) Detector() Detector

Detectors creates and returns a new instance of the Detector type with the parameters used by the Heartbeater for consistency.

func (*Heartbeater) Heart

func (h *Heartbeater) Heart() Heart

Heart creates and returns a new instance of the Heart type with the parameters used by the Heartbeater for consistency.

func (*Heartbeater) Interval

func (h *Heartbeater) Interval() time.Duration

Interval returns the interval at which the heart should tick itself.

func (*Heartbeater) MaxAge

func (h *Heartbeater) MaxAge() time.Duration

MaxAge returns the maximum amount of time that can pass from the last tick of an item before that item may be considered dead. By default, it is three halves of the Interval() time, but is only one second if the interval time is less than five seconds.

func (*Heartbeater) SetStrategy

func (h *Heartbeater) SetStrategy(strategy Strategy) *Heartbeater

SetStrategy changes the strategy used by all future Heart and Detector instantiations.

type SimpleDetector

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

SimpleDetector is an implementation of the Detector interface which uses the provided Strategy in order to determine which items may be considered dead.

func (SimpleDetector) Detect

func (d SimpleDetector) Detect() (expired []string, err error)

Detect implements the `func Detect` on the `type Detector interface`. This implementation simply delegates into the provided Strategy.

func (SimpleDetector) Location

func (d SimpleDetector) Location() string

Location returns the keyspace in which the detector searches.

func (SimpleDetector) Purge

func (d SimpleDetector) Purge(id string) (err error)

Purge implements the `func Purge` on the `type Detector interface`. This implementation simply delegates into the provided Strategy.

func (SimpleDetector) Strategy

func (d SimpleDetector) Strategy() Strategy

Strategy returns the strategy that the detector uses to search in the keyspace returned by Location().

type SimpleHeart

type SimpleHeart struct {
	ID       string
	Location string
	Interval time.Duration
	// contains filtered or unexported fields
}

SimpleHeart is a implementation of the `type Heart interface` which uses a given ID, Location and Interval, to call the provided Strategy in order to tick items in Redis.

func NewSimpleHeart

func NewSimpleHeart(id, location string, interval time.Duration, pool *redis.Pool,
	strategy Strategy) SimpleHeart

New allocates and returns a new instance of a SimpleHeart, initialized with the given parameters.

pool is the *redis.Pool of which the Heart will take connections from.

location is the location in the Redis keyspace wherein the heartbeat will be stored. Similarily, `id` is the ID of the given Heart that will be touched in that location.

strategy is the Strategy that will be used to Touch() the keyspace in Redis at the given interval.

Both the closer and errs channel are initialized to new, empty channels using the builtin `make()`.

It begins ticking immediately.

func (SimpleHeart) Close

func (s SimpleHeart) Close()

Close implements the `func Close` defined in the `type Heart interface`. It stops the heartbeat process at the next tick.

func (SimpleHeart) Errs

func (s SimpleHeart) Errs() <-chan error

Errs implements the `func Errs` defined in the `type Heart interface`. It returns a read-only channel of errors encountered during the heartbeat operation.

type Strategy

type Strategy interface {
	// Touch ticks the item at `location:ID` in Redis.
	Touch(location, ID string, pool *redis.Pool) (err error)

	// Removes an item at `location:ID` in Redis.
	Purge(location, ID string, pool *redis.Pool) (err error)

	// Expired returns an array of strings that represent the expired IDs in
	// a given keyspace as specified by the `location` parameter.
	Expired(location string, pool *redis.Pool) (expired []string, err error)
}

Strategy is an interface to a type which is responsible for both ticking a heart's "pulse," and detecting what items in a particular section of Redis are to be considered dead.

Jump to

Keyboard shortcuts

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