ctrlctx

package
v0.0.0-...-c92af4d Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: AGPL-3.0, Apache-2.0, CC-BY-SA-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoTransaction   = errors.New("bug: there is no transaction in this context")
	ErrContextFinished = errors.New("refusing to start a transaction after wrapped function already returned")
)

Functions

func CurrentTx

func CurrentTx(ctx context.Context) (*sqlx.Tx, error)

func New

func New(ctx context.Context, getdb func(context.Context) (*sqlx.DB, error)) (context.Context, finishFunc)

New returns a new child context that can be used with CurrentTx(). It does not open a database transaction until the first call to CurrentTx().

The caller must eventually call the returned finishtx() func to commit or rollback the transaction, if any.

func example(ctx context.Context) (err error) {
	ctx, finishtx := New(ctx, dber)
	defer finishtx(&err)
	// ...
	tx, err := CurrentTx(ctx)
	if err != nil {
		return fmt.Errorf("example: %s", err)
	}
	return tx.ExecContext(...)
}

If *err is nil, finishtx() commits the transaction and assigns any resulting error to *err.

If *err is non-nil, finishtx() rolls back the transaction, and does not modify *err.

func NewWithTransaction

func NewWithTransaction(ctx context.Context, tx *sqlx.Tx) context.Context

NewWithTransaction returns a child context in which the given transaction will be used by any localdb API call that needs one. The caller is responsible for calling Commit or Rollback on tx.

func WrapCallsInTransactions

func WrapCallsInTransactions(getdb func(context.Context) (*sqlx.DB, error)) func(api.RoutableFunc) api.RoutableFunc

WrapCallsInTransactions returns a call wrapper (suitable for assigning to router.router.WrapCalls) that starts a new transaction for each API call, and commits only if the call succeeds.

The wrapper calls getdb() to get a database handle before each API call.

Types

This section is empty.

Jump to

Keyboard shortcuts

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