pgxmutex

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: MIT Imports: 6 Imported by: 2

README

pgx-mutex

Pgx-mutex is a distributed lock library using PostgreSQL.

Installation

go get github.com/jokruger/pgx-mutex

Basic Usage

Process 1

...
m, _ := pgxmutex.NewSyncMutex(
    pgxmutex.WithConnStr("postgres://postgres:postgres@localhost:5432/postgres"),
    pgxmutex.WithResourceID(123),
)
m.Lock()
fmt.Println("Process 1 is in critical section")
time.Sleep(10 * time.Second)
m.Unlock()
...

Process 2

...
m, _ := pgxmutex.NewSyncMutex(
    pgxmutex.WithConnStr("postgres://postgres:postgres@localhost:5432/postgres"),
    pgxmutex.WithResourceID(123),
)
m.Lock()
fmt.Println("Process 2 is in critical section")
time.Sleep(10 * time.Second)
m.Unlock()
...

For more examples and betchmarks refer to https://github.com/jokruger/distributed-lock-benchmark

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mutex

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

Mutex is a distributed lock based on PostgreSQL advisory locks

func NewMutex

func NewMutex(options ...Option) (*Mutex, error)

NewMutex initializes a new Mutex with provided options.

func (*Mutex) GetResourceID

func (m *Mutex) GetResourceID() int64

GetResourceID returns the lock ID.

func (*Mutex) Lock

func (m *Mutex) Lock() error

Lock tries to acquire the advisory lock, blocking until it's available.

func (*Mutex) SyncMutex

func (m *Mutex) SyncMutex() SyncMutex

SyncMutex is a wrapper around Mutex that implements sync.Locker interface.

func (*Mutex) TryLock

func (m *Mutex) TryLock() (bool, error)

TryLock attempts to acquire the advisory lock without blocking. Returns an error if unable to acquire the lock.

func (*Mutex) Unlock

func (m *Mutex) Unlock() error

Unlock releases the advisory lock if it's currently held.

type Option

type Option func(*Mutex) error

Option is a functional option type for configuring Mutex.

func WithConn

func WithConn(conn conn) Option

WithConn sets the custom DB connection.

func WithConnStr

func WithConnStr(connStr string) Option

WithConnStr creates new PGX connection from a connection string.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets a custom context for the Mutex operations.

func WithResourceID

func WithResourceID(id int64) Option

WithResourceID sets the lock ID for advisory locking.

type SyncMutex

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

func NewSyncMutex

func NewSyncMutex(opts ...Option) (*SyncMutex, error)

func (SyncMutex) Lock

func (sm SyncMutex) Lock()

func (SyncMutex) TryLock

func (sm SyncMutex) TryLock() bool

func (SyncMutex) Unlock

func (sm SyncMutex) Unlock()

Jump to

Keyboard shortcuts

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