initial import
This commit is contained in:
42
core/logx/limitedexecutor.go
Normal file
42
core/logx/limitedexecutor.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"zero/core/syncx"
|
||||
"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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user