All Projects → cirello-io → pglock

cirello-io / pglock

Licence: Apache-2.0 license
PostgreSQL Lock Client for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to pglock

mutexsafe
MutexSafe will help you use mutex more effectively. Different mutex for different components are presented. In addition, you can add your own lockers and use within the library.
Stars: ✭ 15 (-70%)
Mutual labels:  lock, locking
futex
File-based Ruby Mutex
Stars: ✭ 14 (-72%)
Mutual labels:  lock, locking
Concurrent
Functional Concurrency Primitives
Stars: ✭ 206 (+312%)
Mutual labels:  lock
React Focus On
🎯 Solution for WAI ARIA compatible modal dialogs or full-screen tasks, you were looking for
Stars: ✭ 180 (+260%)
Mutual labels:  lock
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+108%)
Mutual labels:  lock
Lock.swift
A Swift & iOS framework to authenticate using Auth0 and with a Native Look & Feel
Stars: ✭ 215 (+330%)
Mutual labels:  lock
ci
Run npm ci using the appropriate Node package manager (npm, yarn, pnpm)
Stars: ✭ 39 (-22%)
Mutual labels:  lock
activejob-uniqueness
Unique jobs for ActiveJob. Ensure the uniqueness of jobs in the queue.
Stars: ✭ 194 (+288%)
Mutual labels:  lock
keyway
A simple Bash lock file library.
Stars: ✭ 28 (-44%)
Mutual labels:  lock
django-concurrency-talk
🎭 Database Integrity in Django: Safely Handling Critical Data in Distributed Systems
Stars: ✭ 49 (-2%)
Mutual labels:  lock
ES-Timer
A USB timer powered by Digispark ATtiny85 according to 🍅 pomodoro time management technique
Stars: ✭ 45 (-10%)
Mutual labels:  lock
Javainterview
java中高级基础指南
Stars: ✭ 222 (+344%)
Mutual labels:  lock
dynalock
This is a small lock library written Go, which uses AWS DynamoDB as the data store.
Stars: ✭ 15 (-70%)
Mutual labels:  locking
Pottery
Redis for humans. 🌎🌍🌏
Stars: ✭ 204 (+308%)
Mutual labels:  lock
door-controller-test-tool
Door controller test tool for physical access control devices. (THIS PROJECT IS NO LONGER MAINTAINED)
Stars: ✭ 13 (-74%)
Mutual labels:  lock
Ninja Mutex
Mutex implementation for PHP
Stars: ✭ 180 (+260%)
Mutual labels:  lock
LockView
一个自定义的多功能的锁屏页解锁控件
Stars: ✭ 17 (-66%)
Mutual labels:  lock
browser-tabs-lock
Using this package, you can synchronise your code logic across browser tabs.
Stars: ✭ 29 (-42%)
Mutual labels:  lock
go-lock
go-lock is a lock library implementing read-write mutex and read-write trylock without starvation
Stars: ✭ 78 (+56%)
Mutual labels:  lock
react-native-awesome-pin
A highly interactive and customisable PIN code screen for React Native.
Stars: ✭ 28 (-44%)
Mutual labels:  lock

PostgreSQL Lock Client for Go

GoDoc Build status Coverage Status Go Report Card Mentioned in Awesome Go SLA

The PostgreSQL Lock Client for Go is a general purpose distributed locking library built for PostgreSQL. The PostgreSQL Lock Client for Go supports both fine-grained and coarse-grained locking as the lock keys can be any arbitrary string, up to a certain length. Please create issues in the GitHub repository with questions, pull request are very much welcome.

Recommended PostgreSQL version: 9.6 or newer

Use cases

A common use case for this lock client is: let's say you have a distributed system that needs to periodically do work on a given campaign (or a given customer, or any other object) and you want to make sure that two boxes don't work on the same campaign/customer at the same time. An easy way to fix this is to write a system that takes a lock on a customer, but fine-grained locking is a tough problem. This library attempts to simplify this locking problem on top of PostgreSQL.

Another use case is leader election. If you only want one host to be the leader, then this lock client is a great way to pick one. When the leader fails, it will fail over to another host within a customizable lease duration that you set.

Getting Started

To use the PostgreSQL Lock Client for Go, you must make it sure it is present in $GOPATH or in your vendor directory.

$ go get -u cirello.io/pglock

This package has the go.mod file to be used with Go's module system. If you need to work on this package, use go mod edit -replace=cirello.io/pglock@yourlocalcopy.

For your convenience, there is a function in the package called CreateTable that you can use to set up your table. The package level documentation comment has an example of how to use this package. Here is some example code to get you started:

package main

import (
	"log"

	"cirello.io/pglock"
)

func main() {
	db, err := sql.Open("postgres", *dsn)
	if err != nil {
		log.Fatal("cannot connect to test database server:", err)
	}
	c, err := pglock.New(db,
		pglock.WithLeaseDuration(3*time.Second),
		pglock.WithHeartbeatFrequency(1*time.Second),
	)
	if err != nil {
		log.Fatal("cannot create lock client:", err)
	}
	if err := c.CreateTable(); err != nil {
		log.Fatal("cannot create table:", err)
	}
	l, err := c.Acquire("lock-name")
	if err != nil {
		log.Fatal("unexpected error while acquiring 1st lock:", err)
	}
	defer l.Close()
	// execute the logic
}

Selected Features

Send Automatic Heartbeats

When you create the lock client, you can specify WithHeartbeatFrequency(time.Duration) like in the above example, and it will spawn a background goroutine that continually updates the record version number on your locks to prevent them from expiring (it does this by calling the SendHeartbeat() method in the lock client.) This will ensure that as long as your application is running, your locks will not expire until you call Release() or lockItem.Close()

Read the data in a lock without acquiring it

You can read the data in the lock without acquiring it. Here's how:

lock, err := lockClient.Get("kirk");

Logic to avoid problems with clock skew

The lock client never stores absolute times in PostgreSQL. The way locks are expired is that a call to tryAcquire reads in the current lock, checks the record version number of the lock and starts a timer. If the lock still has the same after the lease duration time has passed, the client will determine that the lock is stale and expire it.

What this means is that, even if two different machines disagree about what time it is, they will still avoid clobbering each other's locks.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].