An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# distlock

[![Go](https://github.com/ggicci/distlock/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/ggicci/distlock/actions/workflows/go.yml) [![codecov](https://codecov.io/gh/ggicci/distlock/branch/main/graph/badge.svg?token=2MDBW1V2TI)](https://codecov.io/gh/ggicci/distlock) [![Go Reference](https://pkg.go.dev/badge/github.com/ggicci/distlock.svg)](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.
```