tcppool

package module
v0.1.31 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewExponentialBackoff

func NewExponentialBackoff(baseDelay, maxDelay uint) backoff.Backoff

NewExponentialBackoff creates a new exponential backoff strategy. The delay between retries doubles with each attempt until reaching the maximum delay.

Parameters:

  • baseDelay: The initial delay in seconds for the first retry.
  • maxDelay: The maximum delay in seconds for retries.

Returns:

  • A backoff.Backoff implementation using exponential backoff.

func NewFibonacciBackoff

func NewFibonacciBackoff(maxDelay uint) backoff.Backoff

NewFibonacciBackoff creates a new Fibonacci backoff strategy. The delay between retries follows the Fibonacci sequence until reaching the maximum delay.

Parameters:

  • maxDelay: The maximum delay in seconds for retries.

Returns:

  • A backoff.Backoff implementation using Fibonacci backoff.

func NewFixedBackoff

func NewFixedBackoff(interval uint) backoff.Backoff

NewFixedBackoff creates a new fixed backoff strategy. The delay between retries remains constant.

Parameters:

  • interval: The fixed delay in seconds between retries.

Returns:

  • A backoff.Backoff implementation using fixed backoff.

func NewLinearBackoff

func NewLinearBackoff(scalar uint) backoff.Backoff

NewLinearBackoff creates a new linear backoff strategy. The delay between retries increases linearly with each attempt.

Parameters:

  • scalar: The constant value added to the delay for each retry.

Returns:

  • A backoff.Backoff implementation using linear backoff.

func NewPolynomialBackoff

func NewPolynomialBackoff(exponent uint) backoff.Backoff

NewPolynomialBackoff creates a new polynomial backoff strategy. The delay between retries follows a polynomial growth pattern.

Parameters:

  • exponent: The exponent used for calculating delay growth.

Returns:

  • A backoff.Backoff implementation using polynomial backoff.

Types

type Config

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

Config represents the configuration for a connection pool. It encapsulates various settings like connection limits, timeouts, retries, backoff strategies, and event hooks.

func NewConfig

func NewConfig(
	address, name string,
	maxConnections int,
	connTimeout, idleTimeout time.Duration,
	maxRetries uint,
	backoff backoff.Backoff,
	hooks PoolHooks,
) *Config

NewConfig creates a new Config object with the specified parameters.

Parameters:

  • address: The network address for the pool's connections.
  • name: A custom name for the pool (if empty, it's generated based on the address).
  • maxConnections: The maximum number of active connections in the pool.
  • connTimeout: The timeout for establishing new connections.
  • idleTimeout: The timeout for cleaning up idle connections.
  • maxRetries: The maximum number of retries for failed connections.
  • backoff: The backoff strategy for retrying failed connections.
  • hooks: A set of custom hooks for pool events.

Returns:

  • A pointer to the created Config object.

type Pool

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

Pool represents a connection pool that manages TCP connections. It provides methods to acquire and release connections, as well as to fetch them asynchronously.

func New

func New(c Config) (*Pool, error)

New creates a new Pool instance based on the given configuration. It initializes an internal connection pool and returns a Pool object.

Parameters:

  • c: A Config object containing the pool configuration.

Returns:

  • A pointer to the created Pool.
  • An error, if the pool initialization fails.

func (*Pool) Get

func (p *Pool) Get() (net.Conn, error)

Get retrieves a connection from the pool. If an idle connection is available, it is returned; otherwise, a new connection is created.

Returns:

  • A net.Conn representing the connection.
  • An error, if the connection retrieval fails.

func (*Pool) GetAsync

func (p *Pool) GetAsync() <-chan struct {
	Conn net.Conn
	Err  error
}

GetAsync retrieves a connection from the pool asynchronously. It returns a channel through which the result (connection or error) will be sent once available.

Returns:

  • A read-only channel of a struct containing:
  • Conn: A net.Conn representing the connection.
  • Err: An error, if the connection retrieval fails.

func (*Pool) Release

func (p *Pool) Release(conn net.Conn) error

Release returns a previously acquired connection back to the pool. If the pool is full, the connection is closed.

Parameters:

  • conn: The connection to be released.

Returns:

  • An error, if the release process fails.

type PoolHooks

type PoolHooks struct {
	// OnConnectionCreate is triggered when a new connection is created.
	OnConnectionCreate func(conn net.Conn)
	// OnConnectionAcquire is triggered when a connection is acquired from the pool.
	OnConnectionAcquire func(conn net.Conn)
	// OnConnectionRelease is triggered when a connection is released back into the pool.
	OnConnectionRelease func(conn net.Conn)
	// OnConnectionClose is triggered when a connection is closed.
	OnConnectionClose func(conn net.Conn)
	// OnConnectionError is triggered when an error occurs during connection operations.
	OnConnectionError func(err error)
	// OnPoolCreate is triggered when the connection pool is successfully created.
	OnPoolCreate func(c Config)
	// OnPoolCreateError is triggered when there is an error during pool creation.
	OnPoolCreateError func(err error)
}

PoolHooks defines callback functions that can be triggered during various connection pool events. These hooks allow custom logic to be executed during connection creation, acquisition, release, closure, errors, or during pool creation.

func (PoolHooks) ToInternal

func (h PoolHooks) ToInternal() internal.PoolHooks

ToInternal converts a public PoolHooks object to the corresponding internal representation. This is used to pass hooks from the public API to the internal pool implementation.

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

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