Git Product home page Git Product logo

go-locker's Introduction

go-locker

Build Status Coverage Status Go Reference Go Report Card

Distributed locking using Redis.

Example usage:

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/da440dil/go-locker"
	"github.com/go-redis/redis/v8"
)

func main() {
	client := redis.NewClient(&redis.Options{})
	defer client.Close()

	// Create locker.
	lkr := locker.NewLocker(client)
	ctx := context.Background()
	key := "key"
	err := client.Del(ctx, key).Err()
	requireNoError(err)

	lock := func() {
		// Try to apply lock.
		lr, err := lkr.Lock(ctx, key, time.Second)
		requireNoError(err)
		if !lr.OK() {
			fmt.Printf("Failed to apply lock, retry after %v\n", lr.TTL())
			return
		}
		fmt.Println("Lock applied")

		// Try to release lock.
		defer func() {
			ok, err := lr.Unlock(ctx)
			requireNoError(err)
			if ok {
				fmt.Println("Lock released")
			} else {
				fmt.Println("Failed to release lock")
			}
		}()

		time.Sleep(time.Millisecond * 100) // some code here

		// Optionally try to extend lock.
		r, err := lr.Lock.Lock(ctx, time.Second)
		requireNoError(err)
		if !r.OK() {
			fmt.Printf("Failed to extend lock, retry after %v\n", lr.TTL())
			return
		}
		fmt.Println("Lock extended")
	}

	var wg sync.WaitGroup
	wg.Add(2)
	for i := 0; i < 2; i++ {
		go func() {
			defer wg.Done()
			lock()
		}()
	}
	wg.Wait()
	// Output:
	// Lock applied
	// Failed to apply lock, retry after 999ms
	// Lock extended
	// Lock released
}

func requireNoError(err error) {
	if err != nil {
		panic(err)
	}
}

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.