txn

package
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddPostCommitHook

func AddPostCommitHook(ctx context.Context, hook MustFunc)

func AddPostCompleteHook

func AddPostCompleteHook(ctx context.Context, hook MustFunc)

func AddPostRollbackHook

func AddPostRollbackHook(ctx context.Context, hook MustFunc)

func AddPreCommitHook added in v0.20.0

func AddPreCommitHook(ctx context.Context, hook TxnFunc)

func WithDatabase

func WithDatabase(ctx context.Context, p DatabaseProvider, fn TxnFunc) error

WithDatabase executes fn with the context provided by p.WithDatabase. It does not run inside a transaction, so all database operations will be executed in their own transaction.

func WithReadTxn added in v0.18.0

func WithReadTxn(ctx context.Context, m Manager, fn TxnFunc) error

WithReadTxn executes fn in a transaction. If fn returns an error then the transaction is rolled back. Otherwise it is committed. Transaction is not exclusive and does not enforce read-only restrictions. Multiple threads can run transactions using this function concurrently, but concurrent writes may result in locked database error.

func WithTxn

func WithTxn(ctx context.Context, m Manager, fn TxnFunc) error

WithTxn executes fn in a transaction. If fn returns an error then the transaction is rolled back. Otherwise it is committed. Transaction is exclusive. Only one thread may run a transaction using this function at a time. This function will wait until the lock is available before executing. This function should be used for making changes to the database.

Types

type DatabaseProvider

type DatabaseProvider interface {
	WithDatabase(ctx context.Context) (context.Context, error)
}

type Manager

type Manager interface {
	Begin(ctx context.Context, exclusive bool) (context.Context, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error

	IsLocked(err error) bool
}

type MustFunc added in v0.20.0

type MustFunc func(ctx context.Context)

MustFunc is a function that is used in transaction hooks. It does not return an error.

type Retryer

type Retryer struct {
	Manager Manager
	// use value < 0 to retry forever
	Retries int
	OnFail  func(ctx context.Context, err error, attempt int) error
}

Retryer is a provides WithTxn function that retries the transaction if it fails with a locked database error. Transactions are run in exclusive mode.

func (Retryer) WithTxn

func (r Retryer) WithTxn(ctx context.Context, fn TxnFunc) error

type TxnFunc

type TxnFunc func(ctx context.Context) error

TxnFunc is a function that is used in transaction hooks. It should return an error if something went wrong.

Jump to

Keyboard shortcuts

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