Coder Social home page Coder Social logo

disgo's Introduction

English | δΈ­ζ–‡

DisGo Introduce

DisGo is a distributed lock based on Redis, developed in Golang language. The name comes from Distributed, Disco and Golang, I wish you all to write code as easy as dancing disco.

Features of DisGo

Reentrant Lock

DisGo is a reentrant lock. It uses the Redis's Hash type as the lock, the hash-name is the lock name, the hash-key stores the unique id of the current lock-holding thread, and the hash-value stores the current number of locks.

Fair Lock

Golang itself does not have thread-safe queues to use. For convenience, DisGo uses Redis's ZSet to simulate queues, which ensures first-in first-out(FIFO) to a certain extent and provides a fair lock.

Auto-Renew

DisGo provides an automatic renewal function to prevent data errors caused by early release of locks before business execution is completed.

Spin Lock

DisGo is a spin lock, which will automatically retry the lock grab within the set waiting time until the lock is grabbed or the wait times out.

More Efficient

DisGo uses Redis's publish and subscribe, and will receive the message as soon as the lock is released, and then execute the lock according to the order of the waiting queue.

DisGo locking process

Click Me

API Introduction

Acquire lock object

    redisClient := redis.NewClient(&redis.Options{
        Network: "tcp",
        Addr:    "127.0.0.1:6379",
    })
    lock, err := disgo.GetLock(redisClient, "test")

Ordinary lock (no spin lock and automatic renewal required)

    success, err := lock.Lock(ctx, 5*time.Second, 10*time.Second)

Spin lock (no auto-renewal required)

    success, err := lock.TryLock(ctx, 5*time.Second, 10*time.Second)

Spin lock (auto-renew)

    success, err := lock.TryLockWithSchedule(ctx, 5*time.Second)

Unlock (Universal)

    success, err := lock.Release(ctx)

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.