Skip to content

DavidCai1111/etcd-lock

Repository files navigation

etcd-lock

Build Status Coverage Status

Distributed locks powered by etcd v3 for Go.

Installation

go get -u github.com/DavidCai1993/etcd-lock

Documentation

API documentation can be found here: https://godoc.org/github.com/DavidCai1993/etcd-lock

Usage

import (
  "github.com/DavidCai1993/etcd-lock"
)
locker, err := etcdlock.NewLocker(etcdlock.LockerOptions{
  Address:        "127.0.0.1:2379",
  DialOptions:    []grpc.DialOption{grpc.WithInsecure()},
})

if err != nil {
  log.Fatalln(err)
}

// Acquire a lock for a specified recource.
if _, err = locker.Lock(context.Background(), "resource_key", 5*time.Second); err != nil {
  log.Fatalln(err)
}

// This lock will be acquired after 5s, and before that current goroutine
// will be blocked.
anotherLock, err := locker.Lock(context.Background(), "resource_key", 5*time.Second)
if err != nil {
  log.Fatalln(err)
}

// Unlock the lock manually.
if err := anotherLock.Unlock(context.Background()); err != nil {
  log.Fatalln(err)
}

Releases

No releases published

Packages

No packages published