redlock

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Constants

View Source
const (
	CacheTypeSimple    = "simple"
	CacheTypeFreeCache = "freecache"
)

kv cache types

View Source
const (
	// DefaultRetryCount is the max retry times for lock acquire
	DefaultRetryCount = 10

	// DefaultRetryDelay is upper wait time in millisecond for lock acquire retry
	DefaultRetryDelay = 200

	// ClockDriftFactor is clock drift factor, more information refers to doc
	ClockDriftFactor = 0.01

	// UnlockScript is redis lua script to release a lock
	UnlockScript = `` /* 155-byte string literal not displayed */

)

Variables

View Source
var (
	// ErrLockSingleRedis represents error when acquiring lock on a single redis
	ErrLockSingleRedis = errors.New("set lock on single redis failed")

	// ErrAcquireLock means acquire lock failed after max retry time
	ErrAcquireLock = errors.New("failed to require lock")
)

Functions

This section is empty.

Types

type CacheOption

type CacheOption func(*CacheOptions)

CacheOption alias to the function that can be used to configure CacheOptions

func WithCacheSize

func WithCacheSize(size int) CacheOption

WithCacheSize sets CacheSize of CacheOptions

func WithCacheType

func WithCacheType(tp string) CacheOption

WithCacheType sets CacheType to CacheOptions

func WithDisableGC

func WithDisableGC(disabled bool) CacheOption

WithDisableGC sets DisableGC to CacheOptions

func WithGCInterval

func WithGCInterval(interval time.Duration) CacheOption

WithGCInterval sets GCInterval of CacheOptions

type CacheOptions

type CacheOptions struct {
	CacheType  string
	DisableGC  bool
	GCInterval time.Duration
	CacheSize  int
}

CacheOptions defines optional parameters for configuring kv cache.

type FreeCache

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

FreeCache is a wrapper of freecache.Cache

func NewFreeCache

func NewFreeCache(options *CacheOptions) *FreeCache

NewFreeCache returns a new FreeCache instance

func (*FreeCache) Delete

func (fc *FreeCache) Delete(key string)

Delete implements KVCache.Delete

func (*FreeCache) Get

func (fc *FreeCache) Get(key string) (*LockElem, error)

Get implements KVCache.Get

func (*FreeCache) Set

func (fc *FreeCache) Set(key, val string, expiry int64) (*LockElem, error)

Set implements KVCache.Set

func (*FreeCache) Size

func (fc *FreeCache) Size() int

Size implements KVCache.Size

type KVCache

type KVCache interface {
	// Set sets the key value with its expiry in nanoseconds
	Set(key, val string, expiry int64) (*LockElem, error)

	// Get queries LockElem from given key
	Get(key string) (*LockElem, error)

	// Delete removes the LockElem with given key from storage
	Delete(key string)

	// Size returns element count in kv storage
	Size() int
}

KVCache defines interface for redlock key value storage

func NewCacheImpl

func NewCacheImpl(ctx context.Context, opts ...CacheOption) KVCache

NewCacheImpl returns a KVCache implementation based on given cache type

type LockElem

type LockElem struct {
	Val    string    `json:"val"`
	Expiry int64     `json:"expiry"`
	Ts     time.Time `json:"ts"`
}

LockElem keeps a lock element

type RedClient

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

RedClient holds client to redis

type RedLock

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

RedLock holds the redis lock

func NewRedLock

func NewRedLock(
	ctx context.Context, addrs []string, opts ...CacheOption,
) (*RedLock, error)

NewRedLock creates a RedLock

func (*RedLock) Lock

func (r *RedLock) Lock(ctx context.Context, resource string, ttl time.Duration) (time.Duration, error)

Lock acquires a distribute lock, returns - the remaining valid duration that lock is guaranted - error if acquire lock fails

func (*RedLock) SetRetryCount

func (r *RedLock) SetRetryCount(count int)

SetRetryCount sets acquire lock retry count

func (*RedLock) SetRetryDelay

func (r *RedLock) SetRetryDelay(delay int)

SetRetryDelay sets acquire lock retry max internal in millisecond

func (*RedLock) UnLock

func (r *RedLock) UnLock(ctx context.Context, resource string) error

UnLock releases an acquired lock

type SimpleCache

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

SimpleCache is the most native implementation of KVCache interface

func NewSimpleCache

func NewSimpleCache(ctx context.Context, options *CacheOptions) *SimpleCache

NewSimpleCache creates a new SimpleCache object

func (*SimpleCache) Delete

func (sc *SimpleCache) Delete(key string)

Delete implements KVCache.Delete

func (*SimpleCache) Get

func (sc *SimpleCache) Get(key string) (*LockElem, error)

Get implements KVCache.Get

func (*SimpleCache) Set

func (sc *SimpleCache) Set(key, val string, expiry int64) (*LockElem, error)

Set implements KVCache.Set

func (*SimpleCache) Size

func (sc *SimpleCache) Size() int

Size implements KVCache.Size

Jump to

Keyboard shortcuts

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