Skip to content

a distributed lock implemented by redis/etcd/zookeeper

Notifications You must be signed in to change notification settings

chuckchann/distributed-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

distributed-lock

avatar avatar avatar

distributed-lock is a high performance distributed mutex written in Go. It provides three implements, including redis, etcd and zookeeper.

Implement

  • redis: guarantee AP
  • etcd: guarantee CP
  • zookeeper: guarantee CP

Features

  • provide multi implements, including redis, etcd and zookeeper.
  • in redis implement: use lua script to lock/unlock redis lock, watch lock after acquiring lock, subscribe a topic when acquire lock failed ,when receive message from this topic then can acquire lock again (which can reduce interaction with redis).

How to use

download package

go get -u github.com/chuckchann/distributed-lock

example

func main()  {
	// init redis/etcd/zookeeper client berfore you use lock !!!
	redis_lock.Init(entry.Config{
		Endpoints:   []string{"127.0.0.1:6379"},
		DBIndex:     15,
		MaxConns:    20,
		IdleTimeout: 10 * time.Second,
		Password: 	"123456",
	})
	
	// create a new redis lock
	l := distributed_lock.New("test")
	
    // acquire lock
	err := l.Lock()
	if err != nil {
		return
	}

	// acquire lock successfully

	// do something ...
	
	l.UnLock()
}

TODO

  • redis lock: change old lock policy(self spin if current lock was occupied by other client), the new lock policy just like redission.

About

a distributed lock implemented by redis/etcd/zookeeper

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages