healthcheck

package
v0.0.0-...-890cd7e Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: GPL-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoNetWork = errors.New("no network")
)

errors

Functions

This section is empty.

Types

type HealthCheck

type HealthCheck struct {
	Storage *Storages
	// contains filtered or unexported fields
}

HealthCheck is the health checker for balancers

func New

func New(
	router adapter.Router,
	providers []adapter.Provider, providersByTag map[string]adapter.Provider,
	options *option.HealthCheckOptions, logger log.Logger,
) *HealthCheck

New creates a new HealthPing with settings.

The globalHistory is optional and is only used to sync latency history between different health checkers. Each HealthCheck will maintain its own history storage since different ones can have different check destinations, sampling numbers, etc.

func (*HealthCheck) CheckAll

func (h *HealthCheck) CheckAll(ctx context.Context) (map[string]uint16, error)

CheckAll performs checks for nodes of all providers

func (*HealthCheck) CheckOutbound

func (h *HealthCheck) CheckOutbound(ctx context.Context, tag string) (uint16, error)

CheckOutbound performs check for the specified node

func (*HealthCheck) CheckProvider

func (h *HealthCheck) CheckProvider(ctx context.Context, tag string) (map[string]uint16, error)

CheckProvider performs checks for nodes of the provider

func (*HealthCheck) Close

func (h *HealthCheck) Close() error

Close stops the health check service, implements adapter.Service

func (*HealthCheck) InterfaceUpdated

func (h *HealthCheck) InterfaceUpdated() error

InterfaceUpdated implements adapter.InterfaceUpdateListener

func (*HealthCheck) ReportFailure

func (h *HealthCheck) ReportFailure(outbound adapter.Outbound)

ReportFailure reports a failure of the node

func (*HealthCheck) Start

func (h *HealthCheck) Start() error

Start starts the health check service, implements adapter.Service

type History

type History struct {
	Time  time.Time `json:"time"`
	Delay RTT       `json:"delay"`
}

History is the rtt history

type MetaData

type MetaData struct {
	sync.Mutex

	context.Context
	// contains filtered or unexported fields
}

MetaData is the context for health check, it collects network connectivity status and checked status of outbounds

About connectivity status collection:

Consider the health checks are done asynchronously, success checks will report network is available in a short time, after that, there will be failure checks query the network connectivity. So,

1. In cases of any one check success, the network is known to be available, no extra connectivity check needed.

2. In cases of all checks failed, we can not distinguesh from the network is down or all nodes are dead. Only in this case we need to do connectivity check, and it's rare.

func NewMetaData

func NewMetaData(ctx context.Context, connectivityURL string) *MetaData

NewMetaData creates a new MetaData

func (*MetaData) Checked

func (c *MetaData) Checked(tag string) bool

Checked tells if the outbound of the tag is checked

func (*MetaData) Connected

func (c *MetaData) Connected() bool

Connected tells if the network connected

func (*MetaData) ReportChecked

func (c *MetaData) ReportChecked(tag string)

ReportChecked reports the outbound of the tag is checked

func (*MetaData) ReportConnected

func (c *MetaData) ReportConnected()

ReportConnected reports the network is connected

type RTT

type RTT uint16

RTT is the round trip time with underlying type uint16, precision is millisecond

const (
	// Failed is a special value to indicate a failed check
	// or unable to get a valid result, e.g.: average RTT
	// will be Fail(0) if a node is never tested.
	Failed RTT = 0

	Millisecond RTT = 1
	Second      RTT = 1000
)

RTT constant values

func RTTOf

func RTTOf[T preciseDuration](d T) RTT

RTTOf converts a precise duration (underlying type int64, like `time.Duration`) to `rtt.Duration`

func RTTsOf

func RTTsOf[T preciseDuration](values []T) []RTT

RTTsOf converts precise durations (underlying type int64, like `[]time.Duration`) to `[]rtt.Duration`

func (RTT) String

func (r RTT) String() string

func (RTT) TimeDuration

func (r RTT) TimeDuration() time.Duration

TimeDuration converts a rtt.Duration to time.Duration

type Stats

type Stats struct {
	All       int // total number of health checks
	Fail      int // number of failed health checks
	Deviation RTT // standard deviation of RTTs
	Average   RTT // average RTT of all health checks
	Max       RTT // maximum RTT of all health checks
	Min       RTT // minimum RTT of all health checks
	Latest    RTT // latest RTT of all health checks

	Expires time.Time // time of the statistics expires
}

Stats is the statistics of RTTs

type Storage

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

Storage holds ping rtts for health Checker, it's not thread safe

func NewStorage

func NewStorage(cap uint, validity time.Duration) *Storage

NewStorage returns a new rtt storage with specified capacity

func (*Storage) All

func (s *Storage) All() []*History

All returns all the history, ignores the validity

func (*Storage) Get

func (s *Storage) Get(offset int) *History

Get gets the history at the offset to the latest history, ignores the validity

func (*Storage) Latest

func (s *Storage) Latest() *History

Latest gets the latest history, alias of Get(0)

func (*Storage) Put

func (s *Storage) Put(d RTT)

Put puts a new rtt to the HealthPingResult

func (*Storage) Stats

func (s *Storage) Stats() Stats

Stats get statistics and write cache for next call Make sure use Mutex.Lock() before calling it, RWMutex.RLock() is not an option since it writes cache

type Storages

type Storages struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Storages is the storages for different tags (nodes)

func NewStorages

func NewStorages(cap uint, validity time.Duration) *Storages

NewStorages returns a new Storages

func (*Storages) All

func (s *Storages) All(tag string) []*History

All gets all histories for the tag

func (*Storages) Delete

func (s *Storages) Delete(tag string)

Delete remove the histories storage for the tag

func (*Storages) Latest

func (s *Storages) Latest(tag string) *History

Latest gets the latest history for the tag

func (*Storages) List

func (s *Storages) List() []string

List returns the storage list

func (*Storages) Put

func (s *Storages) Put(tag string, delay RTT)

Put gets all histories for the tag

func (*Storages) Stats

func (s *Storages) Stats(tag string) Stats

Stats gets the statistics of all histories for the tag

Jump to

Keyboard shortcuts

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