Documentation
¶
Overview ¶
AdvisoryLock implements primitives to use PostgreSQL's advisory locks.
These locks can be used to pipeline concurrent access between multiple database sessions. Keep in mind, both [Lock] and [TryLock] are stackable, can be called multiple times on the same session, requiring the same amount of calls to [Unlock] to free up the lock:
var l := NewAdvisoryLock(conn, 32) l.Lock(context.Background()) // lock does not exist previously in this session, so locks successfully l.Lock(context.Background()) // lock already exists, but this increments the lock l.Unlock() // lock is not freed yet, as it was locked twice l.Unlock() // only here lock is released See https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS for more details on the specifics
of PostgreSQL advisory locks
Index ¶
Constants ¶
View Source
const ( DefaultMinConns = 1 DefaultMaxConns = 4 DefaultConnLifeTimeSecond = 3600 DefaultConnIdleTimeSecond = 1800 DefaultHealthCheckSecond = 60 DefaultConnTimeoutSecond = 5 ErrEmptyDSN = utils.Error("Empty DSN") ErrNilConfig = utils.Error("Config is nil") ErrInvalidMinConns = utils.Error("Invalid minConns") ErrInvalidMaxConns = utils.Error("Invalid maxConns") ErrInvalidMinMaxConns = utils.Error("minConns must be <= maxConns") ErrInvalidConnLifeTime = utils.Error("connLifeTime must be >= 1") ErrInvalidConnIdleTime = utils.Error("connIdleTime must be >= 1") ErrInvalidHealthCheckInterval = utils.Error("healthCheckInterval must be >= 1") ErrInvalidConnTimeout = utils.Error("connTimeout must be >= 1") )
Variables ¶
This section is empty.
Functions ¶
func NewClientX ¶ added in v0.1.1
Types ¶
type AdvisoryLock ¶ added in v0.1.1
type AdvisoryLock struct {
// contains filtered or unexported fields
}
func NewAdvisoryLock ¶ added in v0.1.1
func NewAdvisoryLock(conn LockConn, id int) *AdvisoryLock
func (*AdvisoryLock) Lock ¶ added in v0.1.1
func (l *AdvisoryLock) Lock(ctx context.Context) error
Lock attempts to perform a lock, and waits until it is available
type ClientConfig ¶
type ClientConfig struct {
DSN string `json:"dsn"`
}
func (ClientConfig) Validate ¶
func (c ClientConfig) Validate() error
type PoolConfig ¶ added in v0.1.1
type PoolConfig struct {
DSN string `json:"dsn"` // DSN database connection string
MinConns int32 `json:"minConns"` // MinConns minimum number of pool connections
MaxConns int32 `json:"maxConns"` // MaxConns max number of pool connections
// ConnLifeTime is the duration in seconds since creation after which a connection will be automatically closed
ConnLifeTime int `json:"connLifeTime"`
// ConnIdleTime is the duration in seconds after which an idle connection will be automatically closed by the health check
ConnIdleTime int `json:"connIdleTime"`
// HealthCheckInterval is the duration in seconds between checks of the health of idle connections
HealthCheckInterval int `json:"healthCheckInterval"`
// ConnTimeout is the max duration in seconds of a database operation until timeout is reached
ConnTimeout int `json:"connTimeout"`
// optional method override
BeforeConnect func(context.Context, *pgx.ConnConfig) error
AfterConnect func(context.Context, *pgx.Conn) error
BeforeAcquire func(context.Context, *pgx.Conn) bool
AfterRelease func(*pgx.Conn) bool
BeforeClose func(*pgx.Conn)
}
func NewPoolConfig ¶ added in v0.1.1
func NewPoolConfig() *PoolConfig
func (PoolConfig) Validate ¶ added in v0.1.1
func (c PoolConfig) Validate() error
Click to show internal directories.
Click to hide internal directories.