pgadvisorylock

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 4 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcquireLock

func AcquireLock(p *sql.DB, ctx context.Context, lockID string) (bool, int64, error)

AcquireLock acquires a session-level postgresql advisory lock Hashes the value with xxh3 hash to generate a unique lockID see: AcquireLock

func AcquireLockInt64

func AcquireLockInt64(p *sql.DB, ctx context.Context, lockID int64) (bool, error)

AcquireLock acquires a session-level postgresql advisory lock uses pg_try_advisory_lock which returns immediately

func AcquireSharedLock

func AcquireSharedLock(p *sql.DB, ctx context.Context, lockID string) (bool, int64, error)

AcquireLock acquires a session-level postgresql advisory lock uses pg_try_advisory_lock which returns immediately

func AcquireSharedLockInt64

func AcquireSharedLockInt64(p *sql.DB, ctx context.Context, lockID int64) (bool, error)

AcquireLock acquires a shared session-level postgresql advisory lock uses pg_try_advisory_lock which returns immediately

func AcquireTxnLock

func AcquireTxnLock(p *sql.Tx, ctx context.Context, lockID int64) (bool, error)

AcquireLock acquires a transaction-level postgresql advisory lock uses pg_try_advisory_xact_lock which returns immediately

func ReleaseLock

func ReleaseLock(p *sql.DB, ctx context.Context, lockID int64) (bool, error)

ReleaseLock releases an advisory lock and returns whether lock was released successfully or not

func ReleaseSharedLock

func ReleaseSharedLock(p *sql.DB, ctx context.Context, lockID int64) (bool, error)

ReleaseLock releases a shared session-level advisory lock and returns whether lock was released successfully or not

Types

type AdvisoryLock

type AdvisoryLock struct {
	Pid      int64  `json:"pid"`      // the process id of the process that acquired the lock
	ObjectID int64  `json:"objectID"` // ObjectID when using 32 bit lock with class id
	ClassID  int64  `json:"classID"`  // ClassID when using 32 bit lock with object id
	Granted  bool   `json:"granted"`  // Whether the lock is held or not
	Locktype string `json:"locktype"` // the type of lock
}

func FetchAdvisoryLocks

func FetchAdvisoryLocks(conn *sql.DB, ctx context.Context) ([]*AdvisoryLock, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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