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
Note: since pgxpool is used, the lock will keep a connection from the connection pool for himself; this connection is freed when the number of locked unlocks is achieved
Index ¶
- Constants
- func DialectOptions() *goqu.SQLDialectOptions
- func ForeignTableExists(ctx context.Context, db *sqlx.DB, tableName string, schema string) (bool, error)
- func GetServerVersion(db *sqlx.DB, ctx context.Context) (string, error)
- func NewClient(config *ClientConfig) (*db.SqlClient, error)
- func NewMigrationManager(ctx context.Context, client *db.SqlClient) (migrations.Manager, error)
- func TableExists(ctx context.Context, db *sqlx.DB, tableName string, schema string) (bool, error)
- func ViewExists(ctx context.Context, db *sqlx.DB, tableName string, schema string) (bool, error)
- type AdvisoryLock
- type ClientConfig
- type LockConn
Constants ¶
const ( DefaultIdleConns = 2 DefaultMaxConns = 4 DefaultConnLifeTimeSecond = 3600 DefaultConnIdleTimeSecond = 1800 ErrEmptyDSN = utils.Error("Empty DSN") ErrNilConfig = utils.Error("Config is nil") ErrInvalidIdleConns = utils.Error("Invalid idleConns") ErrInvalidMaxConns = utils.Error("Invalid maxConns") ErrInvalidConnLifeTime = utils.Error("connLifeTime must be >= 1") ErrInvalidConnIdleTime = utils.Error("connIdleTime must be >= 1") )
const ( EngineSchema = "public" MigrationTable = "db_migration" EngineMigrationTable = EngineSchema + "." + MigrationTable MigrationLockId = 2343452349 )
const ( SchemaDefault = "public" TblTypeTable = "BASE TABLE" TblTypeView = "VIEW" TblTypeForeignTable = "FOREIGN TABLE" TblTypeLocal = "LOCAL TEMPORARY" )
Variables ¶
This section is empty.
Functions ¶
func DialectOptions ¶ added in v0.2.0
func DialectOptions() *goqu.SQLDialectOptions
func ForeignTableExists ¶ added in v0.1.3
func ForeignTableExists(ctx context.Context, db *sqlx.DB, tableName string, schema string) (bool, error)
ForeignTableExists returns true if specified foreign table exists
func GetServerVersion ¶ added in v0.1.3
GetServerVersion fetch postgresql version
func NewMigrationManager ¶ added in v0.1.3
func TableExists ¶ added in v0.1.3
TableExists returns true if specified table exists
Types ¶
type AdvisoryLock ¶ added in v0.1.1
type AdvisoryLock struct {
// contains filtered or unexported fields
}
func NewAdvisoryLock ¶ added in v0.1.1
func (*AdvisoryLock) Close ¶ added in v0.2.0
func (l *AdvisoryLock) Close()
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"` MaxOpenConns int `json:"maxOpenConns"` // MaxOpenConns max number of pool connections MaxIdleConns int `json:"maxIdleConns"` // MaxIdleConns max number of idle 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"` }
func NewClientConfig ¶ added in v0.2.0
func NewClientConfig() *ClientConfig
func (ClientConfig) Validate ¶
func (c ClientConfig) Validate() error