Categorygithub.com/tangelo-labs/go-dmux
modulepackage
0.0.1
Repository: https://github.com/tangelo-labs/go-dmux.git
Documentation: pkg.go.dev

# README

go-dmux

go-dmux is a Go package that provides an abstraction for distributed mutexes. It allows you to create and manage distributed locks using different backends. Currently, it supports Redis, in-memory and MySQL as a backend.

Installation

To install go-dmux, use the go get command:

go get github.com/tangelo-labs/go-dmux

Usage

First, you need to create a factory for the mutexes. This factory will be responsible for creating new mutexes. Here is an example of how to create a factory that uses Redis as a backend:

cfg := dmux.RedisConfig{DSN: "redis://localhost:6379/0"}
factory, err := dmux.NewRedisFactory(cfg)
if err != nil {
    log.Fatalf("Failed to create factory: %v", err)
}

Once you have a factory, you can create a new mutex:

mu, err := factory.NewMutex(context.Background(), "my-mutex")
if err != nil {
    log.Fatalf("Failed to create mutex: %v", err)
}

To lock and unlock the mutex, use the Lock and Unlock methods:

err = mu.Lock(context.Background())
if err != nil {
    log.Fatalf("Failed to lock: %v", err)
}

// Do some work...

err = mu.Unlock(context.Background())
if err != nil {
    log.Fatalf("Failed to unlock: %v", err)
}

Testing

The package includes a test suite that you can run with the go test command:

go test ./...

Contributing

Contributions to go-dmux are welcome. Please submit a pull request or create an issue to discuss the changes you want to make.

License

go-dmux is licensed under the MIT License. See the LICENSE file for more information.

# Functions

NewInMemoryFactory returns a new in-memory distributed mutex factory.
NewMySQLFactory returns a new distributed mutex factory that uses MySQL as the backend for the locks.
NewRedisFactory builds a new distributed mutex factory that uses Redis as backend to provide distributed mutexes.

# Structs

RedisConfig configuration needed by Redis factory.

# Interfaces

Mutex is an abstraction for a distributed mutex.
MutexFactory is an abstraction for a distributed mutex factory.