Skip to content

Go library for acquiring and releasing PostgreSQL's Advisory Locks with added support for pgx.

License

Notifications You must be signed in to change notification settings

zikani03/pgadvisorylock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgadvisorylock

Go library for acquiring and releasing PostgreSQL's Advisory Locks with added support for pgx.

Use in your project

$ go get github.com/zikani03/pgadvisorylock

Example Usage

package main

import (
    "context"
    "github.com/zikani03/pgadvisorylock"
)

func main() {
    // conn is *sql.DB wherever you get your flavour from
    ctx := context.Context
    ok, id, err := pgadvisorylock.AcquireLock(conn, ctx, "person:1")
    if !ok {
        panic("Failed to acquire lock")
    }

    ok, err = pgadvisorylock.ReleaseLock(conn, ctx, id)
    if !ok {
        panic("Failed to release lock")
    }

    ok, id, err = pgadvisorylock.AcquireSharedLock(conn, ctx, "person:1")
    if !ok {
        panic("Failed to acquire lock")
    }


    advisoryLocks, err = pgadvisorylock.FetchAdvisoryLocks(conn, ctx)
    if err != nil {
        panic("Failed to fetch locks")
    }

    for _, l := range advisoryLocks {
        fmt.Printf("LockID:%s, ClassID:%s, PID:%s\n", string(l.ObjectID), string(l.ClassID), string(l.PID))
    }


    ok, err = pgadvisorylock.ReleaseSharedLock(conn, ctx, id)
    if !ok {
        panic("Failed to release lock")
    }
}

Copyright (c) Zikani Nyirenda Mwase

About

Go library for acquiring and releasing PostgreSQL's Advisory Locks with added support for pgx.

Topics

Resources

License

Stars

Watchers

Forks

Languages