goRedisson

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2023 License: MIT Imports: 13 Imported by: 0

README

goRedisson

Redisson go implementation

Go codecov Release Go Report Card Go Reference

Description

redis mutex rwmutex go implementation with watchdog

English | 简体中文

Example use:
package main

import (
	"context"
	"log"
	"sync"
	"time"
    
	"github.com/me-cs/goRedisson"
	"github.com/redis/go-redis/v9"
)

func main() {
	// create redis client
	redisDB := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	defer redisDB.Close()

	g := goRedisson.NewGoRedisson(redisDB)

	mutex := g.GetLock("example")
	ctx,cancel:=context.WithTimeout(context.Background(), 1*time.Second)
	defer cancel()
	err := mutex.LockContext(ctx)
	if err != nil {
		log.Print(err)
		return
	}

	//Your business code

	err = mutex.Unlock()
	if err != nil {
		log.Print(err)
		return
	}

	// or you can use a rwlock
	testRwMutex()
	return
}

func testRwMutex() {
	redisDB := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	defer redisDB.Close()

	g := goRedisson.NewGoRedisson(redisDB)
	l := g.GetReadWriteLock("testRwMutest")
	a := 0
	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		defer wg.Done()
		innerWg := sync.WaitGroup{}
		for i := 0; i < 100; i++ {
			innerWg.Add(1)
			go func() {
				defer innerWg.Done()
				ctx,cancel:=context.WithTimeout(context.Background(), 5*time.Second)
				defer cancel()
				err := l.WriteLock().LockContext(ctx)
				if err != nil {
					panic(err)
				}
				a++
				err = l.WriteLock().Unlock()
				if err != nil {
					panic(err)
				}
			}()
		}
		innerWg.Wait()
	}()

	go func() {
		defer wg.Done()
		innerWg := sync.WaitGroup{}
		for i := 0; i < 100; i++ {
			innerWg.Add(1)
			go func() {
				defer innerWg.Done()
				ctx,cancel:=context.WithTimeout(context.Background(), 5*time.Second)
				defer cancel()
				err := l.ReadLock().LockContext(ctx)
				if err != nil {
					panic(err)
				}
				err = l.ReadLock().Unlock()
				if err != nil {
					panic(err)
				}
			}()
		}
		innerWg.Wait()
	}()

	wg.Wait()
	if a != 100 {
		panic(a)
	}
}

Contributing

Contributing is done with commit code. There is no help that is too small! :)

If you wish to contribute to this project, please branch and issue a pull request against master ("GitHub Flow")

Give a Star! ⭐

If you like or are using this project to learn or solve real problems through this project. please give it a star. Thanks!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWatchDogTimeout = 30 * time.Second

DefaultWatchDogTimeout The default watchdog timeout, the watchdog will go every 1/3 of the DefaultWatchDogTimeout to renew the lock held by the current thread.

View Source
var (
	// ErrObtainLockTimeout indicates that Lock cannot be acquired within waitTime
	ErrObtainLockTimeout = errors.New("obtained lock timeout")
)

Functions

This section is empty.

Types

type GoRedisson

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

GoRedisson is a redisson client.

func NewGoRedisson

func NewGoRedisson(redisClient *redis.Client, opts ...OptionFunc) *GoRedisson

NewGoRedisson returns a new GoRedisson instance.

func (*GoRedisson) GetLock

func (g *GoRedisson) GetLock(key string) Lock

GetLock returns a Lock named "key" which can be used to lock and unlock the resource "key". A Lock can be copied after first use, but most of the time it is advisable to keep instances of Lock.

func (*GoRedisson) GetMutex added in v1.1.2

func (g *GoRedisson) GetMutex(key string) Lock

GetMutex returns a Mutex named "key" which can be used to lock and unlock the resource "key". A Mutex can be copied after first use, but most of the time it is advisable to keep instances of Lock. the difference between Mutex and Lock is that Mutex can't be locked by any thread twice.

func (*GoRedisson) GetReadWriteLock

func (g *GoRedisson) GetReadWriteLock(key string) ReadWriteLock

GetReadWriteLock returns a ReadWriteLock named "key" which can be used to lock and unlock the resource "key" when reading or writing. A ReadWriteLock can be copied after first use, but most of the time it is advisable to keep instances of ReadWriteLock.

type Lock

type Lock interface {
	Lock() error
	Unlock() error

	LockContext(context.Context) error
	UnlockContext(context.Context) error
}

A Lock represents an object that can be locked and unlocked.

type OptionFunc

type OptionFunc func(g *GoRedisson)

OptionFunc is a function that can be used to configure a GoRedisson instance.

func WithWatchDogTimeout

func WithWatchDogTimeout(t time.Duration) OptionFunc

WithWatchDogTimeout sets the timeout for the watchdog.

type ReadWriteLock

type ReadWriteLock interface {
	ReadLock() Lock
	WriteLock() Lock
}

ReadWriteLock is a interface for read/write lock

Jump to

Keyboard shortcuts

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