lockmanager

package module
v0.0.0-...-31505dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 5 Imported by: 0

README

Lock manager

A simple package for running functions using key-based distributed locks backed by Redis.

Installation

go get github.com/kajjagtenberg/lockmanager

Usage

Use the following example to get started.

package main

import (
	"context"
	"log"
	"time"

	"github.com/kajjagtenberg/lockmanager"
	"github.com/go-redis/redis/v8"
)

func main() {
	rds := redis.NewClient(&redis.Options{})

	manager, err := lockmanager.NewLockManager(rds)
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()

	if err := manager.Lock(ctx, "test", func() error {
		log.Println("Running under lock")

		time.Sleep(time.Second * 20)

		return nil
	}); err != nil {
		log.Fatal(err)
	}

	if err := manager.Lock(ctx, "test", func() error {
		log.Println("Running under lock")

		time.Sleep(time.Second * 20)

		return nil
	}); err != nil {
		log.Fatal(err)
	}
}

Authors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrResourceLocked = fmt.Errorf("resource already locked")
)

Functions

This section is empty.

Types

type LockManager

type LockManager struct {
	// contains filtered or unexported fields
}

func NewLockManager

func NewLockManager(rds *redis.Client) (*LockManager, error)

func (*LockManager) Lock

func (m *LockManager) Lock(ctx context.Context, key string, fn func() error) error

func (*LockManager) Prefix

func (m *LockManager) Prefix() string

func (*LockManager) SetPrefix

func (m *LockManager) SetPrefix(prefix string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL