fixed documentation issues

main
KeeganForelight 2 years ago
parent 4914b93408
commit b622ac321d

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

Loading…
Cancel
Save