GithubHelp home page GithubHelp logo

dlock's Introduction

dlock Locking Library

dlock provides a unified way to acquire and manage distributed locks using various 3rd party backends.

Introduction

Distributed systems often require coordination and synchronization to avoid conflicts and ensure smooth operation. dlock simplifies this process by offering an easy-to-use interface for acquiring and managing distributed locks.

Here's what you can do with this library:

  • Acquire locks with a customizable time-to-live (TTL).
  • Refresh locks to prevent automatic expiration.
  • Retry lock acquisition with a customizable interval and limit.

Supported Backends

  • SoonTM

Installation

go get github.com/charconstpointer/dlock

Usage

Lock Configuration

Before acquiring a lock, you can configure its behavior using the LockConfig struct and various LockOpt functions.

type LockConfig struct {
    // TTL is the time-to-live of the lock. If not refreshed within this time, it will be released.
    TTL time.Duration `json:"ttl"`
    // RefreshInterval is the interval at which the lock is refreshed.
    RefreshInterval time.Duration `json:"refresh_interval"`
    // RetryInterval is the interval at which the lock is retried if held by another client.
    RetryInterval time.Duration `json:"retry_interval"`
    // RetryLimit is the maximum number of retry attempts before giving up.
    RetryLimit int `json:"retry_limit"`
}

Here are some configuration options:

  • WithTTL: Set the time-to-live (TTL) of the lock.
  • WithRefreshInterval: Configure the refresh interval of the lock.
  • WithRetryInterval: Customize the retry interval for acquiring the lock.
  • WithRetryLimit: Set the maximum retry attempts before giving up.

Acquiring a Lock

To acquire a lock, you can use the Locker interface:

type Locker interface {
    Lock(key, value string, opts ...LockOpt) (*Lock, error)
}

Here's an example of acquiring a lock:

locker := NewLocker() // Initialize your locker instance
lock, err := locker.Lock("resource-key", "unique-value", WithTTL(time.Minute), WithRetryLimit(3))
if err != nil {
    // Handle lock acquisition failure
} else {
    // You've acquired the lock!
    // Access your critical resource here.
    defer lock.Release() // Release the lock when done.
}

dlock's People

Contributors

charconstpointer 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.