2022-05-19 07:49:32 +00:00
|
|
|
package txn
|
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
)
|
2022-05-19 07:49:32 +00:00
|
|
|
|
|
|
|
type Manager interface {
|
|
|
|
Begin(ctx context.Context) (context.Context, error)
|
|
|
|
Commit(ctx context.Context) error
|
|
|
|
Rollback(ctx context.Context) error
|
2022-07-13 06:30:54 +00:00
|
|
|
|
2022-08-11 06:14:57 +00:00
|
|
|
IsLocked(err error) bool
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
AddPostCommitHook(ctx context.Context, hook TxnFunc)
|
|
|
|
AddPostRollbackHook(ctx context.Context, hook TxnFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatabaseProvider interface {
|
|
|
|
WithDatabase(ctx context.Context) (context.Context, error)
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TxnFunc func(ctx context.Context) error
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
// WithTxn executes fn in a transaction. If fn returns an error then
|
|
|
|
// the transaction is rolled back. Otherwise it is committed.
|
2022-05-19 07:49:32 +00:00
|
|
|
func WithTxn(ctx context.Context, m Manager, fn TxnFunc) error {
|
|
|
|
var err error
|
|
|
|
ctx, err = m.Begin(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
// a panic occurred, rollback and repanic
|
|
|
|
_ = m.Rollback(ctx)
|
|
|
|
panic(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// something went wrong, rollback
|
|
|
|
_ = m.Rollback(ctx)
|
|
|
|
} else {
|
|
|
|
// all good, commit
|
|
|
|
err = m.Commit(ctx)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = fn(ctx)
|
|
|
|
return err
|
|
|
|
}
|
2022-07-13 06:30:54 +00:00
|
|
|
|
|
|
|
// 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 WithDatabase(ctx context.Context, p DatabaseProvider, fn TxnFunc) error {
|
|
|
|
var err error
|
|
|
|
ctx, err = p.WithDatabase(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(ctx)
|
|
|
|
}
|
2022-08-11 06:14:57 +00:00
|
|
|
|
|
|
|
type Retryer struct {
|
|
|
|
Manager Manager
|
2022-09-01 07:54:34 +00:00
|
|
|
// use value < 0 to retry forever
|
2022-08-11 06:14:57 +00:00
|
|
|
Retries int
|
|
|
|
OnFail func(ctx context.Context, err error, attempt int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Retryer) WithTxn(ctx context.Context, fn TxnFunc) error {
|
|
|
|
var attempt int
|
|
|
|
var err error
|
2022-09-01 07:54:34 +00:00
|
|
|
for attempt = 1; attempt <= r.Retries || r.Retries < 0; attempt++ {
|
2022-08-11 06:14:57 +00:00
|
|
|
err = WithTxn(ctx, r.Manager, fn)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Manager.IsLocked(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.OnFail != nil {
|
|
|
|
if err := r.OnFail(ctx, err, attempt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed after %d attempts: %w", attempt, err)
|
|
|
|
}
|