pool

package
v0.0.0-...-13b24d6 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ConnectionConfig *alphasql.ConnectionConfig

	// BeforeConnect is called before a new Connection is made. It is passed a copy of the underlying
	// alphasql.ConnectionConfig and will not impact any existing open connections.
	BeforeConnect func(context.Context, *alphasql.ConnectionConfig) error

	// AfterConnect is called after a Connection is established, but before it is added to the pool.
	AfterConnect func(context.Context, *alphasql.Connection) error

	// BeforeAcquire is called before a Connection is acquired from the pool. It must return true to allow the
	// acquisition or false to indicate that the Connection should be destroyed and a different Connection should be
	// acquired.
	BeforeAcquire func(context.Context, *Connection) bool

	// AfterRelease is called after a Connection is released, but before it is returned to the pool. It must return true to
	// return the Connection to the pool or false to destroy the Connection.
	AfterRelease func(context.Context, *Connection) bool

	// BeforeClose is called right before a Connection is closed and removed from the pool.
	BeforeClose func(context.Context, *alphasql.Connection)

	// MaxConnectionLifetime is the duration since creation after which a Connection will be automatically closed.
	MaxConnectionLifetime time.Duration

	// MaxConnectionLifetimeJitter is the duration after MaxConnectionLifetime to randomly decide to close a Connection.
	// This helps prevent all connections from being closed at the exact same time, starving the pool.
	MaxConnectionLifetimeJitter time.Duration

	// MaxConnectionIdleTime is the duration after which an idle Connection will be automatically closed by the health check.
	MaxConnectionIdleTime time.Duration

	// MaxConnections is the maximum size of the pool. The default is the greatest of 4 or runtime.NumCPU().
	MaxConnections int32

	// MinConnections is the minimum size of the pool. After Connection closes, the pool might dip below MinConnections.
	// A low number of MinConnections might mean the pool is empty after MaxConnectionLifetime until the health check
	// has a chance to create new connections.
	MinConnections int32

	// HealthCheckPeriod is the duration between checks of the health of idle connections.
	HealthCheckPeriod time.Duration
}

Config is the configuration required for creating a pool.

func (*Config) ValidateAndDefault

func (c *Config) ValidateAndDefault() error

ValidateAndDefault is used to validate and set the defaults for the mandatory parameters not passed.

type Connection

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

Connection is used for managing the instance of a Connection in the pool.

func (*Connection) BeginTX

func (c *Connection) BeginTX(ctx context.Context, options *alphasql.TXOptions) (alphasql.TX, error)

BeginTX starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the `alphasql` package will roll back the transaction. [TX.Commit] will return an error if the context provided to BeginTX is canceled.

The provided [TXOptions] is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*Connection) Exec

func (c *Connection) Exec(ctx context.Context, query string, args ...any) (alphasql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Connection) Ping

func (c *Connection) Ping(ctx context.Context) error

Ping verifies a Connection to the database is still alive, establishing a Connection if necessary.

func (*Connection) Prepare

func (c *Connection) Prepare(ctx context.Context, query string) (alphasql.Statement, error)

Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's alphasql.Statement.Close method when the statement is no longer needed.

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, query string, args ...any) (alphasql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, query string, args ...any) alphasql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until alphasql.Row's Scan method is called. If the query selects no rows, the alphasql.Row.Scan will return alphasql.ErrNoRows. Otherwise, alphasql.Row.Scan scans the first selected row and discards the rest.

type Pool

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

Pool is used to manage the set of connections, also being able to reuse it.

func New

func New(ctx context.Context, cfg *Config) (*Pool, error)

New is used to create a new pool

func (*Pool) Acquire

func (p *Pool) Acquire(ctx context.Context) (*Connection, error)

Acquire is used to get a (*Connection) from the pool.

func (*Pool) BeginTX

func (p *Pool) BeginTX(ctx context.Context, options *alphasql.TXOptions) (alphasql.TX, error)

BeginTX starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the `alphasql` package will roll back the transaction. alphasql.TX.Commit will return an error if the context provided to BeginTX is canceled.

The provided alphasql.TXOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*Pool) Close

func (p *Pool) Close(ctx context.Context)

Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned.

func (*Pool) Exec

func (p *Pool) Exec(ctx context.Context, query string, args ...any) (alphasql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Pool) Ping

func (p *Pool) Ping(ctx context.Context) error

Ping verifies a Connection to the database is still alive, establishing a Connection if necessary.

func (*Pool) Prepare

func (p *Pool) Prepare(ctx context.Context, query string) (alphasql.Statement, error)

Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's alphasql.Statement.Close method when the statement is no longer needed.

func (*Pool) Query

func (p *Pool) Query(ctx context.Context, query string, args ...any) (alphasql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Pool) QueryRow

func (p *Pool) QueryRow(ctx context.Context, query string, args ...any) alphasql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until alphasql.Row's Scan method is called. If the query selects no rows, the alphasql.Row.Scan will return alphasql.ErrNoRows. Otherwise, alphasql.Row.Scan scans the first selected row and discards the rest.

func (*Pool) Release

func (p *Pool) Release(ctx context.Context, c *Connection)

Release is used to return a (*Connection) to the pool.

Jump to

Keyboard shortcuts

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