txguard

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

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package txguard provides a utility class that keeps a database transaction active on a periodic basis.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Guard

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

Guard protects long-running database transactions in two ways. First, it will periodically execute a test statement on an existing database transaction to prevent an idle-in-session timeout from occurring. Secondly, it will roll the guarded transaction back if IsAlive is not called on a regular basis. The keepalive behavior will cease when either Commit or Rollback are called or if IsAlive is not called on a periodic basis.

func New

func New(tx pgx.Tx, options ...Option) *Guard

New constructs a Guard around the given transaction. Once this function is called, the transaction should only ever be accessed via Guard.Use.

func (*Guard) Commit

func (g *Guard) Commit(ctx context.Context) error

Commit the underlying transaction.

func (*Guard) IsAlive

func (g *Guard) IsAlive() error

IsAlive returns an error if the transaction has been committed, rolled back, or if it experienced an error while keeping it alive.

func (*Guard) Rollback

func (g *Guard) Rollback()

Rollback is safe to call multiple times.

func (*Guard) Use

func (g *Guard) Use(fn func(tx types.StagingQuerier) error) error

Use accesses the underlying transaction in a thread-safe manner.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option can be passed to New.

func WithMaxMisses

func WithMaxMisses(count int) Option

WithMaxMisses returns an Option that controls how many guard periods can elapse before the transaction is rolled back.

func WithPeriod

func WithPeriod(d time.Duration) Option

WithPeriod controls how often the keepalive statement is executed and how often the Guard checks for calls to Guard.IsAlive.

func WithQuery

func WithQuery(s string) Option

WithQuery allows the health-check database query to be overridden.

Jump to

Keyboard shortcuts

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