Documentation ¶
Overview ¶
Package connpool provides the connection pool.
Index ¶
- Variables
- func Dial(opts *DialOptions) (net.Conn, error)
- type ConnectionPool
- type DialFunc
- type DialOptions
- type GetOptions
- func (o *GetOptions) WithContext(ctx context.Context)
- func (o *GetOptions) WithCustomReader(customReader func(io.Reader) io.Reader)
- func (o *GetOptions) WithDialTLS(certFile, keyFile, caFile, serverName string)
- func (o *GetOptions) WithDialTimeout(dur time.Duration)
- func (o *GetOptions) WithFramerBuilder(fb codec.FramerBuilder)
- func (o *GetOptions) WithLocalAddr(addr string)
- func (o *GetOptions) WithProtocol(s string)
- type HealthChecker
- type Option
- func WithDialFunc(d DialFunc) Option
- func WithDialTimeout(t time.Duration) Option
- func WithForceClose(f bool) Option
- func WithHealthChecker(c HealthChecker) Option
- func WithIdleTimeout(t time.Duration) Option
- func WithMaxActive(s int) Option
- func WithMaxConnLifetime(t time.Duration) Option
- func WithMaxIdle(m int) Option
- func WithMinIdle(n int) Option
- func WithPoolIdleTimeout(t time.Duration) Option
- func WithPushIdleConnToTail(c bool) Option
- func WithWait(w bool) Option
- type Options
- type Pool
- type PoolConn
Constants ¶
This section is empty.
Variables ¶
var ( ErrPoolLimit = errors.New("connection pool limit") // ErrPoolLimit number of connections exceeds the limit error. ErrPoolClosed = errors.New("connection pool closed") // ErrPoolClosed connection pool closed error. ErrConnClosed = errors.New("conn closed") // ErrConnClosed connection closed. ErrNoDeadline = errors.New("dial no deadline") // ErrNoDeadline has no deadline set. ErrConnInPool = errors.New("conn already in pool") // ErrNoDeadline has no deadline set. )
connection pool error message.
var DefaultConnectionPool = NewConnectionPool()
DefaultConnectionPool is the default connection pool, replaceable.
Functions ¶
Types ¶
type ConnectionPool ¶
type ConnectionPool struct { Dial func(context.Context) (net.Conn, error) // initialize the connection. MinIdle int // Minimum number of idle connections. MaxIdle int // Maximum number of idle connections, 0 means no limit. MaxActive int // Maximum number of active connections, 0 means no limit. IdleTimeout time.Duration // idle connection timeout. // Whether to wait when the maximum number of active connections is reached. Wait bool MaxConnLifetime time.Duration // Maximum lifetime of the connection. PushIdleConnToTail bool // connection to ip will be push tail when ConnectionPool.put method is called. // contains filtered or unexported fields }
ConnectionPool is the connection pool.
func (*ConnectionPool) Close ¶
func (p *ConnectionPool) Close() error
Close releases the connection.
func (*ConnectionPool) Get ¶
func (p *ConnectionPool) Get(ctx context.Context) (*PoolConn, error)
Get gets the connection from the connection pool.
func (*ConnectionPool) RegisterChecker ¶
func (p *ConnectionPool) RegisterChecker(interval time.Duration, checker HealthChecker)
RegisterChecker registers the idle connection check method.
type DialFunc ¶
type DialFunc func(opts *DialOptions) (net.Conn, error)
DialFunc connects to an endpoint with the information in options.
type DialOptions ¶
type DialOptions struct { Network string Address string LocalAddr string Timeout time.Duration CACertFile string // ca certificate. TLSCertFile string // client certificate. TLSKeyFile string // client secret key. TLSServerName string // The client verifies the server's service name, // if not filled in, it defaults to the http hostname. IdleTimeout time.Duration }
DialOptions request parameters.
type GetOptions ¶
type GetOptions struct { FramerBuilder codec.FramerBuilder CustomReader func(io.Reader) io.Reader Ctx context.Context CACertFile string // ca certificate. TLSCertFile string // client certificate. TLSKeyFile string // client secret key. TLSServerName string // The client verifies the server's service name, LocalAddr string // The local address when establishing a connection, which is randomly selected by default. DialTimeout time.Duration // Connection establishment timeout. Protocol string // protocol type. }
GetOptions is the get conn configuration.
func NewGetOptions ¶
func NewGetOptions() GetOptions
NewGetOptions creates and initializes GetOptions.
func (*GetOptions) WithContext ¶
func (o *GetOptions) WithContext(ctx context.Context)
WithContext returns an Option which sets the requested ctx.
func (*GetOptions) WithCustomReader ¶
func (o *GetOptions) WithCustomReader(customReader func(io.Reader) io.Reader)
WithCustomReader returns an option which sets a customReader. Connection pool will uses this customReader to create a reader encapsulating the underlying connection, which is usually used to create a buffer.
func (*GetOptions) WithDialTLS ¶
func (o *GetOptions) WithDialTLS(certFile, keyFile, caFile, serverName string)
WithDialTLS returns an Option which sets the client to support TLS.
func (*GetOptions) WithDialTimeout ¶
func (o *GetOptions) WithDialTimeout(dur time.Duration)
WithDialTimeout returns an Option which sets the connection timeout.
func (*GetOptions) WithFramerBuilder ¶
func (o *GetOptions) WithFramerBuilder(fb codec.FramerBuilder)
WithFramerBuilder returns an Option which sets the FramerBuilder.
func (*GetOptions) WithLocalAddr ¶
func (o *GetOptions) WithLocalAddr(addr string)
WithLocalAddr returns an Option which sets the local address when establishing a connection, and it is randomly selected by default when there are multiple network cards.
func (*GetOptions) WithProtocol ¶
func (o *GetOptions) WithProtocol(s string)
WithProtocol returns an Option which sets the backend service protocol name.
type HealthChecker ¶
HealthChecker idle connection health check function. The function supports quick check and comprehensive check. Quick check is called when an idle connection is obtained, and only checks whether the connection status is abnormal. The function returns true to indicate that the connection is available normally.
type Option ¶
type Option func(*Options)
Option is the Options helper.
func WithDialFunc ¶
WithDialFunc returns an Option which sets dial function.
func WithDialTimeout ¶
WithDialTimeout returns an Option which sets the default timeout time for the connection pool to establish a connection.
func WithForceClose ¶
WithForceClose returns an Option which sets whether to force the connection to be closed.
func WithHealthChecker ¶
func WithHealthChecker(c HealthChecker) Option
WithHealthChecker returns an Option which sets health checker.
func WithIdleTimeout ¶
WithIdleTimeout returns an Option which sets the idle connection time, after which it may be closed.
func WithMaxActive ¶
WithMaxActive returns an Option which sets the maximum number of active connections.
func WithMaxConnLifetime ¶
WithMaxConnLifetime returns an Option which sets the maximum lifetime of the connection, after which it may be closed.
func WithMaxIdle ¶
WithMaxIdle returns an Option which sets the maximum number of idle connections.
func WithMinIdle ¶
WithMinIdle returns an Option which sets the number of initialized connections.
func WithPoolIdleTimeout ¶
WithPoolIdleTimeout returns an Option which sets pool idle timeout. after the timeout, ConnectionPool resource may be cleaned up.
func WithPushIdleConnToTail ¶
WithPushIdleConnToTail returns an Option which sets PushIdleConnToTail flag.
type Options ¶
type Options struct { MinIdle int // Initialize the number of connections, ready for the next io. MaxIdle int // Maximum number of idle connections, 0 means no idle. MaxActive int // Maximum number of active connections, 0 means no limit. // Whether to wait when the maximum number of active connections is reached. Wait bool IdleTimeout time.Duration // idle connection timeout. MaxConnLifetime time.Duration // Maximum lifetime of the connection. DialTimeout time.Duration // Connection establishment timeout. ForceClose bool Dial DialFunc Checker HealthChecker PushIdleConnToTail bool // connection to ip will be push tail when ConnectionPool.put method is called PoolIdleTimeout time.Duration // ConnectionPool idle timeout }
Options indicates pool configuration.
type Pool ¶
Pool is the interface that specifies client connection pool options. Compared with Pool, Pool directly uses the GetOptions data structure for function input parameters. Compared with function option input parameter mode, it can reduce memory escape and improve calling performance.
func NewConnectionPool ¶
NewConnectionPool creates a connection pool.
type PoolConn ¶
PoolConn is the connection in the connection pool.
func (*PoolConn) Close ¶
Close overrides the Close method of net.Conn and puts it back into the connection pool.
func (*PoolConn) GetRawConn ¶
GetRawConn gets raw connection in PoolConn.