Documentation ¶
Index ¶
- Constants
- func IsNil(err error) bool
- func SetLogger(log gutils.LoggerItf)
- type GetItemBlockingOptionFunc
- type Mutex
- type MutexOptionFunc
- func WithMutexBlockingLock(blocking bool) MutexOptionFunc
- func WithMutexClientID(clientID string) MutexOptionFunc
- func WithMutexLogger(logger *gutils.LoggerType) MutexOptionFunc
- func WithMutexRefreshInterval(interval time.Duration) MutexOptionFunc
- func WithMutexSpinInterval(interval time.Duration) MutexOptionFunc
- func WithMutexTTL(ttl time.Duration) MutexOptionFunc
- type Rank
- type RdbIntCmdItf
- type RdbItf
- type RdbScanCmdItf
- type RdbSliceCmdItf
- type RdbStatusCmdItf
- type RdbStringCmdItf
- type Semaphore
- type SemaphoreOptionFunc
- func WithSemaphoreBlockingLock(blocking bool) SemaphoreOptionFunc
- func WithSemaphoreClientID(clientID string) SemaphoreOptionFunc
- func WithSemaphoreLogger(logger *gutils.LoggerType) SemaphoreOptionFunc
- func WithSemaphoreRefreshInterval(interval time.Duration) SemaphoreOptionFunc
- func WithSemaphoreSpinInterval(interval time.Duration) SemaphoreOptionFunc
- func WithSemaphoreTTL(ttl time.Duration) SemaphoreOptionFunc
- type Utils
- func (u *Utils) GetItem(ctx context.Context, key string) (string, error)
- func (u *Utils) GetItemBlocking(ctx context.Context, dbkey string, opts ...GetItemBlockingOptionFunc) (data string, err error)
- func (u *Utils) GetItemWithPrefix(ctx context.Context, keyPrefix string) (map[string]string, error)
- func (u *Utils) LPopKeysBlocking(ctx context.Context, keys ...string) (key, val string, err error)
- func (u *Utils) NewMutex(lockName string, opts ...MutexOptionFunc) (Mutex, error)
- func (u *Utils) NewRank(name string, maxSnapshotID int) (Rank, error)
- func (u *Utils) NewSemaphore(lockName string, limit int, opts ...SemaphoreOptionFunc) (Semaphore, error)
- func (u *Utils) RPush(ctx context.Context, key string, payloads ...interface{}) (err error)
- func (u *Utils) SetItem(ctx context.Context, key, val string, exp time.Duration) error
- func (u *Utils) WithGetItemBlockingDel(del bool) GetItemBlockingOptionFunc
Examples ¶
Constants ¶
const ( // KeyExpImmortal nolimit KeyExpImmortal = 0 // KeyExp1Day 1day KeyExp1Day = 24 * time.Hour // ScanCount how many items return by each scan ScanCount = 10 )
Redis Key expirations
const (
// DefaultKeyPrefix default prefix of key in redis
DefaultKeyPrefix = "/rtils/"
)
const (
WaitDBKeyDuration = 1 * time.Second
)
Pop time
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GetItemBlockingOptionFunc ¶
type GetItemBlockingOptionFunc func(*getItemBlockingOption) error
GetItemBlockingOptionFunc optional arguments for GetItemBlocking
type Mutex ¶ added in v1.0.3
type Mutex interface { // Lock acquire a recursive lock // // if succeed acquired lock, // * locked == true // * lockCtx is context of lock, this context will be set to done when lock is expired Lock(ctx context.Context) (locked bool, lockCtx context.Context, err error) // Unlock release lock Unlock(ctx context.Context) error }
mutexType distributed mutex
Redis keys:
`/rtils/sync/mutex/<lock_id>/<client_id>`
Implementations:
- generate client id(cid)
- set if not exists by `SETNX` with ttl: lock_name -> cid
- if succeeded set, auto refresh lock's ttl
type MutexOptionFunc ¶ added in v1.0.3
type MutexOptionFunc func(*mutex) error
MutexOptionFunc options for mutex
func WithMutexBlockingLock ¶ added in v1.0.3
func WithMutexBlockingLock(blocking bool) MutexOptionFunc
WithMutexBlockingLock set whether blocking lock
func WithMutexClientID ¶ added in v1.0.3
func WithMutexClientID(clientID string) MutexOptionFunc
WithMutexClientID set client id
func WithMutexLogger ¶ added in v1.0.3
func WithMutexLogger(logger *gutils.LoggerType) MutexOptionFunc
WithMutexLogger set lock's expiration
func WithMutexRefreshInterval ¶ added in v1.0.3
func WithMutexRefreshInterval(interval time.Duration) MutexOptionFunc
WithMutexRefreshInterval set lock refreshing interval
func WithMutexSpinInterval ¶ added in v1.0.3
func WithMutexSpinInterval(interval time.Duration) MutexOptionFunc
WithMutexSpinInterval set lock spin interval
func WithMutexTTL ¶ added in v1.0.3
func WithMutexTTL(ttl time.Duration) MutexOptionFunc
WithMutexTTL set lock's expiration
type Rank ¶ added in v1.1.0
type Rank interface { // Set set/update someone's score and snapshotID Set(ctx context.Context, key string, score, snapshotID int) error // Del delete a key Del(ctx context.Context, key string) error // List get top N scores List(ctx context.Context, limit uint) ([]redis.Z, error) // Get get someone's score and snapshotID Get(ctx context.Context, key string) (snapshotID int, err error) }
Rank Use the ordered set of redis to implement dynamic ranking.
When a user's score changes, a snapshot is taken of the user's current information and status to get a snapshot ID, and then the user's key, score, and snapshot ID are stored in the ordered set. member is the key, score is the (score * 100000 + snapshot ID).
Example ¶
rdb := redis.NewClient(&redis.Options{}) rtils := NewRedisUtils(rdb) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() r, _ := rtils.NewRank("test", 10000) _ = r.Set(ctx, "user-1", 100, 1) _ = r.Set(ctx, "user-2", 101, 1) _ = r.Set(ctx, "user-3", 511, 1) _ = r.Set(ctx, "user-4", 201, 1) _ = r.Set(ctx, "user-5", 301, 1) ret, _ := r.List(ctx, 1) fmt.Println(ret[0].Member)
Output: user-3
type RdbIntCmdItf ¶
RdbIntCmdItf string result
type RdbItf ¶
type RdbItf interface { Get(string) RdbStringCmdItf LPop(string) RdbStringCmdItf LLen(string) RdbIntCmdItf LTrim(string, int64, int64) RdbStatusCmdItf MGet(...string) RdbSliceCmdItf Del(...string) RdbIntCmdItf Set(string, interface{}, time.Duration) RdbStatusCmdItf Scan(uint64, string, int64) RdbScanCmdItf RPush(string, ...interface{}) RdbIntCmdItf }
RdbItf redis SDK
type RdbScanCmdItf ¶
RdbScanCmdItf string result
type RdbSliceCmdItf ¶
RdbSliceCmdItf string result
type RdbStatusCmdItf ¶
RdbStatusCmdItf string result
type RdbStringCmdItf ¶
RdbStringCmdItf string result
type Semaphore ¶ added in v1.0.3
type Semaphore interface { // Lock acquire a recursive lock // // if succeed acquired lock, // * locked == true // * lockCtx is context of lock, this context will be set to done when lock is expired Lock(ctx context.Context) (locked bool, lockCtx context.Context, err error) // Unlock release lock Unlock(ctx context.Context) (err error) }
semaphore distributed fair semaphore
Redis keys:
`/rtils/sync/sema/<lock_id>/` * cids/: client_id -> ts, all clients * owners/: client_id -> counter, all clients acquired lock * counter:
you can specified client_id by `WithSemaphoreClientID`. will auto generate client_id by UUID4 if not set.
Implementations:
- generate client id(cid)
- delete all expired clients in `cids`, by `ZREMRANGEBYSCORE cids -inf <now - ttl>`
- delete all expired clients in `owners`, by `ZINTERSTORE owners 2 owners cids WEIGHTS 1 0`
- increment semaphore's counter
- add cid:counter to `owners`, get rank (smaller is better). 5-1. delete from `owners` if the rank is over the limit of semaphore
- add cid:timestamp to `cids`
type SemaphoreOptionFunc ¶ added in v1.0.3
type SemaphoreOptionFunc func(*semaphore) error
SemaphoreOptionFunc options for semaphore
func WithSemaphoreBlockingLock ¶ added in v1.0.3
func WithSemaphoreBlockingLock(blocking bool) SemaphoreOptionFunc
WithSemaphoreBlockingLock set whether blocking lock
func WithSemaphoreClientID ¶ added in v1.0.3
func WithSemaphoreClientID(clientID string) SemaphoreOptionFunc
WithSemaphoreClientID set client id
func WithSemaphoreLogger ¶ added in v1.0.3
func WithSemaphoreLogger(logger *gutils.LoggerType) SemaphoreOptionFunc
WithSemaphoreLogger set lock's expiration
func WithSemaphoreRefreshInterval ¶ added in v1.0.3
func WithSemaphoreRefreshInterval(interval time.Duration) SemaphoreOptionFunc
WithSemaphoreRefreshInterval set lock refreshing interval
func WithSemaphoreSpinInterval ¶ added in v1.0.3
func WithSemaphoreSpinInterval(interval time.Duration) SemaphoreOptionFunc
WithSemaphoreSpinInterval set lock spin interval
func WithSemaphoreTTL ¶ added in v1.0.3
func WithSemaphoreTTL(ttl time.Duration) SemaphoreOptionFunc
WithSemaphoreTTL set lock's expiration
type Utils ¶ added in v1.0.3
type Utils struct { *redis.Client // contains filtered or unexported fields }
Utils utils enhancemant for redis
func NewRedisUtils ¶ added in v1.0.3
func NewRedisUtils(rdb *redis.Client) *Utils
NewRedisUtils wrap redis client with utils
func (*Utils) GetItemBlocking ¶ added in v1.0.3
func (u *Utils) GetItemBlocking(ctx context.Context, dbkey string, opts ...GetItemBlockingOptionFunc) (data string, err error)
GetItemBlocking get key blocking
will delete key after get in default.
func (*Utils) GetItemWithPrefix ¶ added in v1.0.3
GetItemWithPrefix get item with prefix, return `map[key]: val`
func (*Utils) LPopKeysBlocking ¶ added in v1.0.3
LPopKeysBlocking LPop from mutiple keys
func (*Utils) NewMutex ¶ added in v1.0.3
func (u *Utils) NewMutex(lockName string, opts ...MutexOptionFunc) (Mutex, error)
NewMutex new mutex
func (*Utils) NewSemaphore ¶ added in v1.0.3
func (u *Utils) NewSemaphore(lockName string, limit int, opts ...SemaphoreOptionFunc) (Semaphore, error)
NewSemaphore new semaphore
func (*Utils) RPush ¶ added in v1.0.3
RPush rpush keys and truncate its length
default max length is 100
func (*Utils) WithGetItemBlockingDel ¶ added in v1.0.3
func (u *Utils) WithGetItemBlockingDel(del bool) GetItemBlockingOptionFunc
WithGetItemBlockingDel delete after get