chpool

package
v3.0.0-beta7 Latest Latest
Warning

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

Go to latest
Published: May 21, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ConnConfig *chconn.Config

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

	// AfterConnect is called after a connection is established, but before it is added to the pool.
	AfterConnect func(context.Context, chconn.Conn) error

	// BeforeAcquire is called before a connection is acquired from the pool. It must return true to allow the
	// acquision or false to indicate that the connection should be destroyed and a different connection should be
	// acquired.
	BeforeAcquire func(context.Context, chconn.Conn) 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(chconn.Conn) bool

	// BeforeClose is called right before a connection is closed and removed from the pool.
	BeforeClose func(chconn.Conn)

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

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

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

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

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

	// HealthCheckPeriod is the duration between checks of the health of idle connections.
	HealthCheckPeriod time.Duration
	// contains filtered or unexported fields
}

Config is the configuration struct for creating a pool. It must be created by ParseConfig and then it can be modified.

func ParseConfig

func ParseConfig(connString string) (*Config, error)

ParseConfig builds a Config from connString. It parses connString with the same behavior as chconn.ParseConfig with the addition of the following variables:

  • pool_max_conns: integer greater than 0
  • pool_min_conns: integer 0 or greater
  • pool_max_conn_lifetime: duration string
  • pool_max_conn_idle_time: duration string
  • pool_health_check_period: duration string
  • pool_max_conn_lifetime_jitter: duration string

See Config for definitions of these arguments.

# Example DSN
user=vahid password=secret host=clickhouse.example.com port=9000 dbname=mydb sslmode=verify-ca pool_max_conns=10

# Example URL
clickhouse://vahid:secret@ch.example.com:9000/mydb?sslmode=verify-ca&pool_max_conns=10

func (*Config) ConnString

func (c *Config) ConnString() string

ConnString returns the connection string as parsed by pgxpool.ParseConfig into pgxpool.Config.

func (*Config) Copy

func (c *Config) Copy() *Config

Copy returns a deep copy of the config that is safe to use and modify. The only exception is the tls.Config: according to the tls.Config docs it must not be modified after creation.

type Conn

