go_redislock

package module
v0.0.0-...-53a0ca7 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: MIT Imports: 8 Imported by: 0

README

go-redislock

a distributed redis lock usable in a production environment implemented in Go

How To Use

  • Pull codes

    go get github.com/hedon954/go-redislock
    
    import (
    	...
    	redislock "github.com/hedon954/go-redislock"
    	...
    )
    
  • Init Client

    yourRedisClient := ...
    c := redislock.NewCliengt(yourRedisClient)
    
  • Prepare arguments according to your real business needs and call Lock() or TryLock()

    c.Lock(...)
    c.TryLock(...)
    
  • If the concurrency is high, you can use the 'SingleFlightLock()' method to optimize the performance of distributed locks

    c.SingleLightLock(...)
    

Implementation Cores

  • core: SETNX
  • refresh expiration
  • retry while outtime
  • singleflight

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLockFailed  = errors.New("lock failed")
	ErrLockNotHold = errors.New("not hold current lock")
)

Functions

This section is empty.

Types

type Client

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

Client is a client held redis client

func NewClient

func NewClient(c redis.Cmdable) *Client

NewClient creates a new client

func (*Client) Lock

func (c *Client) Lock(ctx context.Context, key string, expiration, timeout time.Duration, retry RetryStrategy) (*Lock, error)

Lock locks

func (*Client) SingleFlightLock

func (c *Client) SingleFlightLock(ctx context.Context, key string, expiration, timeout time.Duration, retry RetryStrategy) (*Lock, error)

SingleFlightLock uses singleFlight to reduce stress in high concurrency situations

func (*Client) TryLock

func (c *Client) TryLock(ctx context.Context, key string, expiration time.Duration) (*Lock, error)

TryLock trys to hold a lock

type FixIntervalRetry

type FixIntervalRetry struct {
	Interval time.Duration // retry interval
	Max      int           // max retry times
	// contains filtered or unexported fields
}

FixIntervalRetry is a fix interval retry strategy

func (*FixIntervalRetry) Next

func (f *FixIntervalRetry) Next() (time.Duration, bool)

type Lock

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

Lock is a lock created after client locks successfully

func (*Lock) AutoRefresh

func (l *Lock) AutoRefresh(interval time.Duration, timeout time.Duration) error

AutoRefresh always refreshes lock's expiration automatically

func (*Lock) Refresh

func (l *Lock) Refresh(ctx context.Context) error

Refresh refreshes lock's expiration

func (*Lock) UnLock

func (l *Lock) UnLock(ctx context.Context) error

UnLock unlocks

type RetryStrategy

type RetryStrategy interface {

	// Next
	//	first return means the interval of the next retry
	//	second return means need to retry or not
	Next() (time.Duration, bool)
}

RetryStrategy defines retry strategy

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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