zeekayandHanzo Dev 51e10ac833 Build on Go 1.27
Build and vet are clean on the 1.27.1 toolchain. The test suite needs a
local redis-server binary, which is absent here and fails the same way
without this change.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 13:04:06 -07:00
2025-06-23 20:11:36 +06:00
2026-07-26 14:19:38 -07:00
2024-01-06 12:42:16 +06:00
2026-01-17 17:17:57 +06:00
2021-03-13 21:05:26 +06:00
2026-09-17 13:04:06 -07:00
2026-07-26 14:19:38 -07:00
2023-09-08 11:49:18 +06:00
2025-06-25 13:17:59 +06:00
2026-07-26 14:19:38 -07:00

Redsync

Go Reference Build Status

Redsync provides a KV-based distributed mutual exclusion lock implementation for Go as described in this post. A reference library (by antirez) for Ruby is available at github.com/antirez/redlock-rb.

Installation

Install Redsync using the go get command:

$ go get github.com/hanzokv/lock/v4

Driver implementations for several clients are installed; however, only the one used will be included in your project.

See the examples folder for usage of each driver.

Documentation

Usage

Error handling is simplified to panic for shorter example.

package main

import (
	kvlib "github.com/hanzokv/go/v9"
	"github.com/hanzokv/lock/v4"
	"github.com/hanzokv/lock/v4/kv/kvgo"
)

func main() {
	// Create a pool with hanzokv/go (or redigo) which is the pool redsync will
	// use while communicating with the KV store. This can also be any pool that
	// implements the `kv.Pool` interface.
	client := kvlib.NewClient(&kvlib.Options{
		Addr: "localhost:6379",
	})
	pool := kvgo.NewPool(client) // or, pool := redigo.NewPool(...)

	// Create an instance of redsync to be used to obtain a mutual exclusion
	// lock.
	rs := redsync.New(pool)

	// Obtain a new mutex by using the same name for all instances wanting the
	// same lock.
	mutexname := "my-global-mutex"
	mutex := rs.NewMutex(mutexname)

	// Obtain a lock for our given mutex. After this is successful, no one else
	// can obtain the same lock (the same mutex name) until we unlock it.
	if err := mutex.Lock(); err != nil {
		panic(err)
	}

	// Do your work that requires the lock.

	// Release the lock so other processes or threads can obtain a lock.
	if ok, err := mutex.Unlock(); !ok || err != nil {
		panic("unlock failed")
	}
}

Contributing

Contributions are welcome.

License

Redsync is available under the BSD (3-Clause) License.

Disclaimer

This code implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in production environments.

Real World Uses

Below is a list of public, open source projects that use Redsync:

  • Sourcegraph: Universal code search and intelligence platform. Uses Redsync in an internal cache implementation.
  • Open Match by Google: Flexible, extensible, and scalable video game matchmaking. Uses Redsync with its state store implementation.
  • Gocron by go-co-op: gocron is a job distributed scheduling package which lets you run Go functions at pre-determined intervals using a simple, human-friendly syntax. Uses Redsync with its distributed job scheduler implementation.

If you are using Redsync in a project please send a pull request to add it to the list.

S
Description
Distributed locks for Hanzo KV — fork of go-redsync/redsync retargeted at github.com/hanzokv/go
Readme BSD-3-Clause
780 KiB
Languages
Go 98.6%
Makefile 1.4%