type Conn interface {
	Release()
	// ExecWithOption executes a query without returning any rows with Query options.
	// NOTE: don't use it for insert and select query
	ExecWithOption(
		ctx context.Context,
		query string,
		queryOptions *chconn.QueryOptions,
	) error
	Query(ctx context.Context, sql string, args ...chconn.Parameter) (chconn.Rows, error)
	QueryWithOption(ctx context.Context, sql string, queryOptions *chconn.QueryOptions, args ...chconn.Parameter) (chconn.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...chconn.Parameter) chconn.Row
	QueryRowWithOption(ctx context.Context, sql string, queryOptions *chconn.QueryOptions, args ...chconn.Parameter) chconn.Row

	// Select executes a query with the the query options and return select stmt.
	// NOTE: only use for select query
	SelectWithOption(
		ctx context.Context,
		query string,
		queryOptions *chconn.QueryOptions,
		columns ...column.ColumnBasic,
	) (chconn.SelectStmt, error)
	// InsertWithSetting executes a query with the query options and commit all columns data.
	// NOTE: only use for insert query
	InsertWithOption(ctx context.Context, query string, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error
	// InsertWithSetting executes a query with the query options and commit all columns data.
	// NOTE: only use for insert query
	InsertStreamWithOption(ctx context.Context, query string, queryOptions *chconn.QueryOptions) (chconn.InsertStmt, error)
	// Conn get the underlying chconn.Conn
	Conn() chconn.Conn
	// Hijack assumes ownership of the connection from the pool. Caller is responsible for closing the connection. Hijack
	// will panic if called on an already released or hijacked connection.
	Hijack() chconn.Conn
	Ping(ctx context.Context) error
	// contains filtered or unexported methods
}

Conn is an acquired *chconn.Conn from a Pool.

type Pool

type Pool interface {
	// Close closes all connections in the pool and rejects future Acquire calls. Blocks until all connections are returned
	// to pool and closed.
	Close()
	// Acquire returns a connection (Conn) from the Pool
	Acquire(ctx context.Context) (Conn, error)
	// AcquireFunc acquires a Conn and calls f with that Conn. ctx will only affect the Acquire. It has no effect on the
	// call of f. The return value is either an error acquiring the Conn or the return value of f. The Conn is
	// automatically released after the call of f.
	AcquireFunc(ctx context.Context, f func(Conn) error) error
	// AcquireAllIdle atomically acquires all currently idle connections. Its intended use is for health check and
	// keep-alive functionality. It does not update pool statistics.
	AcquireAllIdle(ctx context.Context) []Conn
	// Exec executes a query without returning any rows.
	// NOTE: don't use it for insert and select query
	Exec(ctx context.Context, query string) error
	// ExecWithOption executes a query without returning any rows with Query options.
	// NOTE: don't use it for insert and select query
	ExecWithOption(
		ctx context.Context,
		query string,
		queryOptions *chconn.QueryOptions,
	) error
	// Insert executes a insert query and commit all columns data.
	//
	// If the query is successful, the columns buffer will be reset.
	//
	// NOTE: only use for insert query
	Insert(ctx context.Context, query string, columns ...column.ColumnBasic) error
	// InsertWithOption executes a insert query with a query options and commit all columns data.
	//
	// If the query is successful, the columns buffer will be reset.
	//
	// NOTE: only use for insert query
	InsertWithOption(ctx context.Context, query string, queryOptions *chconn.QueryOptions, columns ...column.ColumnBasic) error
	// Insert executes a insert query and return a InsertStmt.
	//
	// NOTE: only use for insert query
	InsertStream(ctx context.Context, query string) (chconn.InsertStmt, error)
	// InsertWithOption executes a insert query with a query options and return a InsertStmt.
	//
	// If the query is successful, the columns buffer will be reset.
	//
	// NOTE: only use for insert query
	InsertStreamWithOption(
		ctx context.Context,
		query string,
		queryOptions *chconn.QueryOptions) (chconn.InsertStmt, error)
	// Select executes a query and return select stmt.
	//
	// NOTE: only use for select query
	Select(ctx context.Context, query string, columns ...column.ColumnBasic) (chconn.SelectStmt, error)
	// Select executes a query with a query options and return select stmt.
	//
	// NOTE: only use for select query
	SelectWithOption(
		ctx context.Context,
		query string,
		queryOptions *chconn.QueryOptions,
		columns ...column.ColumnBasic,
	) (chconn.SelectStmt, error)
	// Query acquires a connection and executes a (select) query that returns chconn.Rows.
	// See chconn.Rows documentation to close the returned Rows and return the acquired connection to the Pool.
	//
	// If there is an error, the returned chconn.Rows will be returned in an error state.
	// If preferred, ignore the error returned from Query and handle errors using the returned chconn.Rows.
	//
	Query(ctx context.Context, sql string, args ...chconn.Parameter) (chconn.Rows, error)
	// QueryRow acquires a connection and executes a query that is expected
	// to return at most one row (chconn.Row). Errors are deferred until chconn.Row's
	// Scan method is called. If the query selects no rows, chconn.Row's Scan will
	// return ErrNoRows. Otherwise, chconn.Row's Scan scans the first selected row
	// and discards the rest. The acquired connection is returned to the Pool when
	// chconn.Row's Scan method is called.
	QueryRow(ctx context.Context, sql string, args ...chconn.Parameter) chconn.Row
	// Query acquires a connection and executes a (select) query with a query options that returns chconn.Rows.
	// See chconn.Rows documentation to close the returned Rows and return the acquired connection to the Pool.
	//
	// If there is an error, the returned chconn.Rows will be returned in an error state.
	// If preferred, ignore the error returned from Query and handle errors using the returned chconn.Rows.
	//
	QueryWithOption(ctx context.Context, sql string, queryOptions *chconn.QueryOptions, args ...chconn.Parameter) (chconn.Rows, error)
	// QueryRow acquires a connection and executes a query with a query options that is expected
	// to return at most one row (chconn.Row). Errors are deferred until chconn.Row's
	// Scan method is called. If the query selects no rows, chconn.Row's Scan will
	// return ErrNoRows. Otherwise, chconn.Row's Scan scans the first selected row
	// and discards the rest. The acquired connection is returned to the Pool when
	// chconn.Row's Scan method is called.
	QueryRowWithOption(ctx context.Context, sql string, queryOptions *chconn.QueryOptions, args ...chconn.Parameter) chconn.Row
	// Ping acquires a connection from the Pool and send ping
	// If returns without error, the database Ping is considered successful, otherwise, the error is returned.
	Ping(ctx context.Context) error
	// Stat returns a chpool.Stat struct with a snapshot of Pool statistics.
	Stat() *Stat
	// Reset closes all connections, but leaves the pool open. It is intended for use when an error is detected that would
	// disrupt all connections (such as a network interruption or a server state change).
	//
	// It is safe to reset a pool while connections are checked out. Those connections will be closed when they are returned
	// to the pool.
	Reset()
	// Config returns a copy of config that was used to initialize this pool.
	Config() *Config
}

Pool is a connection pool for chconn

func New

func New(connString string) (Pool, error)

New creates a new Pool. See ParseConfig for information on connString format.

func NewWithConfig

func NewWithConfig(config *Config) (Pool, error)

NewWithConfig creates a new Pool. config must have been created by ParseConfig.

type Stat

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

Stat is a snapshot of Pool statistics.

func (*Stat) AcquireCount

func (s *Stat) AcquireCount() int64

AcquireCount returns the cumulative count of successful acquires from the pool.

func (*Stat) AcquireDuration

func (s *Stat) AcquireDuration() time.Duration

AcquireDuration returns the total duration of all successful acquires from the pool.

func (*Stat) AcquiredConns

func (s *Stat) AcquiredConns() int32

AcquiredConns returns the number of currently acquired connections in the pool.

func (*Stat) CanceledAcquireCount

func (s *Stat) CanceledAcquireCount() int64

CanceledAcquireCount returns the cumulative count of acquires from the pool that were canceled by a context.

func (*Stat) ConstructingConns

func (s *Stat) ConstructingConns() int32

ConstructingConns returns the number of conns with construction in progress in the pool.

func (*Stat) EmptyAcquireCount

func (s *Stat) EmptyAcquireCount() int64

EmptyAcquireCount returns the cumulative count of successful acquires from the pool that waited for a resource to be released or constructed because the pool was empty.

func (*Stat) IdleConns

func (s *Stat) IdleConns() int32

IdleConns returns the number of currently idle conns in the pool.

func (*Stat) MaxConns

func (s *Stat) MaxConns() int32

MaxConns returns the maximum size of the pool.

func (*Stat) MaxIdleDestroyCount

func (s *Stat) MaxIdleDestroyCount() int64

MaxIdleDestroyCount returns the cumulative count of connections destroyed because they exceeded MaxConnIdleTime.

func (*Stat) MaxLifetimeDestroyCount

func (s *Stat) MaxLifetimeDestroyCount() int64

MaxLifetimeDestroyCount returns the cumulative count of connections destroyed because they exceeded MaxConnLifetime.

func (*Stat) NewConnsCount

func (s *Stat) NewConnsCount() int64

NewConnsCount returns the cumulative count of new connections opened.

func (*Stat) TotalConns

func (s *Stat) TotalConns() int32

TotalConns returns the total number of resources currently in the pool. The value is the sum of ConstructingConns, AcquiredConns, and IdleConns.

Jump to

Keyboard shortcuts

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