Skip to content

Pgx-mutex is a distributed lock library using PostgreSQL.

License

Notifications You must be signed in to change notification settings

jokruger/pgx-mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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