https://github.com/ggicci/distlock
:lock: Simple Distributed Locks in Go using Redis, MySQL, PostgreSQL, MongoDB, etc.
https://github.com/ggicci/distlock
distributed-lock distributed-mutex go go-mongo-mutex go-redis-lock go-redis-mutex go-sql-mutex mutex named-lock
Last synced: 5 months ago
JSON representation
:lock: Simple Distributed Locks in Go using Redis, MySQL, PostgreSQL, MongoDB, etc.
- Host: GitHub
- URL: https://github.com/ggicci/distlock
- Owner: ggicci
- License: mit
- Created: 2020-04-25T09:22:44.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-11-25T19:07:49.000Z (over 1 year ago)
- Last Synced: 2026-01-14T20:45:17.456Z (5 months ago)
- Topics: distributed-lock, distributed-mutex, go, go-mongo-mutex, go-redis-lock, go-redis-mutex, go-sql-mutex, mutex, named-lock
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 38
- Watchers: 1
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# distlock
[](https://github.com/ggicci/distlock/actions/workflows/go.yml) [](https://codecov.io/gh/ggicci/distlock) [](https://pkg.go.dev/github.com/ggicci/distlock)
Simple Distributed Locks implementation in Go
backed on
Redis, MySQL, PostgreSQL, MongoDB etc.
## Features
1. Namespace (names in the same namespace are unique, default namespace is `"default"`)
2. Auto/No expiration (auto-released after a specific time or never expire)
3. Support multiple backends:
- [x] Redis
- [x] MySQL
- [x] PostgreSQL
- [ ] MongoDB
## Usage
```go
import (
"github.com/ggicci/distlock"
"github.com/gomodule/redigo/redis"
)
var redisPool = &redis.Pool{
// ... configure your redis client
}
var heavyRequestsGuard = distlock.New(
distlock.NewRedisProvider(redisPool),
distlock.WithNamespace("heavy_requests"),
distlock.WithLockLifetime(10 * time.Second), // lifetime: 10s
)
user := session.CurrentUser()
mu := heavyRequestsGuard.New(
user.Username,
WithLockLifetime(time.Minute), // override the default lifetime option: 10s
)
if err := mu.Lock(); err != nil {
return err
}
defer mu.Unlock()
// do sth.
```