https://github.com/xyctruth/lock
Go distributed lock by etcd
https://github.com/xyctruth/lock
Last synced: 6 months ago
JSON representation
Go distributed lock by etcd
- Host: GitHub
- URL: https://github.com/xyctruth/lock
- Owner: xyctruth
- License: apache-2.0
- Created: 2022-07-26T10:30:48.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-28T08:06:28.000Z (almost 4 years ago)
- Last Synced: 2024-06-20T14:20:28.763Z (about 2 years ago)
- Language: Go
- Homepage:
- Size: 20.5 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Lock
Go distributed lock library
## ETCD Drive
```go
locker, _ := drive_etcd.NewEtcdLocker(
etcdClient.Config{
Endpoints: []string{"http://0.0.0.0:2379"},
},
)
lock1, err := locker.Acquire("key")
if err != nil {
fmt.Println(err)
return
}
defer lock1.Release()
// Do something
```
获取锁超时时间, 超过此时间没有获取到锁返回 `ErrDeadlineExceeded` 错误
```go
locker.Acquire(key, lock.WithAcquireTimeout(5*time.Second))
```
非堵塞尝试获取锁,如果没有获取到锁返回 `ErrAlreadyLocked` 错误
```go
locker.Acquire(key, lock.WithTry(true))
```
锁过期时间,如果为0则会自动续约不会过期,需要手动释放锁
```go
locker.Acquire(key, lock.WithLockTTL(5*time.Second))
```