|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
// Package manager provides basic manager functions to embed into higher level managers
|
|
|
|
|
// Package manager provides basic manager functions to embed into higher level managers.
|
|
|
|
|
package manager
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
@ -17,7 +17,7 @@ var (
|
|
|
|
|
ErrMaxAttemptsExceeded = errors.New("max connection attempts exceeded")
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ManagerStatus is used as an enum for the current status
|
|
|
|
|
// ManagerStatus is used as an enum for the current status.
|
|
|
|
|
// Could be expanded to include others such as killed, sleeping, etc.
|
|
|
|
|
type ManagerStatus int
|
|
|
|
|
|
|
|
|
@ -26,20 +26,20 @@ const (
|
|
|
|
|
Active ManagerStatus = 1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// MaxConnAttempts is the maximum allowable connection attempts
|
|
|
|
|
// Limited to 255 to prevent excessive timeout scaling
|
|
|
|
|
// MaxConnAttempts is the maximum allowable connection attempts.
|
|
|
|
|
// Limited to 255 to prevent excessive timeout scaling.
|
|
|
|
|
const MaxConnAttempts = 0xFF
|
|
|
|
|
|
|
|
|
|
// Manager is a general purpose structure to implement basic capabilities
|
|
|
|
|
// Tracks its state via Active, modifying it on starts and stops
|
|
|
|
|
// Embeds a connection to be used in generating timeouts
|
|
|
|
|
// Manager is a general purpose structure to implement basic capabilities.
|
|
|
|
|
// Stores state in active variable, modified through atomic swaps.
|
|
|
|
|
// Embeds a connection to be used in generating timeouts.
|
|
|
|
|
type Manager struct {
|
|
|
|
|
*connection // embedded for timeout stuff
|
|
|
|
|
active int32 // atomic checks
|
|
|
|
|
*connection
|
|
|
|
|
active int32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new manager with the maxConn maximum attempts
|
|
|
|
|
// Throws ErrInvalidMaxConn if maxConn is not in [0, MaxConnAttempts]
|
|
|
|
|
// New creates a new manager with the maxConn maximum attempts.
|
|
|
|
|
// Throws ErrInvalidMaxConn if maxConn is not in [0, MaxConnAttempts].
|
|
|
|
|
func New(maxConn int) (*Manager, error) {
|
|
|
|
|
|
|
|
|
|
if maxConn < 0 || maxConn > MaxConnAttempts {
|
|
|
|
@ -55,8 +55,8 @@ func New(maxConn int) (*Manager, error) {
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start attempts to start the manager
|
|
|
|
|
// Throws ErrManagerActive error if the manager is already active
|
|
|
|
|
// Start attempts to start the manager.
|
|
|
|
|
// Throws ErrManagerActive error if the manager is already active.
|
|
|
|
|
func (m *Manager) Start() error {
|
|
|
|
|
if atomic.CompareAndSwapInt32(&m.active, 0, 1) {
|
|
|
|
|
m.ResetConnections()
|
|
|
|
@ -66,8 +66,8 @@ func (m *Manager) Start() error {
|
|
|
|
|
return ErrManagerActive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop attempts to stop the manager
|
|
|
|
|
// Throws ErrManagerInactive error if the manager is already inactive
|
|
|
|
|
// Stop attempts to stop the manager.
|
|
|
|
|
// Throws ErrManagerInactive error if the manager is already inactive.
|
|
|
|
|
func (m *Manager) Stop() error {
|
|
|
|
|
if atomic.CompareAndSwapInt32(&m.active, 1, 0) {
|
|
|
|
|
return nil
|
|
|
|
@ -76,15 +76,15 @@ func (m *Manager) Stop() error {
|
|
|
|
|
return ErrManagerInactive
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsActive returns the current ManagerStatus
|
|
|
|
|
// IsActive returns the current ManagerStatus.
|
|
|
|
|
func (m *Manager) IsActive() ManagerStatus {
|
|
|
|
|
return ManagerStatus(atomic.LoadInt32(&m.active))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HeartBeat will send an empty struct over ping every hb (scale)
|
|
|
|
|
// The pings are sent ever (hb + rand(interval)) * scale
|
|
|
|
|
// HeartBeat will send an empty struct over ping every hb (scale).
|
|
|
|
|
// The pings are sent ever (hb + rand(interval)) * scale.
|
|
|
|
|
// Where scale is typically time.Millisecond, time.Second etc.
|
|
|
|
|
// Will close the channel on exit to prevent leaks
|
|
|
|
|
// Will close the channel on exit to prevent leaks.
|
|
|
|
|
func (m *Manager) HeartBeat(
|
|
|
|
|
ping chan struct{},
|
|
|
|
|
hb, interval int,
|
|
|
|
@ -105,17 +105,17 @@ func (m *Manager) HeartBeat(
|
|
|
|
|
close(ping)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// connection keeps track of maximum and current number of connection attempts
|
|
|
|
|
// Concurrency safe as it is protected by a mutex
|
|
|
|
|
// connection keeps track of maximum and current number of connection attempts.
|
|
|
|
|
// Concurrency safe as it is protected by a mutex.
|
|
|
|
|
type connection struct {
|
|
|
|
|
Attempts float64
|
|
|
|
|
MaxAttempts int
|
|
|
|
|
sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Timeout returns an exponentially decaying timeout based on attempts
|
|
|
|
|
// Returns timeout of type time.Duration in milliseconds
|
|
|
|
|
// Returns ErrMaxAttemptsExceeded if too many attempts are tried
|
|
|
|
|
// Timeout returns an exponentially decaying timeout based on attempts.
|
|
|
|
|
// Returns timeout of type time.Duration in milliseconds.
|
|
|
|
|
// Returns ErrMaxAttemptsExceeded if too many attempts are tried.
|
|
|
|
|
func (c *connection) Timeout() (time.Duration, error) {
|
|
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
@ -130,7 +130,7 @@ func (c *connection) Timeout() (time.Duration, error) {
|
|
|
|
|
return 0, ErrMaxAttemptsExceeded
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ResetConnections sets the current connection attempts back to 0
|
|
|
|
|
// ResetConnections sets the current connection attempts back to 0.
|
|
|
|
|
func (c *connection) ResetConnections() {
|
|
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
|
|
|
|