Documentation ¶
Index ¶
- func DeepClone(v interface{}) interface{}
- func DeepCopy(dst, src interface{})
- func DispatchReadPK(money float64, amount int, average bool) (pks []float64)
- func RandGroup(p ...uint32) int
- func RandInterval(b1, b2 int32) int32
- func RandIntervalN(b1, b2 int32, n uint32) []int32
- func RandString(len int) string
- type LimiterMap
- func (l *LimiterMap) Add(key interface{}, limit int64)
- func (l *LimiterMap) Clean()
- func (l *LimiterMap) Del(key interface{})
- func (l *LimiterMap) IsLimited(key interface{}, seconds int64) bool
- func (l *LimiterMap) UnSafeDel(key interface{})
- func (l *LimiterMap) UnsafeAdd(key interface{}, limit int64)
- type RateLimiter
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 ¶
DispatchReadPK random || @average money/amount
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
Types ¶
type LimiterMap ¶
var Limiter *LimiterMap
func (*LimiterMap) Add ¶
func (l *LimiterMap) Add(key interface{}, limit int64)
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.