Documentation ¶
Index ¶
- Constants
- func IsErrRetryable(err error) bool
- func WithMetrics(ctx context.Context, metrics Metrics) context.Context
- type MetricErrorType
- type Metrics
- type Option
- type RetryStrategy
- type RetryingTxnRunner
- func (t *RetryingTxnRunner) Retry(ctx context.Context, fn func() error) error
- func (t *RetryingTxnRunner) StdTxn(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) error
- func (t *RetryingTxnRunner) Txn(ctx context.Context, db *sqlair.DB, fn func(context.Context, *sqlair.TX) error) error
Constants ¶
const (
DefaultTimeout = time.Second * 30
)
Variables ¶
This section is empty.
Functions ¶
func IsErrRetryable ¶
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
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 ¶
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 ¶
WithLogger defines a logger for the transaction.
func WithRetryStrategy ¶
func WithRetryStrategy(retryStrategy RetryStrategy) Option
WithRetryStrategy defines a retry strategy for the transaction.
func WithTimeout ¶
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 ¶
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.