ctrlctx

package
v0.0.0-...-d6d9ae6 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: AGPL-3.0, Apache-2.0, CC-BY-SA-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoAuthContext   = errors.New("bug: there is no authorization in this context")
	ErrUnauthenticated = errors.New("unauthenticated request")
)
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 CurrentAuth

CurrentAuth returns the arvados.User whose privileges should be used in the given context, and the arvados.APIClientAuthorization the caller presented in order to authenticate the current request.

Returns ErrUnauthenticated if the current request was not authenticated (no token provided, token is expired, etc).

func CurrentTx

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

CurrentTx returns a transaction that will be committed after the current API call completes, or rolled back if the current API call returns an 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, getdb)
	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 NewTx

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

NewTx starts a new transaction. The caller is responsible for calling Commit or Rollback. This is suitable for database queries that are separate from the API transaction (see CurrentTx), e.g., ones that will be committed even if the API call fails, or held open after the API call finishes.

func NewWithToken

func NewWithToken(ctx context.Context, cluster *arvados.Cluster, token string) context.Context

NewWithToken returns a context with the provided auth token.

The incoming context must come from WrapCallsInTransactions or NewWithTransaction.

Used for attaching system auth to background threads.

Also useful for tests, where context doesn't necessarily come from a router that uses WrapCallsWithAuth.

The returned context comes with its own token lookup cache, so NewWithToken is not appropriate to use in a per-request code path.

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.

func WrapCallsWithAuth

func WrapCallsWithAuth(cluster *arvados.Cluster) func(api.RoutableFunc) api.RoutableFunc

WrapCallsWithAuth returns a call wrapper (suitable for assigning to router.router.WrapCalls) that makes CurrentUser(ctx) et al. work from inside the wrapped functions.

The incoming context must come from WrapCallsInTransactions or NewWithTransaction.

Types

type DBConnector

type DBConnector struct {
	PostgreSQL arvados.PostgreSQL
	// contains filtered or unexported fields
}

func (*DBConnector) Close

func (dbc *DBConnector) Close() error

func (*DBConnector) GetDB

func (dbc *DBConnector) GetDB(ctx context.Context) (*sqlx.DB, error)

Jump to

Keyboard shortcuts

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