Simple Distributed Locks implementation in Go
backed on
Redis, MySQL, PostgreSQL, MongoDB etc.
- Namespace (names in the same namespace are unique, default namespace is
"default"
) - Auto/No expiration (auto-released after a specific time or never expire)
- Support multiple backends:
- Redis
- MySQL
- PostgreSQL
- MongoDB
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.