Skip to content

Lock for golang, support monolithic application lock or distributed lock.

License

Notifications You must be signed in to change notification settings

x-punch/x-locker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

X Locker

sync.Mutex can be used to lock in golang, but you need to define the mutex in advance. Sometimes what we want to lock is generated dynamicly, so this package is used to lock in dynamic. We can group some lock by id, and they shared the same mutex.

Usage

import locker "github.com/x-punch/x-locker"
l := locker.NewLocker()
l.Lock("id")
defer l.Unlock("id")
// do something

Redlock

Redsync provides a Redis-based distributed mutual exclusion lock implementation for Go as described in this post.

Usage

package main

import (
	"log"
	"sync"
	"time"

	"github.com/go-redis/redis/v8"
	"github.com/x-punch/x-locker/redlock"
)

func main() {
	locker := redlock.New([]redlock.RedisClient{redis.NewClient(&redis.Options{Addr: ":6379"})})

	l := locker.NewLock("id")
	if err := l.Lock(); err != nil {
		panic(err)
	}
	defer l.Unlock()
    
    // do something
}

About

Lock for golang, support monolithic application lock or distributed lock.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages