redlock

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

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

kv cache types

View Source
const (
	OptDisableGC  = "opt-disable-gc"
	OptGCInterval = "opt-gc-interval"
	OptCacheSize  = "opt-cache-size"
)

kv cache options

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 FreeCache

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

FreeCache is a wrapper of freecache.Cache

func NewFreeCache

func NewFreeCache(opts map[string]interface{}) *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(cacheType string, opts map[string]interface{}) 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(addrs []string) (*RedLock, error)

NewRedLock creates a RedLock

func (*RedLock) Lock

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

Lock acquires a distribute lock

func (*RedLock) SetCache

func (r *RedLock) SetCache(cacheType string, opts map[string]interface{})

SetCache resets cache based on cache type

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(opts map[string]interface{}) *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