Skip to content

Repository files navigation

RWMutex

build-img pkg-img coverage-img

Distributed rw locking implementation using Redis with auto extended lock expiration. RWMutex can work in write prefer mode, when readers do not acquire lock if writer has declared intention to lock.

Example

package main

import (
	"context"
	"fmt"
	"log"
    
	"github.com/redis/redis/v9"
	"github.com/itcomusic/redismutex"
)

func main() {
    // connect to redis
    rc := redis.NewClient(&redis.Options{
        Network: "tcp",
        Addr:    "127.0.0.1:6379",
    })
    defer rc.Close()

    // init mutex
    mx := redismutex.NewMutex(rc, "mutex_name")

    // lock, optionally add key and lock_key will be "mutex_name:key"
    lock, ok := mx.Lock(redismutex.WithKey("key")) // also available RLock, TryLock, TryRLock
    if !ok {
        log.Fatalln("could not obtain lock, disconnected from redis?")
    }
    defer lock.Unlock()

    // lock implements interface context.Context
    handler := func(ctx context.Context) { 
        select {
        case <-ctx.Done():
            fmt.Println("lock expired")
			
        default:
            fmt.Println("lock active")
        }
    }

    fmt.Printf("lock %q! \n", lock.Key())
    handler(lock) 
}

License

MIT License

About

Distributed locking implementation using Redis

Topics

Resources

Stars

Watchers

Forks

Releases

Used by

Contributors

Languages