txn

package
v0.0.0-...-4f3f676 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = time.Second * 30
)

Variables

This section is empty.

Functions

func IsErrRetryable

func IsErrRetryable(err error) bool

IsErrRetryable returns true if the given error might be transient and the interaction can be safely retried. See: https://github.com/canonical/go-dqlite/v2/issues/220

func WithMetrics

func WithMetrics(ctx context.Context, metrics Metrics) context.Context

WithMetrics returns a new context with the given metrics.

Types

type MetricErrorType

type MetricErrorType string

MetricErrorType is the type of error that should be recorded.

type Metrics

type Metrics interface {
	// RecordSuccess records a successful operation.
	RecordSuccess()
	// RecordError records an error of the given error type.
	RecordError(MetricErrorType)
}

Metrics is the interface that must be implemented by the metrics collector.

func MetricsFromContext

func MetricsFromContext(ctx context.Context) Metrics

MetricsFromContext returns the metrics from the given context. If no metrics are found, then a noops metrics is returned.

type Option

type Option func(*option)

Option defines a function for setting options on a TransactionRunner.

func WithLogger

func WithLogger(logger logger.Logger) Option

WithLogger defines a logger for the transaction.

func WithRetryStrategy

func WithRetryStrategy(retryStrategy RetryStrategy) Option

WithRetryStrategy defines a retry strategy for the transaction.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout defines a timeout for the transaction. This is useful for defining a timeout for a transaction that is expected to take longer than the default timeout.

type RetryStrategy

type RetryStrategy func(context.Context, func() error) error

RetryStrategy defines a function for retrying a transaction.

type RetryingTxnRunner

type RetryingTxnRunner struct {
	// contains filtered or unexported fields
}

RetryingTxnRunner defines a generic runner for applying transactions to a given database. It expects that no individual transaction function should take longer than the default timeout. Transient errors are retried based on the defined retry strategy.

func NewRetryingTxnRunner

func NewRetryingTxnRunner(opts ...Option) *RetryingTxnRunner

NewRetryingTxnRunner returns a new RetryingTxnRunner.

func (*RetryingTxnRunner) Retry

func (t *RetryingTxnRunner) Retry(ctx context.Context, fn func() error) error

Retry defines a generic retry function for applying a function that interacts with the database. It will retry in cases of transient known database errors.

func (*RetryingTxnRunner) StdTxn

func (t *RetryingTxnRunner) StdTxn(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) error

StdTxn executes the input function against the tracked database, within a transaction that depends on the input context. Retry semantics are applied automatically based on transient failures. This is the function that almost all downstream database consumers should use.

This should not be used directly, instead the TxnRunner should be used to handle transactions.

func (*RetryingTxnRunner) Txn

func (t *RetryingTxnRunner) Txn(ctx context.Context, db *sqlair.DB, fn func(context.Context, *sqlair.TX) error) error

Txn executes the input function against the tracked database, using the sqlair package. The sqlair package provides a mapping library for SQL queries and statements. Retry semantics are applied automatically based on transient failures. This is the function that almost all downstream database consumers should use.

This should not be used directly, instead the TxnRunner should be used to handle transactions.

Jump to

Keyboard shortcuts

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