lock

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package lock defines the Locker interface and implements the locking logic.

Index

Constants

View Source
const (
	// DefaultLockID is the id used to lock the database for migrations. It is a crc64 hash of the
	// string "goose". This is used to ensure that the lock is unique to goose.
	//
	// crc64.Checksum([]byte("goose"), crc64.MakeTable(crc64.ECMA))
	DefaultLockID int64 = 5887940537704921958
)

Variables

View Source
var (
	// ErrLockNotImplemented is returned when the database does not support locking.
	ErrLockNotImplemented = errors.New("lock not implemented")
	// ErrUnlockNotImplemented is returned when the database does not support unlocking.
	ErrUnlockNotImplemented = errors.New("unlock not implemented")
)

Functions

This section is empty.

Types

type SessionLocker

type SessionLocker interface {
	SessionLock(ctx context.Context, conn *sql.Conn) error
	SessionUnlock(ctx context.Context, conn *sql.Conn) error
}

SessionLocker is the interface to lock and unlock the database for the duration of a session. The session is defined as the duration of a single connection and both methods must be called on the same connection.

func NewPostgresSessionLocker

func NewPostgresSessionLocker(opts ...SessionLockerOption) (SessionLocker, error)

NewPostgresSessionLocker returns a SessionLocker that utilizes PostgreSQL's exclusive session-level advisory lock mechanism.

This function creates a SessionLocker that can be used to acquire and release a lock for synchronization purposes. The lock acquisition is retried until it is successfully acquired or until the failure threshold is reached. The default lock duration is set to 5 minutes, and the default unlock duration is set to 1 minute.

If you have long running migrations, you may want to increase the lock duration.

See SessionLockerOption for options that can be used to configure the SessionLocker.

type SessionLockerOption

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

SessionLockerOption is used to configure a SessionLocker.

func WithLockID

func WithLockID(lockID int64) SessionLockerOption

WithLockID sets the lock ID to use when locking the database.

If WithLockID is not called, the DefaultLockID is used.

func WithLockTimeout

func WithLockTimeout(period, failureThreshold uint64) SessionLockerOption

WithLockTimeout sets the max duration to wait for the lock to be acquired. The total duration will be the period times the failure threshold.

By default, the lock timeout is 300s (5min), where the lock is retried every 5 seconds (period) up to 60 times (failure threshold).

The minimum period is 1 second, and the minimum failure threshold is 1.

func WithUnlockTimeout

func WithUnlockTimeout(period, failureThreshold uint64) SessionLockerOption

WithUnlockTimeout sets the max duration to wait for the lock to be released. The total duration will be the period times the failure threshold.

By default, the lock timeout is 60s, where the lock is retried every 2 seconds (period) up to 30 times (failure threshold).

The minimum period is 1 second, and the minimum failure threshold is 1.

Jump to

Keyboard shortcuts

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