Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"base_utils.go",
"limiter.go",
],
importmap = "go-common/app/service/main/upcredit/mathutil",
importpath = "go-common/app/service/main/upcredit/mathutil",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["limiter_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)

View File

@@ -0,0 +1,45 @@
package mathutil
//EPSILON very small
var EPSILON float32 = 0.00000001
//FloatEquals float equal
func FloatEquals(a, b float32) bool {
if (a-b) < EPSILON && (b-a) < EPSILON {
return true
}
return false
}
//EPSILON64 very small
var EPSILON64 = 0.00000001
//Float64Equals float equal
func Float64Equals(a, b float64) bool {
if (a-b) < EPSILON64 && (b-a) < EPSILON64 {
return true
}
return false
}
//Min min
func Min(a, b int) int {
if a < b {
return a
}
if b < a {
return b
}
return a
}
//Max max
func Max(a, b int) int {
if a > b {
return a
}
if b > a {
return b
}
return a
}

View File

@@ -0,0 +1,32 @@
package mathutil
import "time"
//Limiter speed limiter
type Limiter struct {
Rate float64 // 每秒多少个
token chan time.Time
timer *time.Ticker
}
//Token get token
func (l *Limiter) Token() (c <-chan time.Time) {
return l.token
}
func (l *Limiter) putToken() {
for t := range l.timer.C {
l.token <- t
}
}
//NewLimiter create new limiter
func NewLimiter(rate float64) *Limiter {
var l = &Limiter{
Rate: rate,
token: make(chan time.Time, 1),
timer: time.NewTicker(time.Duration(1.0 / rate * float64(time.Second))),
}
go l.putToken()
return l
}

View File

@@ -0,0 +1,46 @@
package mathutil
import (
. "github.com/smartystreets/goconvey/convey"
"math"
"testing"
"time"
)
func Test_limiter(t *testing.T) {
Convey("limit interval", t, func() {
var rate = 100.0
var limit = NewLimiter(rate)
var interval = 1.0 / rate
var last time.Time
for i := 0; i < 100; i++ {
var t = <-limit.Token()
if !last.IsZero() {
var diff = t.Sub(last)
So(math.Abs(diff.Seconds()-interval), ShouldBeLessThanOrEqualTo, 0.002)
}
last = t
}
})
Convey("limit count", t, func() {
var rate = 100.0
var seconds = 10.0
var limit = NewLimiter(rate)
var expect = rate * seconds
var timer = time.NewTimer(time.Duration(float64(time.Second) * seconds))
var total = 0
var run = true
for run {
select {
case <-timer.C:
run = false
default:
<-limit.Token()
total++
}
}
So(math.Abs(float64(total)-expect), ShouldBeLessThanOrEqualTo, rate*0.01)
})
}