utils

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Auth01Validate added in v1.2.6

func Auth01Validate(auth int64, operation int) bool

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

func DispatchReadPK(money float64, amount int, average bool) (pks []float64)

DispatchReadPK random || @average money/amount

func ExistedCache added in v1.2.5

func ExistedCache(key string) bool

是否存在redis key

func Exists added in v1.1.7

func Exists(path string) bool

Exists 判断所给路径文件/文件夹是否存在

func FormatTime2String added in v1.1.7

func FormatTime2String(t time.Time) string

FormatTime2String 时间to字符串

func GetIrisRemoteAddr added in v1.1.7

func GetIrisRemoteAddr(ctx *context.Context) string

GetIrisRemoteAddr 获取Iris 客户端IP

func GetMD5Hash added in v1.1.7

func GetMD5Hash(text string) string

GetMD5Hash get md5 of text

func GetTodayUnix added in v1.2.5

func GetTodayUnix() int64

GetTodayUnix 获取今日凌晨时间戳s

func GetTomorrowUnix added in v1.2.5

func GetTomorrowUnix() int64

GetTomorrowUnix 获取明日凌晨时间戳s

func IsDir added in v1.1.7

func IsDir(path string) bool

IsDir 判断所给路径是否为文件夹

func IsEmail added in v1.2.6

func IsEmail(email string) bool

func IsFile added in v1.1.7

func IsFile(path string) bool

IsFile 判断所给路径是否为文件

func IsPhone added in v1.2.6

func IsPhone(phoneStr string) bool

func RDLockOp added in v1.2.0

func RDLockOp(operation string) (release func(), got bool, wait chan struct{})

RDLockOp redis 分布式锁, 足够的操作逻辑时间 release释放,got 是否获取锁,wait在线等待,直到获取锁

func RDLockOpTimeout added in v1.2.0

func RDLockOpTimeout(operation string, timeout time.Duration) (release func(), got bool, wait chan struct{})

RDLockOpTimeout redis 分布式锁, 时间不够可能引发并发同步问题 release释放,got 是否获取锁,wait在线等待,直到获取锁

func RDLockOpWait added in v1.2.7

func RDLockOpWait(operation string) func()

RDLockOpWait redis 等待分布式锁,直到获取锁

func RDLockOpWaitTimeout added in v1.3.8

func RDLockOpWaitTimeout(operation string, timeout time.Duration) func()

RDLockOpWaitTimeout 自定义超时操作

func RDLockOpWithContext added in v1.3.8

func RDLockOpWithContext(ctx context.Context, operation string) (func(), bool)

RDLockOpWithContext redis 上下文获取锁

func RandGroup

func RandGroup(p ...uint32) int

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

func RandInterval(b1, b2 int32) int32

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

func RandIntervalN(b1, b2 int32, n uint32) []int32

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

func RandItemWeight(rd *rand.Rand, data map[interface{}]int64) (interface{}, error)

RandItemWeight 根据权重随机

func RandString

func RandString(len int) string

RandString by len

func ReplaceKeyword added in v1.2.3

func ReplaceKeyword(cfg, value string) string

ReplaceKeyword 替换关键字

func RunCmd added in v1.1.7

func RunCmd(command string, args ...string) error

RunCmd 调用命令

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

func SyncStrWithTimeout(objKey string, duration time.Duration) func()

SyncStrWithTimeout 锁定一个字符串的同步操作,允许过期

Types

type AuthValidator added in v1.2.3

type AuthValidator struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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 LazyQueue added in v1.1.5

type LazyQueue struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

LazyQueue 排队保存,重复的数据只排一次,时效性一般的情况,场景:降低数据库写压力

func NewLazyQueue added in v1.1.4

func NewLazyQueue(qps, size int, cf func(interface{}) error) (*LazyQueue, error)

func (*LazyQueue) Executed added in v1.3.8

func (lazy *LazyQueue) Executed(f func(interface{}, error))

Executed 已执行

func (*LazyQueue) Length added in v1.2.2

func (lazy *LazyQueue) Length() int

Length 队列长度

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 对象同步加入保存队列

func (*LazyQueue) Queued added in v1.3.8

func (lazy *LazyQueue) Queued(f func(interface{}))

Queued 已排队

func (*LazyQueue) Reset added in v1.2.2

func (lazy *LazyQueue) Reset()

Reset 重置队列

func (*LazyQueue) Run added in v1.1.5

func (lazy *LazyQueue) Run()

Run 启动LazySave 并返回错误

type LimiterMap

type LimiterMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}
var Limiter *LimiterMap

func (*LimiterMap) Add

func (l *LimiterMap) Add(key interface{}, duration time.Duration)

func (*LimiterMap) Clean

func (l *LimiterMap) Clean()

Clean self clean

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 (l *LimiterMap) IsV2Limited(key interface{}, duration time.Duration, max int64) (bool, int64)

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 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) Clean added in v1.1.5

func (rp *RkPool) Clean()

Clean 清空排序池

func (*RkPool) GetNO1 added in v1.1.5

func (rp *RkPool) GetNO1() *RankItem

GetNO1 获取第一个用户

func (*RkPool) GetRankData added in v1.1.5

func (rp *RkPool) GetRankData(from, to int) (data []RankItem)

GetRankData 获取已排序数据

func (*RkPool) IsInPool added in v1.1.5

func (rp *RkPool) IsInPool(item RankItem) bool

IsInPool 是否在池子中

func (*RkPool) Queue added in v1.1.5

func (rp *RkPool) Queue(item RankItem)

Queue 排队到池

func (*RkPool) Rank added in v1.1.5

func (rp *RkPool) Rank()

进行排序

func (*RkPool) Remove added in v1.1.5

func (rp *RkPool) Remove(key interface{})

Remove from pool

func (*RkPool) Serve added in v1.1.5

func (rp *RkPool) Serve()

Serve 开启间隙自动排序

type Waiter added in v1.2.0

type Waiter struct {
	Id string
	sync.Mutex

	OnNotify func(string)
	// contains filtered or unexported fields
}

等待者

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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