Documentation
¶
Index ¶
- Constants
- Variables
- func CompareStringSlicesIgnoreOrder(a, b []string) bool
- func CurTime() time.Time
- func GetAdvisoryLockIDFromString(id string) uint64
- func GetBoolPtr(b bool) *bool
- func GetCurTime() time.Time
- func GetIDB(tx *Tx, dbSession *Session) bun.IDB
- func GetIntPtr(i int) *int
- func GetStrPtr(s string) *string
- func GetStringToTsQuery(inputQuery string) string
- func GetStringToUint64Hash(id string) uint64
- func GetTimePtr(t time.Time) *time.Time
- func GetUUIDPtr(u uuid.UUID) *uuid.UUID
- func IsStrInSlice(s string, sl []string) bool
- func RollbackTx(ctx context.Context, tx *Tx, committed *bool)
- type Config
- type ErrorChecker
- type LockRetryOptions
- type PostgresErrorChecker
- type Session
- type Tx
Constants ¶
const ( // DefaultPageSize is the size for query results to request from DB DefaultPageSize = 20 // MaxBatchItems limits the maximum number of items allowed in a single batch operation // to prevent performance degradation and potential timeouts from overly large batches. MaxBatchItems = 100 // MaxBatchItemsToTrace limits the number of items traced in detail for batch operations // to avoid producing overly-large spans and reduce the risk of hitting tracing backend limits. // Items beyond this limit will still be processed but won't have their individual field values traced. MaxBatchItemsToTrace = 20 )
const (
DefaultTxLockTimeoutSeconds = 300 // in seconds
)
Variables ¶
var ( // ErrDoesNotExist is raised a DB query fails to find the requested entity ErrDoesNotExist = errors.New("the requested entity does not exist") // ErrDBError is a generalized error to expose to the user when unexpected errors occur when communicating with DB ErrDBError = errors.New("error communicating with data store") // ErrInvalidValue is raised when a value to be stored in DB is invalid ErrInvalidValue = errors.New("provided value is invalid") // ErrInvalidParams is raised when a function is called with invalid set of parameters ErrInvalidParams = errors.New("provided params are invalid or conflicting") // ErrXactAdvisoryLockFailed indicates that the transaction advisory lock could not be taken ErrXactAdvisoryLockFailed = errors.New("unable to take transaction advisory lock") // ErrSessionAdvisoryLockFailed indicates that the session advisory lock could not be taken ErrSessionAdvisoryLockFailed = errors.New("unable to take session advisory lock") // ErrSessionAdvisoryLockUnlockFailed indicates that the session advisory lock could not be released. ErrSessionAdvisoryLockUnlockFailed = errors.New("unable to release session advisory lock or lock was not held by this session") // ErrInvalidPort indicates the DB_PORT environment variable is not a valid integer. ErrInvalidPort = errors.New("failed to parse DB_PORT") // ErrInvalidCredential indicates the credential is not valid. ErrInvalidCredential = errors.New("invalid credential") )
Functions ¶
func CompareStringSlicesIgnoreOrder ¶
CompareStringSlicesIgnoreOrder compares two string slices ignoring order
func CurTime ¶
CurTime returns the current UTC time rounded to microseconds (useful for DB timestamps).
func GetAdvisoryLockIDFromString ¶
GetAdvisoryLockIDFromString returns the advisory lock ID from a string pg expects lockid to not have the msb set
func GetIDB ¶
GetIDB is used by DAO methods to get the DB interface If DAO method's tx parameter is non-nil, return it else return the dbSession note: both bun.Tx and bun.DB implement the bun.IDB
func GetStringToTsQuery ¶
GetStringToTsQuery returns a string into a to_tsquery format from the input string
func GetStringToUint64Hash ¶
GetStringToUint64Hash returns a uint64 hash of the input string this is used for advisory lock ids
func GetTimePtr ¶
GetTimePtr returns a pointer for the provided time
func GetUUIDPtr ¶
GetUUIDPtr returns a pointer for the provided UUID
func IsStrInSlice ¶
IsStrInSlice returns true if the provided string is in the provided slice
Types ¶
type Config ¶
type Config struct {
Host string
Port int
DBName string
Credential credential.Credential
CACertificatePath string
}
Config represents the configuration needed to connect to a database.
func ConfigFromEnv ¶
ConfigFromEnv builds a Config from environment variables. Reads: DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_NAME, DB_CERT_PATH (optional CA certificate).
type ErrorChecker ¶
ErrorChecker abstracts database error classification.
type LockRetryOptions ¶
type PostgresErrorChecker ¶
type PostgresErrorChecker struct{}
PostgresErrorChecker classifies common Postgres errors such as no rows and unique constraint violations.
func (*PostgresErrorChecker) IsErrNoRows ¶
func (checker *PostgresErrorChecker) IsErrNoRows(err error) bool
func (*PostgresErrorChecker) IsUniqueConstraintError ¶
func (checker *PostgresErrorChecker) IsUniqueConstraintError(err error) bool
type Session ¶
Session is a wrapper for an ORM DB object
func NewSession ¶
func NewSession(ctx context.Context, host string, port int, dbName string, user string, password string, caCertPath string) (*Session, error)
NewSession creates and returns a new session object using pgx v5 + pgxpool. It delegates to NewSessionFromConfig to keep DSN logic centralized.
func NewSessionFromConfig ¶
NewSessionFromConfig creates a Session from a Config.
func (*Session) Close ¶
func (s *Session) Close()
Close closes the session and the underlying connection pool.
func (*Session) GetErrorChecker ¶
func (s *Session) GetErrorChecker() ErrorChecker
GetErrorChecker returns the error classifier for this session.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a thin wrapper around the bun.Tx object
func (*Tx) AcquireAdvisoryLock ¶
AcquireAdvisoryLock will "try" to take the specified advisory lock on the transaction Error case: ----------- if the lock is already held by another transaction, this will error, and the caller needs to (possibly) retry in the same transaction (after a delay) this is the api-handler usecase or retry in a new transaction after rolling back the current transaction this is the workflow worker usecase Success case: ------------- the transaction lock when acquired is automatically released when the transaction commits or rollsback (or the transaction connection dies which is equivalent to a rollback for the transaction)
func (*Tx) TryAcquireAdvisoryLock ¶
func (tx *Tx) TryAcquireAdvisoryLock(ctx context.Context, lockID uint64, options *LockRetryOptions) error
TryAcquireAdvisoryLock acquires an advisory lock retrying (up to retryCnt times which defaults to 3) when the lock acquisition attempt fails note, that each lock acquisition attempt is a non-blocking pg_try_advisory_xact_lock retries will backoff exponentially starting with initial delay of 300ms (300ms, 600ms, 1200ms etc..) with a max-jitter of 100ms.