Documentation
¶
Index ¶
- Constants
- func Auth01Validate(auth int64, operation int) bool
- func DeepClone(v interface{}) interface{}
- func DeepCopy(dst, src interface{})
- func DelOpKey(key string)
- func DispatchReadPK(money float64, amount int, average bool) (pks []float64)
- func ExistedCache(key string) bool
- func Exists(path string) bool
- func FormatTime2String(t time.Time) string
- func GetIrisRemoteAddr(ctx *context.Context) string
- func GetMD5Hash(text string) string
- func GetTodayUnix() int64
- func GetTomorrowUnix() int64
- func GraceExit(done chan struct{}, callbacks ...func())
- func InitDsbLocker(nodeId string, rdb redis.UniversalClient, notifies ...func(string)) func()
- func IrisSVExit(done chan struct{}, callbacks ...func())
- func IsDir(path string) bool
- func IsEmail(email string) bool
- func IsFile(path string) bool
- func IsPhone(phoneStr string) bool
- func OpLockTimeout(key string, timeout time.Duration) func()
- func OpLocked(key string) (func(), error)
- func OpLockedWait(key string) func()
- func RandGroup(p ...uint32) int
- func RandInterval(b1, b2 int32) int32
- func RandIntervalN(b1, b2 int32, n uint32) []int32
- func RandItemWeight(rd *rand.Rand, data map[interface{}]int64) (interface{}, error)
- func RandString(len int) string
- func ReplaceKeyword(cfg, value string) string
- func Reverse(s interface{})
- func RunCmd(command string, args ...string) error
- func SignalExit(callback func())
- func SyncObjByStr(objKey string) func()
- func SyncStrWithTimeout(objKey string, duration time.Duration) func()
- type AuthValidator
- type Callback
- type DedupQueue
- type LazyQueue
- func (lazy *LazyQueue) Executed(f func(interface{}, error))
- func (lazy *LazyQueue) Length() int
- func (lazy *LazyQueue) OutOfQueue(key interface{})
- func (lazy *LazyQueue) PushToQueue(obj interface{})
- func (lazy *LazyQueue) PushToQueueWait(obj interface{})
- func (lazy *LazyQueue) Queued(f func(interface{}))
- func (lazy *LazyQueue) Reset()
- func (lazy *LazyQueue) Run()
- type LimiterMap
- func (l *LimiterMap) Add(key interface{}, duration time.Duration)
- func (l *LimiterMap) Clean()
- func (l *LimiterMap) Del(key interface{})
- func (l *LimiterMap) IsLimited(key interface{}, seconds int64) bool
- func (l *LimiterMap) IsV2Limited(key interface{}, duration time.Duration, max int64) (bool, int64)
- func (l *LimiterMap) UnSafeDel(key interface{})
- func (l *LimiterMap) UnsafeAdd(key interface{}, duration time.Duration)
- type RNodeLock
- func (locker *RNodeLock) HandleMsg(msg string)
- func (locker *RNodeLock) Pub(key string)
- func (locker *RNodeLock) RDLockAsync(operation string, lockExp time.Duration) (func(), bool)
- func (locker *RNodeLock) RDLockAuto(operation string) func()
- func (locker *RNodeLock) RDLockWait(operation string) func()
- func (locker *RNodeLock) RDLockWaitHasExp(operation string, lockExp time.Duration) func()
- func (locker *RNodeLock) RDLockWaitHour(operation string) func()
- func (locker *RNodeLock) RDLockWithContext(ctx context.Context, operation string) (func(), bool)
- func (locker *RNodeLock) RDLockWithContextExp(ctx context.Context, operation string, exp time.Duration) (func(), bool)
- func (locker *RNodeLock) Release()
- func (locker *RNodeLock) Sub()
- func (locker *RNodeLock) WatchKeys()
- type RankItem
- type RateLimiter
- type RkPool
- func (rp *RkPool) Clean()
- func (rp *RkPool) GetNO1() *RankItem
- func (rp *RkPool) GetRankData(from, to int) (data []RankItem)
- func (rp *RkPool) IsInPool(item RankItem) bool
- func (rp *RkPool) Queue(item RankItem)
- func (rp *RkPool) Rank()
- func (rp *RkPool) Remove(key interface{})
- func (rp *RkPool) Serve()
- type Waiter
Examples ¶
Constants ¶
const ( OnlineNodes = "dlock:online" //在线节点 NodeIdKey = "dlock:nodeId" //自增ID NodeFileDir = "./runtime" NodeIdFile = "./runtime/node_locker_id_" NotifyEventChannel = "SET_NX:notify_unlock_event" )
Variables ¶
This section is empty.
Functions ¶
func Auth01Validate ¶ added in v1.2.6
Auth01Validate 验证操权限值和操作值
func DeepClone ¶
func DeepClone(v interface{}) interface{}
DeepClone v
Example ¶
package main
import (
"fmt"
"github.com/helloh2o/lucky/utils"
)
func main() {
src := []int{1, 2, 3}
dst := utils.DeepClone(src).([]int)
for _, v := range dst {
fmt.Println(v)
}
}
Output: 1 2 3
func DeepCopy ¶
func DeepCopy(dst, src interface{})
DeepCopy src to dst
Example ¶
package main
import (
"fmt"
"github.com/helloh2o/lucky/utils"
)
func main() {
src := []int{1, 2, 3}
var dst []int
utils.DeepCopy(&dst, &src)
for _, v := range dst {
fmt.Println(v)
}
}
Output: 1 2 3
func DispatchReadPK ¶
DispatchReadPK random || @average money/amount
func FormatTime2String ¶ added in v1.1.7
FormatTime2String 时间to字符串
func GetIrisRemoteAddr ¶ added in v1.1.7
GetIrisRemoteAddr 获取Iris 客户端IP
func GraceExit ¶ added in v1.4.8
func GraceExit(done chan struct{}, callbacks ...func())
GraceExit 优雅退出
func InitDsbLocker ¶ added in v1.4.8
func InitDsbLocker(nodeId string, rdb redis.UniversalClient, notifies ...func(string)) func()
InitDsbLocker 初始化默认分布式锁节点 @nodeId 节点ID @rdb Redis 客户端 @notifies 解锁通知方法
func IrisSVExit ¶ added in v1.4.8
func IrisSVExit(done chan struct{}, callbacks ...func())
IrisSVExit Iris服务优雅退出
func OpLockTimeout ¶ added in v1.4.8
OpLockTimeout 单机锁一个操作字符串可超时
func RandGroup ¶
RandGroup by []unit32
Example ¶
package main
import (
"fmt"
"github.com/helloh2o/lucky/utils"
)
func main() {
i := utils.RandGroup(0, 0, 50, 50)
switch i {
case 2, 3:
fmt.Println("ok")
}
}
Output: ok
func RandInterval ¶
RandInterval b1 to b2
Example ¶
package main
import (
"fmt"
"github.com/helloh2o/lucky/utils"
)
func main() {
v := utils.RandInterval(-1, 1)
switch v {
case -1, 0, 1:
fmt.Println("ok")
}
}
Output: ok
func RandIntervalN ¶
RandIntervalN b1, b2, n
Example ¶
package main
import (
"fmt"
"github.com/helloh2o/lucky/utils"
)
func main() {
r := utils.RandIntervalN(-1, 0, 2)
if r[0] == -1 && r[1] == 0 ||
r[0] == 0 && r[1] == -1 {
fmt.Println("ok")
}
}
Output: ok
func RandItemWeight ¶ added in v1.2.2
RandItemWeight 根据权重随机
func ReplaceKeyword ¶ added in v1.2.3
ReplaceKeyword 替换关键字
func Reverse ¶ added in v1.4.5
func Reverse(s interface{})
Reverse Answers for Go version 1.20 and earlier:
func SignalExit ¶ added in v1.1.7
func SignalExit(callback func())
func SyncObjByStr ¶ added in v1.1.6
func SyncObjByStr(objKey string) func()
SyncObjByStr 锁定一个字符串的同步操作
func SyncStrWithTimeout ¶ added in v1.1.7
SyncStrWithTimeout 锁定一个字符串的同步操作,允许过期
Types ¶
type AuthValidator ¶ added in v1.2.3
func NewAuthValidator ¶ added in v1.2.3
func NewAuthValidator() *AuthValidator
NewAuthValidator 创建一个权限检验器
func (*AuthValidator) AddAuthData ¶ added in v1.2.3
func (au *AuthValidator) AddAuthData(key interface{}, maxAuth int)
AddAuthData 添加对象权限
func (*AuthValidator) GetAuth ¶ added in v1.2.3
func (au *AuthValidator) GetAuth(key interface{}) int
GetAuth 获取对象的权限值
func (*AuthValidator) Validate ¶ added in v1.2.3
func (au *AuthValidator) Validate(key interface{}, operation int) bool
Validate 验证操作是否有权限
type Callback ¶ added in v1.4.8
type Callback[T comparable] func(T)
type DedupQueue ¶ added in v1.4.8
type DedupQueue[T comparable] struct { // contains filtered or unexported fields }
DedupQueue 这是安全的去重排队queue
func NewDedupQueue ¶ added in v1.4.8
func NewDedupQueue[T comparable]( size int, workers int, interval time.Duration, cb Callback[T], ) *DedupQueue[T]
func (*DedupQueue[T]) Enqueue ¶ added in v1.4.8
func (q *DedupQueue[T]) Enqueue(key T) bool
Enqueue 入队,如果 key 已存在返回 false
func (*DedupQueue[T]) Len ¶ added in v1.4.8
func (q *DedupQueue[T]) Len() int64
func (*DedupQueue[T]) Stop ¶ added in v1.4.8
func (q *DedupQueue[T]) Stop()
type LazyQueue ¶ added in v1.1.5
LazyQueue 排队保存,重复的数据只排一次,时效性一般的情况,场景:降低数据库写压力
func NewLazyQueue ¶ added in v1.1.4
func (*LazyQueue) OutOfQueue ¶ added in v1.1.5
func (lazy *LazyQueue) OutOfQueue(key interface{})
OutOfQueue 解除对象慢保存排队
func (*LazyQueue) PushToQueue ¶ added in v1.1.5
func (lazy *LazyQueue) PushToQueue(obj interface{})
PushToQueue 对象加入保存队列
func (*LazyQueue) PushToQueueWait ¶ added in v1.2.2
func (lazy *LazyQueue) PushToQueueWait(obj interface{})
PushToQueueWait 对象同步加入保存队列
type LimiterMap ¶
var Limiter *LimiterMap
func (*LimiterMap) Add ¶
func (l *LimiterMap) Add(key interface{}, duration time.Duration)
func (*LimiterMap) Del ¶
func (l *LimiterMap) Del(key interface{})
func (*LimiterMap) IsLimited ¶
func (l *LimiterMap) IsLimited(key interface{}, seconds int64) bool
func (*LimiterMap) IsV2Limited ¶ added in v1.1.9
func (*LimiterMap) UnSafeDel ¶
func (l *LimiterMap) UnSafeDel(key interface{})
func (*LimiterMap) UnsafeAdd ¶ added in v1.1.2
func (l *LimiterMap) UnsafeAdd(key interface{}, duration time.Duration)
type RNodeLock ¶ added in v1.4.8
type RNodeLock struct {
// 节点ID
Nid string
sync.Mutex
SelfNotify func(string) // 自定义解锁通知
// contains filtered or unexported fields
}
RNodeLock 基于Redis的分布式锁
func (*RNodeLock) RDLockAsync ¶ added in v1.4.8
RDLockAsync 获取锁,不阻塞只返回结果,ok的情况,业务执行defer done()释放锁
func (*RNodeLock) RDLockAuto ¶ added in v1.4.8
RDLockAuto 默认15分钟,自动续期
func (*RNodeLock) RDLockWait ¶ added in v1.4.8
RDLockWait redis 等待分布式锁,直到获取锁,锁不过期 (一般情况下不推荐使用此方法)
func (*RNodeLock) RDLockWaitHasExp ¶ added in v1.4.8
RDLockWaitHasExp redis 等待分布式锁,直到获取锁, 锁自定义过期时间(确保自己的逻辑时间足够)
func (*RNodeLock) RDLockWaitHour ¶ added in v1.4.8
RDLockWaitHour redis 等待分布式锁,直到获取锁,锁一小时过期 (推荐使用,一小时足够完成逻辑执行)
func (*RNodeLock) RDLockWithContext ¶ added in v1.4.8
type RankItem ¶ added in v1.1.5
type RankItem struct {
// 键
Key interface{}
// 原始值
Data interface{}
// 排名值
RankVal int64
}
RankItem 排序项目
type RateLimiter ¶ added in v1.1.3
type RateLimiter struct {
// contains filtered or unexported fields
}
From:: https://github.com/beefsack/go-rate/blob/master/rate.go A RateLimiter limits the rate at which an action can be performed. It applies neither smoothing (like one could achieve in a token bucket system) nor does it offer any conception of warmup, wherein the rate of actions granted are steadily increased until a steady throughput equilibrium is reached.
func New ¶ added in v1.1.3
func New(limit int, interval time.Duration) *RateLimiter
New creates a new rate limiter for the limit and interval.
func (*RateLimiter) Try ¶ added in v1.1.3
func (r *RateLimiter) Try() (ok bool, remaining time.Duration)
Try returns true if under the rate limit, or false if over and the remaining time before the rate limit expires.
func (*RateLimiter) Wait ¶ added in v1.1.3
func (r *RateLimiter) Wait()
Wait blocks if the rate limit has been reached. Wait offers no guarantees of fairness for multiple actors if the allowed rate has been temporarily exhausted.
type RkPool ¶ added in v1.1.5
type RkPool struct {
// contains filtered or unexported fields
}
RankPool 排序池
func NewRKPool ¶ added in v1.1.5
func NewRKPool(name string, size int, rankCycle time.Duration, selfSort func([]RankItem) []RankItem) *RkPool
NewRKPool 创建一个新的排序池
func (*RkPool) GetRankData ¶ added in v1.1.5
GetRankData 获取已排序数据