smartconnpool

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeout is returned if a connection get times out.
	ErrTimeout = vterrors.New(vtrpcpb.Code_RESOURCE_EXHAUSTED, "connection pool timed out")

	// ErrCtxTimeout is returned if a ctx is already expired by the time the connection pool is used
	ErrCtxTimeout = vterrors.New(vtrpcpb.Code_DEADLINE_EXCEEDED, "connection pool context already expired")

	// ErrConnPoolClosed is returned when trying to get a connection from a closed conn pool
	ErrConnPoolClosed = vterrors.New(vtrpcpb.Code_INTERNAL, "connection pool is closed")

	// PoolCloseTimeout is how long to wait for all connections to be returned to the pool during close
	PoolCloseTimeout = 10 * time.Second
)

Functions

This section is empty.

Types

type Config

type Config[C Connection] struct {
	Capacity        int64
	IdleTimeout     time.Duration
	MaxLifetime     time.Duration
	RefreshInterval time.Duration
	LogWait         func(time.Time)
}

type ConnPool

type ConnPool[C Connection] struct {
	Metrics Metrics
	Name    string
	// contains filtered or unexported fields
}

ConnPool is a connection pool for generic connections

func NewPool

func NewPool[C Connection](config *Config[C]) *ConnPool[C]

NewPool creates a new connection pool with the given Config. The pool must be ConnPool.Open before it can start giving out connections

func (*ConnPool[C]) Active

func (pool *ConnPool[C]) Active() int64

Active returns the numer of connections that the pool has currently open.

func (*ConnPool[C]) Available

func (pool *ConnPool[C]) Available() int64

Available returns the number of connections that the pool can immediately lend out to clients without blocking.

func (*ConnPool[C]) Capacity

func (pool *ConnPool[C]) Capacity() int64

Capacity returns the maximum amount of connections that this pool can maintain open

func (*ConnPool[C]) Close

func (pool *ConnPool[C]) Close()

Close shuts down the pool. No connections will be returned from ConnPool.Get after calling this, but calling ConnPool.Put is still allowed. This function will not return until all of the pool's connections have been returned or the default PoolCloseTimeout has elapsed

func (*ConnPool[C]) CloseWithContext added in v0.19.5

func (pool *ConnPool[C]) CloseWithContext(ctx context.Context) error

CloseWithContext behaves like Close but allows passing in a Context to time out the pool closing operation

func (*ConnPool[C]) Get

func (pool *ConnPool[C]) Get(ctx context.Context, setting *Setting) (*Pooled[C], error)

Get returns a connection from the pool with the given Setting applied. If there are no connections in the pool to be returned, Get blocks until one is returned, or until the given ctx is cancelled. The connection must be returned to the pool once it's not needed by calling Pooled.Recycle

func (*ConnPool[D]) IdleTimeout

func (pool *ConnPool[D]) IdleTimeout() time.Duration

func (*ConnPool[C]) InUse

func (pool *ConnPool[C]) InUse() int64

InUse returns the number of connections that the pool has lent out to clients and that haven't been returned yet.

func (*ConnPool[C]) IsOpen

func (pool *ConnPool[C]) IsOpen() bool

IsOpen returns whether the pool is open

func (*ConnPool[C]) MaxCapacity

func (pool *ConnPool[C]) MaxCapacity() int64

MaxCapacity returns the maximum value to which Capacity can be set via ConnPool.SetCapacity

func (*ConnPool[C]) Open

func (pool *ConnPool[C]) Open(connect Connector[C], refresh RefreshCheck) *ConnPool[C]

Open starts the background workers that manage the pool and gets it ready to start serving out connections.

func (*ConnPool[D]) RefreshInterval

func (pool *ConnPool[D]) RefreshInterval() time.Duration

func (*ConnPool[C]) RegisterStats

func (pool *ConnPool[C]) RegisterStats(stats *servenv.Exporter, name string)

RegisterStats registers this pool's metrics into a stats Exporter

func (*ConnPool[C]) SetCapacity

func (pool *ConnPool[C]) SetCapacity(ctx context.Context, newcap int64) error

SetCapacity changes the capacity (number of open connections) on the pool. If the capacity is smaller than the number of connections that there are currently open, we'll close enough connections before returning, even if that means waiting for clients to return connections to the pool. If the given context times out before we've managed to close enough connections an error will be returned.

func (*ConnPool[C]) SetIdleTimeout

func (pool *ConnPool[C]) SetIdleTimeout(duration time.Duration)

func (*ConnPool[C]) StatsJSON

func (pool *ConnPool[C]) StatsJSON() map[string]any

type Connection

type Connection interface {
	ApplySetting(ctx context.Context, setting *Setting) error
	ResetSetting(ctx context.Context) error
	Setting() *Setting

	IsClosed() bool
	Close()
}

type Connector

type Connector[C Connection] func(ctx context.Context) (C, error)

type Metrics

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

func (*Metrics) DiffSettingCount

func (m *Metrics) DiffSettingCount() int64

func (*Metrics) GetCount

func (m *Metrics) GetCount() int64

func (*Metrics) GetSettingCount

func (m *Metrics) GetSettingCount() int64

func (*Metrics) IdleClosed

func (m *Metrics) IdleClosed() int64

func (*Metrics) MaxLifetimeClosed

func (m *Metrics) MaxLifetimeClosed() int64

func (*Metrics) ResetSettingCount

func (m *Metrics) ResetSettingCount() int64

func (*Metrics) WaitCount

func (m *Metrics) WaitCount() int64

func (*Metrics) WaitTime

func (m *Metrics) WaitTime() time.Duration

type Pooled

type Pooled[C Connection] struct {
	Conn C
	// contains filtered or unexported fields
}

func (*Pooled[C]) Close

func (dbc *Pooled[C]) Close()

func (*Pooled[C]) Recycle

func (dbc *Pooled[C]) Recycle()

func (*Pooled[C]) Taint

func (dbc *Pooled[C]) Taint()

type RefreshCheck

type RefreshCheck func() (bool, error)

type Setting

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

Setting is a setting applied to a connection in this pool. Setting values must be interned for optimal usage (i.e. a Setting that represents a specific set of SQL connection settings should always have the same pointer value).

func NewSetting

func NewSetting(apply, reset string) *Setting

func (*Setting) ApplyQuery

func (s *Setting) ApplyQuery() string

func (*Setting) CachedSize

func (cached *Setting) CachedSize(alloc bool) int64

func (*Setting) ResetQuery

func (s *Setting) ResetQuery() string

Directories

Path Synopsis
benchmarking

Jump to

Keyboard shortcuts

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