package lock

import "github.com/pressly/goose/v3/lock"

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

Index

Constants

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

	// Default values for the lock (time to wait for the lock to be acquired) and unlock (time to
	// wait for the lock to be released) wait durations.
	DefaultLockTimeout   time.Duration = 60 * time.Minute
	DefaultUnlockTimeout time.Duration = 1 * time.Minute
)

Variables

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")
)

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 locks for synchronization purposes. The lock acquisition is retried until it is successfully acquired or until the maximum duration is reached. The default lock duration is set to 60 minutes, and the default unlock duration is set to 1 minute.

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(duration time.Duration) SessionLockerOption

WithLockTimeout sets the max duration to wait for the lock to be acquired.

func WithUnlockTimeout

func WithUnlockTimeout(duration time.Duration) SessionLockerOption

WithUnlockTimeout sets the max duration to wait for the lock to be released.

Source Files

postgres.go session_locker.go session_locker_options.go

Version
v3.15.1
Published
Oct 10, 2023
Platform
linux/amd64
Imports
6 packages
Last checked
2 hours ago

Tools for package owners.