2024-09-22 20:48:09 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022-2024 The go-mail Authors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package mail
|
|
|
|
|
2024-09-22 21:12:59 +02:00
|
|
|
import (
|
2024-09-23 09:56:23 +02:00
|
|
|
"context"
|
2024-09-22 21:12:59 +02:00
|
|
|
"errors"
|
2024-09-23 09:56:23 +02:00
|
|
|
"fmt"
|
2024-09-22 21:12:59 +02:00
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
)
|
2024-09-22 20:48:09 +02:00
|
|
|
|
2024-09-23 11:17:58 +02:00
|
|
|
// Parts of the connection pool code is forked/took inspiration from https://github.com/fatih/pool/
|
|
|
|
// Thanks to Fatih Arslan and the project contributors for providing this great concurrency template.
|
2024-09-22 20:48:09 +02:00
|
|
|
|
2024-09-22 21:12:59 +02:00
|
|
|
var (
|
2024-09-23 10:09:38 +02:00
|
|
|
// ErrClosed is returned when an operation is attempted on a closed connection pool.
|
|
|
|
ErrClosed = errors.New("connection pool is closed")
|
|
|
|
// ErrNilConn is returned when a nil connection is passed back to the connection pool.
|
|
|
|
ErrNilConn = errors.New("connection is nil")
|
2024-09-23 09:56:23 +02:00
|
|
|
// ErrPoolInvalidCap is returned when the connection pool's capacity settings are
|
|
|
|
// invalid (e.g., initial capacity is negative).
|
2024-09-22 21:12:59 +02:00
|
|
|
ErrPoolInvalidCap = errors.New("invalid connection pool capacity settings")
|
|
|
|
)
|
|
|
|
|
2024-09-22 20:48:09 +02:00
|
|
|
// Pool interface describes a connection pool implementation. A Pool is
|
|
|
|
// thread-/go-routine safe.
|
|
|
|
type Pool interface {
|
|
|
|
// Get returns a new connection from the pool. Closing the connections returns
|
|
|
|
// it back into the Pool. Closing a connection when the Pool is destroyed or
|
|
|
|
// full will be counted as an error.
|
2024-09-23 13:44:03 +02:00
|
|
|
Get(ctx context.Context) (net.Conn, error)
|
2024-09-22 20:48:09 +02:00
|
|
|
|
|
|
|
// Close closes the pool and all its connections. After Close() the pool is
|
|
|
|
// no longer usable.
|
|
|
|
Close()
|
|
|
|
|
2024-09-23 10:33:06 +02:00
|
|
|
// Size returns the current number of connections of the pool.
|
|
|
|
Size() int
|
2024-09-22 20:48:09 +02:00
|
|
|
}
|
2024-09-22 21:12:59 +02:00
|
|
|
|
|
|
|
// connPool implements the Pool interface
|
|
|
|
type connPool struct {
|
2024-09-23 09:56:23 +02:00
|
|
|
// mutex is used to synchronize access to the connection pool to ensure thread-safe operations.
|
2024-09-22 21:12:59 +02:00
|
|
|
mutex sync.RWMutex
|
2024-09-23 09:56:23 +02:00
|
|
|
// conns is a channel used to manage and distribute net.Conn objects within the connection pool.
|
2024-09-22 21:12:59 +02:00
|
|
|
conns chan net.Conn
|
2024-09-23 09:56:23 +02:00
|
|
|
|
|
|
|
// dialCtxFunc represents the actual net.Conn returned by the DialContextFunc.
|
|
|
|
dialCtxFunc DialContextFunc
|
|
|
|
// dialNetwork specifies the network type (e.g., "tcp", "udp") used to establish connections in
|
|
|
|
// the connection pool.
|
|
|
|
dialNetwork string
|
|
|
|
// dialAddress specifies the address used to establish network connections within the connection pool.
|
|
|
|
dialAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PoolConn is a wrapper around net.Conn to modify the the behavior of net.Conn's Close() method.
|
|
|
|
type PoolConn struct {
|
|
|
|
net.Conn
|
|
|
|
mutex sync.RWMutex
|
|
|
|
pool *connPool
|
|
|
|
unusable bool
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 10:09:38 +02:00
|
|
|
// Close puts a given pool connection back to the pool instead of closing it.
|
|
|
|
func (c *PoolConn) Close() error {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
|
|
|
|
if c.unusable {
|
|
|
|
if c.Conn != nil {
|
|
|
|
return c.Conn.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.pool.put(c.Conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarkUnusable marks the connection not usable any more, to let the pool close it instead
|
|
|
|
// of returning it to pool.
|
|
|
|
func (c *PoolConn) MarkUnusable() {
|
|
|
|
c.mutex.Lock()
|
|
|
|
c.unusable = true
|
|
|
|
c.mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-09-22 21:12:59 +02:00
|
|
|
// NewConnPool returns a new pool based on buffered channels with an initial
|
|
|
|
// capacity and maximum capacity. The DialContextFunc is used when the initial
|
|
|
|
// capacity is greater than zero to fill the pool. A zero initialCap doesn't
|
|
|
|
// fill the Pool until a new Get() is called. During a Get(), if there is no
|
|
|
|
// new connection available in the pool, a new connection will be created via
|
|
|
|
// the corresponding DialContextFunc() method.
|
2024-09-23 09:56:23 +02:00
|
|
|
func NewConnPool(ctx context.Context, initialCap, maxCap int, dialCtxFunc DialContextFunc,
|
2024-09-23 13:44:03 +02:00
|
|
|
network, address string,
|
|
|
|
) (Pool, error) {
|
2024-09-22 21:12:59 +02:00
|
|
|
if initialCap < 0 || maxCap <= 0 || initialCap > maxCap {
|
|
|
|
return nil, ErrPoolInvalidCap
|
|
|
|
}
|
|
|
|
|
|
|
|
pool := &connPool{
|
2024-09-23 09:56:23 +02:00
|
|
|
conns: make(chan net.Conn, maxCap),
|
|
|
|
dialCtxFunc: dialCtxFunc,
|
|
|
|
dialAddress: address,
|
|
|
|
dialNetwork: network,
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 09:56:23 +02:00
|
|
|
// Initial connections for the pool. Pool will be closed on connection error
|
2024-09-22 21:12:59 +02:00
|
|
|
for i := 0; i < initialCap; i++ {
|
2024-09-23 09:56:23 +02:00
|
|
|
conn, err := dialCtxFunc(ctx, network, address)
|
|
|
|
if err != nil {
|
|
|
|
pool.Close()
|
2024-09-23 13:44:03 +02:00
|
|
|
return nil, fmt.Errorf("dialContextFunc is not able to fill the connection pool: %w", err)
|
2024-09-23 09:56:23 +02:00
|
|
|
}
|
|
|
|
pool.conns <- conn
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
2024-09-23 09:56:23 +02:00
|
|
|
// Get satisfies the Get() method of the Pool inteface. If there is no new
|
|
|
|
// connection available in the Pool, a new connection will be created via the
|
|
|
|
// DialContextFunc() method.
|
2024-09-23 13:44:03 +02:00
|
|
|
func (p *connPool) Get(ctx context.Context) (net.Conn, error) {
|
|
|
|
conns, dialCtxFunc := p.getConnsAndDialContext()
|
2024-09-23 09:56:23 +02:00
|
|
|
if conns == nil {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap the connections into the custom net.Conn implementation that puts
|
|
|
|
// connections back to the pool
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-09-23 13:44:03 +02:00
|
|
|
return nil, fmt.Errorf("failed to get connection: %w", ctx.Err())
|
2024-09-23 09:56:23 +02:00
|
|
|
case conn := <-conns:
|
|
|
|
if conn == nil {
|
|
|
|
return nil, ErrClosed
|
|
|
|
}
|
|
|
|
return p.wrapConn(conn), nil
|
|
|
|
default:
|
|
|
|
conn, err := dialCtxFunc(ctx, p.dialNetwork, p.dialAddress)
|
|
|
|
if err != nil {
|
2024-09-23 13:44:03 +02:00
|
|
|
return nil, fmt.Errorf("dialContextFunc failed: %w", err)
|
2024-09-23 09:56:23 +02:00
|
|
|
}
|
|
|
|
return p.wrapConn(conn), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close terminates all connections in the pool and frees associated resources. Once closed,
|
|
|
|
// the pool is no longer usable.
|
|
|
|
func (p *connPool) Close() {
|
|
|
|
p.mutex.Lock()
|
|
|
|
conns := p.conns
|
|
|
|
p.conns = nil
|
|
|
|
p.dialCtxFunc = nil
|
|
|
|
p.dialAddress = ""
|
|
|
|
p.dialNetwork = ""
|
|
|
|
p.mutex.Unlock()
|
|
|
|
|
|
|
|
if conns == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
close(conns)
|
|
|
|
for conn := range conns {
|
|
|
|
_ = conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 10:33:06 +02:00
|
|
|
// Size returns the current number of connections in the connection pool.
|
|
|
|
func (p *connPool) Size() int {
|
2024-09-23 13:44:03 +02:00
|
|
|
conns, _ := p.getConnsAndDialContext()
|
2024-09-23 09:56:23 +02:00
|
|
|
return len(conns)
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|
2024-09-23 09:56:23 +02:00
|
|
|
|
|
|
|
// getConnsAndDialContext returns the connection channel and the DialContext function for the
|
|
|
|
// connection pool.
|
2024-09-23 13:44:03 +02:00
|
|
|
func (p *connPool) getConnsAndDialContext() (chan net.Conn, DialContextFunc) {
|
2024-09-23 09:56:23 +02:00
|
|
|
p.mutex.RLock()
|
|
|
|
conns := p.conns
|
|
|
|
dialCtxFunc := p.dialCtxFunc
|
|
|
|
p.mutex.RUnlock()
|
2024-09-23 13:44:03 +02:00
|
|
|
return conns, dialCtxFunc
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 10:09:38 +02:00
|
|
|
// put puts a passed connection back into the pool. If the pool is full or closed,
|
|
|
|
// conn is simply closed. A nil conn will be rejected with an error.
|
|
|
|
func (p *connPool) put(conn net.Conn) error {
|
|
|
|
if conn == nil {
|
|
|
|
return ErrNilConn
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mutex.RLock()
|
|
|
|
defer p.mutex.RUnlock()
|
|
|
|
|
|
|
|
if p.conns == nil {
|
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case p.conns <- conn:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 09:56:23 +02:00
|
|
|
// wrapConn wraps a given net.Conn with a PoolConn, modifying the net.Conn's Close() method.
|
|
|
|
func (p *connPool) wrapConn(conn net.Conn) net.Conn {
|
|
|
|
poolconn := &PoolConn{pool: p}
|
|
|
|
poolconn.Conn = conn
|
|
|
|
return poolconn
|
2024-09-22 21:12:59 +02:00
|
|
|
}
|