Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bsm/redislock
Simplified distributed locking implementation using Redis
https://github.com/bsm/redislock
distlock distributed distributed-lock golang lock redis
Last synced: 2 days ago
JSON representation
Simplified distributed locking implementation using Redis
- Host: GitHub
- URL: https://github.com/bsm/redislock
- Owner: bsm
- License: other
- Created: 2019-06-24T11:10:10.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-03-21T12:29:37.000Z (9 months ago)
- Last Synced: 2024-11-27T12:02:44.858Z (16 days ago)
- Topics: distlock, distributed, distributed-lock, golang, lock, redis
- Language: Go
- Size: 68.4 KB
- Stars: 1,482
- Watchers: 16
- Forks: 158
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - redis-lock - Simplified distributed locking implementation using Redis. (Distributed Systems / Search and Analytic Databases)
- awesome-go - redis-lock - Simplified distributed locking implementation using Redis. Stars:`1.5K`. (Distributed Systems / Search and Analytic Databases)
- awesome-go-extra - redislock - 06-24T11:10:10Z|2022-08-22T07:47:00Z| (Distributed Systems / Advanced Console UIs)
README
# redislock
[![Test](https://github.com/bsm/redislock/actions/workflows/test.yml/badge.svg)](https://github.com/bsm/redislock/actions/workflows/test.yml)
[![GoDoc](https://godoc.org/github.com/bsm/redislock?status.png)](http://godoc.org/github.com/bsm/redislock)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)Simplified distributed locking implementation using [Redis](http://redis.io/topics/distlock).
For more information, please see examples.## Examples
```go
import (
"context"
"fmt"
"log"
"time""github.com/bsm/redislock"
"github.com/redis/go-redis/v9"
)func main() {
// Connect to redis.
client := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: "127.0.0.1:6379",
})
defer client.Close()// Create a new lock client.
locker := redislock.New(client)ctx := context.Background()
// Try to obtain lock.
lock, err := locker.Obtain(ctx, "my-key", 100*time.Millisecond, nil)
if err == redislock.ErrNotObtained {
fmt.Println("Could not obtain lock!")
} else if err != nil {
log.Fatalln(err)
}// Don't forget to defer Release.
defer lock.Release(ctx)
fmt.Println("I have a lock!")// Sleep and check the remaining TTL.
time.Sleep(50 * time.Millisecond)
if ttl, err := lock.TTL(ctx); err != nil {
log.Fatalln(err)
} else if ttl > 0 {
fmt.Println("Yay, I still have my lock!")
}// Extend my lock.
if err := lock.Refresh(ctx, 100*time.Millisecond, nil); err != nil {
log.Fatalln(err)
}// Sleep a little longer, then check.
time.Sleep(100 * time.Millisecond)
if ttl, err := lock.TTL(ctx); err != nil {
log.Fatalln(err)
} else if ttl == 0 {
fmt.Println("Now, my lock has expired!")
}}
```## Documentation
Full documentation is available on [GoDoc](http://godoc.org/github.com/bsm/redislock)