https://github.com/pumpkinseed/locker
A distributed lock service client for etcd
https://github.com/pumpkinseed/locker
distributed-lock distributed-storage distributed-systems
Last synced: 7 months ago
JSON representation
A distributed lock service client for etcd
- Host: GitHub
- URL: https://github.com/pumpkinseed/locker
- Owner: PumpkinSeed
- License: mit
- Created: 2018-03-07T14:03:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-08T15:25:01.000Z (over 7 years ago)
- Last Synced: 2025-04-14T13:16:08.891Z (7 months ago)
- Topics: distributed-lock, distributed-storage, distributed-systems
- Language: Go
- Homepage:
- Size: 2.76 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# locker
A distributed lock service client for [etcd](https://github.com/coreos/etcd).
**Forked from github.com/jagregory/locker**
#### Features
- Lock/Unlock mechanism
- Migrate to `github.com/coreos/etcd/clientv3`
- TTL
[](https://godoc.org/github.com/PumpkinSeed/locker)
[](https://travis-ci.org/PumpkinSeed/locker)
## What? Why?
A distributed lock service is somewhat self-explanatory. Locking (mutexes) as a service that's distributed across a cluster.
What a lock service gives you is a key-value store with locking semantics, and a mechanism for watching state changes. The distributed part of a distributed lock service is thanks to the etcd underpinnings, and it simply means your locks are persisted across a cluster.
## Usage
### Creating a lock and release it with the Unlock
```go
import "github.com/PumpkinSeed/locker"
// Add etcd cluster nodes
var machines = []string{"http://127.0.0.1:2379", "http://127.0.0.1:2380", "http://127.0.0.1:2381"}
// Add timeout for etcd DialTimeout option
var timeout = int64(5)
// Add ttl (time-to-live)
var ttl = int64(5)
// Create the locker client.
client, err := locker.New(machines, timeout, ttl, context.Background())
var key = "1231231-123123-123"
quit := make(chan bool)
report := client.Lock(key, locker.DefaultValue, quit)
// report has a Msg and an Err field, Msg will contains 'success' or 'fail' operations.
```
### Report
- Report returned by the `Lock`, it has a Msg and an Err field
- The Msg Success if the key not locked yet, and Fail if the key locked
- The Err will return an error occured among the lock mechanism
### Releasing the lock
The third argument to `Lock` is a `quit` channel. Push anything into this channel to kill the locking, which will delete the locked key from the etcd.
```go
client.Unlock(key, quit)
```
### Watching a lock (deprecated)
An interesting aspect of lock services is the ability to watch a lock that isn't owned by you. A service can alter its behaviour depending on the value of a lock. You can use the `Watch` function to watch the value of a lock.
```go
valueChanges := make(chan string)
go client.Watch("key", valueChanges, nil)
select {
case change := <-valueChanges
fmt.Printf("Lock value changed: %s", change)
}
```
Quitting works the same way as `Lock`.
## Contribution
- Docker environment provided for testing etcd in the docker directory.
- utils.go has a debug option, currently in `false` if it's `true` than it will log debug messages to the os.Stdout