pgmutex

package module
v0.0.0-...-a30407a Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2019 License: MIT Imports: 2 Imported by: 0

README

go-pg-mutex

Build Codecov ReportCard GoDoc License

Mutex lock based on PostgreSQL advisory locks for Go. Can be used to acquire a lock on a resource exclusively across several running instances of the application.

Install

$ go get -u -v github.com/vgarvardt/go-pg-mutex

PostgreSQL drivers

The store accepts an adapter interface that interacts with the DB. Adapter and implementations are extracted to separate package github.com/vgarvardt/go-pg-adapter for easier maintenance.

Usage example

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/jackc/pgx"
	"github.com/vgarvardt/go-pg-adapter/pgxadapter"
	"github.com/vgarvardt/go-pg-mutex"
)

func main() {
	pgxConnConfig, _ := pgx.ParseURI(os.Getenv("DB_URI"))
	pgxConn, _ := pgx.Connect(pgxConnConfig)
	
	m, _ := pgmutex.New(pgxadapter.NewConn(pgxConn))
	
	useExclusiveResource(m)
	
	if !useExclusiveResourceOrNoOp(m) {
		fmt.Println("Resource is busy, doing nothing")
	}
}

func useExclusiveResource(m *pgmutex.PgMutex) {
	lockName := "my-lock"
	_ := m.Lock(lockName)
	defer m.Unlock(lockName)

	// do something with resource exclusively across several instances
}

func useExclusiveResourceOrNoOp(m *pgmutex.PgMutex) bool {
	lockName := "my-try-lock"
	if success, _ := m.TryLock(lockName); !success {
		return false
	}
	defer m.Unlock(lockName)

	// do something with resource exclusively across several instances
}

How to run tests

You will need running PostgreSQL instance. E.g. the one running in docker and exposing a port to a host system

docker run --rm -p 5432:5432 -it -e POSTGRES_PASSWORD=mutex -e POSTGRES_USER=mutex -e POSTGRES_DB=mutex postgres:10

Now you can run tests using the running PostgreSQL instance using PG_URI environment variable

PG_URI=postgres://mutex:mutex@localhost:5432/mutex?sslmode=disable go test -cover ./...

MIT License

Copyright (c) 2019 Vladimir Garvardt

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(mutex *PgMutex)

Option is the configuration options type for PgMutex

func WithInitTableDisabled

func WithInitTableDisabled() Option

WithInitTableDisabled returns option that disables table creation on PgMutex instantiation

func WithTableName

func WithTableName(tableName string) Option

WithTableName returns option that sets PgMutex table name

type PgMutex

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

PgMutex is the mutex lock based on PostgreSQL advisory locks

func New

func New(adapter pgadapter.Adapter, options ...Option) (*PgMutex, error)

New instantiates and prepares PgMutex instance

func (*PgMutex) Lock

func (m *PgMutex) Lock(name string) error

Lock puts a named lock or waits until the the resource becomes available

func (*PgMutex) TableName

func (m *PgMutex) TableName() string

TableName returns currently set mutex table name

func (*PgMutex) TryLock

func (m *PgMutex) TryLock(name string) (bool, error)

TryLock puts a named lock immediately and returns true or returns false if the lock can not be acquired immediately

func (*PgMutex) Unlock

func (m *PgMutex) Unlock(name string) error

Unlock releases a previously-acquired exclusive session level advisory lock

Jump to

Keyboard shortcuts

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