utils

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2021 License: Apache-2.0 Imports: 10 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 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

Types

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 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.

Jump to

Keyboard shortcuts

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