2 Star 0 Fork 336

hxchjm / go-zero

forked from kevwan / go-zero 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
limitedexecutor.go 838 Bytes
一键复制 编辑 原始数据 按行查看 历史
kevwan 提交于 2020-08-08 16:40 . update package reference
package logx
import (
"sync/atomic"
"time"
"github.com/tal-tech/go-zero/core/syncx"
"github.com/tal-tech/go-zero/core/timex"
)
type limitedExecutor struct {
threshold time.Duration
lastTime *syncx.AtomicDuration
discarded uint32
}
func newLimitedExecutor(milliseconds int) *limitedExecutor {
return &limitedExecutor{
threshold: time.Duration(milliseconds) * time.Millisecond,
lastTime: syncx.NewAtomicDuration(),
}
}
func (le *limitedExecutor) logOrDiscard(execute func()) {
if le == nil || le.threshold <= 0 {
execute()
return
}
now := timex.Now()
if now-le.lastTime.Load() <= le.threshold {
atomic.AddUint32(&le.discarded, 1)
} else {
le.lastTime.Set(now)
discarded := atomic.SwapUint32(&le.discarded, 0)
if discarded > 0 {
Errorf("Discarded %d error messages", discarded)
}
execute()
}
}
Go
1
https://gitee.com/hxchjm/go-zero.git
git@gitee.com:hxchjm/go-zero.git
hxchjm
go-zero
go-zero
3eb88c4e4bf1

搜索帮助