Giter Site home page Giter Site logo

palcalde / redsync Goto Github PK

View Code? Open in Web Editor NEW

This project forked from go-redsync/redsync

0.0 1.0 0.0 577 KB

Distributed mutual exclusion lock using Redis for Go

Home Page: http://redis.io/topics/distlock

License: BSD 3-Clause "New" or "Revised" License

Go 98.92% Makefile 1.08%

redsync's Introduction

Redsync

Go Reference Build Status

Redsync provides a Redis-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/go-redsync/redsync/v4

Two driver implementations will be 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 (
	goredislib "github.com/redis/go-redis/v9"
	"github.com/go-redsync/redsync/v4"
	"github.com/go-redsync/redsync/v4/redis/goredis/v9"
)

func main() {
	// Create a pool with go-redis (or redigo) which is the pool redisync will
	// use while communicating with Redis. This can also be any pool that
	// implements the `redis.Pool` interface.
	client := goredislib.NewClient(&goredislib.Options{
		Addr: "localhost:6379",
	})
	pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...)

	// Create an instance of redisync 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.

redsync's People

Contributors

hjr265 avatar rfyiamcool avatar nathanbaulch avatar btomasini avatar bancek avatar bombsimon avatar apty avatar appleboy avatar maruhyl avatar mehdijoafshani avatar reinkrul avatar keiichihirobe avatar kamronbatman avatar solidshake avatar nvartolomei avatar rayzyar avatar x-martinez avatar vadimkulagin avatar milonoir avatar msroy-asapp avatar lzakharov avatar ldcicconi avatar darful avatar fracasula avatar dcormier avatar subtlepseudonym avatar cwjenkins avatar khssnv avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.