db

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockBusy = errors.New("lock busy")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

func New

func New(cfg Config) (*Client, error)

func (Client) TryLock added in v0.41.0

func (c Client) TryLock(ctx context.Context, id string) (*Lock, error)

TryLock uses postgres advisory locks to acquire a lock on a given id if acquired, it returns the Lock object, else fail with ErrLockBusy In worst case if not unlocked, it will be released after the session ends which is configured via SetConnMaxLifetime

func (Client) WithTimeout

func (c Client) WithTimeout(ctx context.Context, collection, operation string, op func(ctx context.Context) error) (err error)

func (Client) WithTxn

func (c Client) WithTxn(ctx context.Context, txnOptions sql.TxOptions, txFunc func(*sqlx.Tx) error) (err error)

WithTxn Handling transactions: https://stackoverflow.com/a/23502629/8244298

type Config

type Config struct {
	Driver          string        `yaml:"driver" mapstructure:"driver" default:"postgres"`
	URL             string        `yaml:"url" mapstructure:"url"`
	MaxIdleConns    int           `yaml:"max_idle_conns" mapstructure:"max_idle_conns" default:"10"`
	MaxOpenConns    int           `yaml:"max_open_conns" mapstructure:"max_open_conns" default:"10"`
	ConnMaxLifeTime time.Duration `yaml:"conn_max_life_time" mapstructure:"conn_max_life_time" default:"15m"`
	MaxQueryTimeout time.Duration `yaml:"max_query_timeout" mapstructure:"max_query_timeout" default:"5s"`
}

type Lock added in v0.41.0

type Lock struct {
	ID int64
	// contains filtered or unexported fields
}

func (Lock) Unlock added in v0.41.0

func (l Lock) Unlock(ctx context.Context) error

Unlock uses postgres advisory locks to release a lock on a given id

Jump to

Keyboard shortcuts

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