utils

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 IsDir added in v1.1.7

func IsDir(path string) bool

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

func IsFile added in v1.1.7

func IsFile(path string) bool

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

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 RandString

func RandString(len int) string

RandString by len

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 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) 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(key interface{})

PushToQueue 将不重要的对象加入保存队列

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{}, limit int64)

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) UnSafeDel

func (l *LimiterMap) UnSafeDel(key interface{})

func (*LimiterMap) UnsafeAdd added in v1.1.2

func (l *LimiterMap) UnsafeAdd(key interface{}, limit int64)

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 开启间隙自动排序

Jump to

Keyboard shortcuts

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