internal

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDeadlock

func IsDeadlock(err error) bool

IsDeadlock returns true if the given error indicates that we found a deadlock.

func IsDup

func IsDup(err error) bool

IsDup returns true if the given error indicates that we found a duplicate record.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the given error indicates that a record could not be found.

func Run

func Run(ctx context.Context, db *sql.DB, fn func(context.Context) error) (err error)

Run runs fn with the given database connection. Run recovers from panics, e.g. in fn.

func RunInTx

func RunInTx(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) (err error)

RunInTx runs fn in a database transaction. The context ctx is passed to fn, as well as the newly created transaction. If fn fails, it is repeated several times before giving up, with exponential backoff.

There are a few rules that fn must respect:

  1. fn must use the passed tx reference for all database calls.
  2. fn must not commit or rollback the transaction: Run will do that.
  3. fn must be idempotent, i.e. it may be called several times without side effects.

If fn returns nil, RunInTx commits the transaction, returning the Commit and a nil error if it succeeds.

If fn returns a non-nil value, RunInTx rolls back the transaction and will return the reported error from fn.

RunInTx also recovers from panics, e.g. in fn.

func RunInTxWithRetry

func RunInTxWithRetry(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error, retryable func(error) bool) (err error)

RunInTxWithRetry is like RunInTx but will retry several times with exponential backoff. In that case, fn must also be idempotent, i.e. it may be called several times without side effects.

func RunInTxWithRetryBackoff

func RunInTxWithRetryBackoff(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error, retryable func(error) bool, b backoff.BackOff) (err error)

RunInTxWithRetryBackoff is like RunInTxWithRetry but with configurable backoff.

func RunWithRetry

func RunWithRetry(ctx context.Context, db *sql.DB, fn func(context.Context) error, retryable func(error) bool) (err error)

func RunWithRetryBackoff

func RunWithRetryBackoff(ctx context.Context, db *sql.DB, fn func(context.Context) error, retryable func(error) bool, b backoff.BackOff) (err error)

RunWithRetryBackoff is like RunWithRetry but with configurable backoff.

Types

This section is empty.

Jump to

Keyboard shortcuts

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