You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
820 B
38 lines
820 B
package logx |
|
|
|
import ( |
|
"git.diulo.com/mogfee/kit/core/syncx" |
|
"git.diulo.com/mogfee/kit/core/timex" |
|
"sync/atomic" |
|
"time" |
|
) |
|
|
|
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 (l *limitedExecutor) logOrDiscard(execute func()) { |
|
if l == nil || l.threshold <= 0 { |
|
execute() |
|
return |
|
} |
|
now := timex.Now() |
|
if now-l.lastTime.Load() <= l.threshold { |
|
atomic.AddUint32(&l.discarded, 1) |
|
} else { |
|
l.lastTime.Set(now) |
|
discarded := atomic.SwapUint32(&l.discarded, 0) |
|
if discarded > 0 { |
|
Errorf("Discarded %d error messages", discarded) |
|
} |
|
execute() |
|
} |
|
}
|
|
|