txn

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 8 Imported by: 2

README

txn

Go Report Card

Overview

txn is a Go library focused on database transaction handling.

Features

  • Acting as a library, not a framework
  • Robust database transaction handling mechanisms
  • Simple and easy-to-use API

Installation

To install, run the following command:

go get github.com/struqt/txn

To optionally work with pgx/v5, run the following command:

go get github.com/struqt/txn/txn_pgx

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilArgument    = errors.New("nil argument")
	ErrNotImplemented = errors.New("not implemented")
)

Functions

func Execute added in v0.0.6

func Execute[
	O any,
	B any,
	D Doer[O, B],
	F DoFunc[O, B, D],
](ctx context.Context, db B, doer D, fn F) (err error)

Execute executes a transaction with the given Doer and function.

func Ping added in v0.0.6

func Ping(limit int, count PingCount, ping func(context.Context) error) (cnt int, err error)

Ping is a function that performs a repeatable ping operation with retry logic. Parameters: - limit: The maximum number of retry attempts. If set to <= 0, a default of 3 is used. - count: A function of type PingCount to report the number of attempts and delay. - ping: A function that takes a context and returns an error. It performs the actual ping operation. Returns: - cnt: The total number of attempts made. - err: Any error encountered

func RunTicker added in v0.0.11

func RunTicker(ctx context.Context, interval time.Duration, tickCount int, tick func(context.Context, int32))

Types

type DoFunc

type DoFunc[O any, B any, D Doer[O, B]] func(ctx context.Context, do D) error

DoFunc defines the function type for transaction execution.

type Doer

type Doer[TOptions any, TBeginner any] interface {
	Mutate(setters ...DoerFieldSetter)
	BeginTxn(context.Context, TBeginner) (Txn, error)
	Title() string
	Rethrow() bool
	Timeout() time.Duration
	MaxPing() int
	MaxRetry() int
	Options() TOptions
}

Doer defines the interface for database transaction operations.

type DoerBase

type DoerBase[TOptions any, TBeginner any] struct {
	// contains filtered or unexported fields
}

DoerBase provides a base implementation for the Doer interface.

func (*DoerBase[_, B]) BeginTxn added in v0.0.5

func (do *DoerBase[_, B]) BeginTxn(context.Context, B) (Txn, error)

BeginTxn begins a new transaction.

func (*DoerBase[_, _]) MaxPing added in v0.0.6

func (do *DoerBase[_, _]) MaxPing() int

MaxPing gets the maximum ping count.

func (*DoerBase[_, _]) MaxRetry added in v0.0.6

func (do *DoerBase[_, _]) MaxRetry() int

MaxRetry gets the maximum retry count.

func (*DoerBase[_, _]) Mutate added in v0.0.8

func (do *DoerBase[_, _]) Mutate(setters ...DoerFieldSetter)

Mutate applies field setters to modify DoerBase's fields.

func (*DoerBase[T, _]) Options

func (do *DoerBase[T, _]) Options() T

Options gets the options.

func (*DoerBase[_, _]) Rethrow added in v0.0.8

func (do *DoerBase[_, _]) Rethrow() bool

Rethrow gets the rethrow panic flag.

func (*DoerBase[_, _]) Timeout

func (do *DoerBase[_, _]) Timeout() time.Duration

Timeout gets the timeout duration.

func (*DoerBase[_, _]) Title

func (do *DoerBase[_, _]) Title() string

Title gets the title.

type DoerFieldSetter added in v0.0.8

type DoerFieldSetter func(*DoerFields)

DoerFieldSetter defines a function signature for setting DoerFields.

func WithMaxPing added in v0.0.8

func WithMaxPing(value int) DoerFieldSetter

WithMaxPing creates a field setter for the maximum ping count.

func WithMaxRetry added in v0.0.8

func WithMaxRetry(value int) DoerFieldSetter

WithMaxRetry creates a field setter for the maximum retry count.

func WithOptions added in v0.0.8

func WithOptions(value any) DoerFieldSetter

WithOptions creates a field setter for options.

func WithRethrow added in v0.0.8

func WithRethrow(value bool) DoerFieldSetter

WithRethrow creates a field setter for the rethrow flag.

func WithTimeout added in v0.0.8

func WithTimeout(value time.Duration) DoerFieldSetter

WithTimeout creates a field setter for the timeout duration.

func WithTitle added in v0.0.8

func WithTitle(value string) DoerFieldSetter

WithTitle creates a field setter for the title.

type DoerFields added in v0.0.8

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

DoerFields provides data fields for DoerBase struct.

type PingCount added in v0.0.7

type PingCount = func(cnt int, delay time.Duration)

PingCount is a type alias for a function that takes an integer count and a time duration as arguments. It is designed to keep track of the number of ping attempts and the delay before the next attempt.

type Txn

type Txn interface {
	Commit(context.Context) error   // Commit the transaction.
	Rollback(context.Context) error // Rollback the transaction.
}

Txn defines the basic database transaction interface.

Directories

Path Synopsis
txn_mongo module
txn_pgx module

Jump to

Keyboard shortcuts

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