mirror of
https://github.com/kopia/kopia.git
synced 2026-01-27 15:58:03 -05:00
* logging: use clock.Now instead of time.Now * timetrack: centralized computing ETAs for various tasks using real clock (time.Now)
27 lines
651 B
Go
27 lines
651 B
Go
package timetrack
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// Throttle throttles UI updates to a specific interval.
|
|
type Throttle int64
|
|
|
|
// ShouldOutput returns true if it's ok to produce output given the for a given time interval.
|
|
func (t *Throttle) ShouldOutput(interval time.Duration) bool {
|
|
nextOutputTimeUnixNano := atomic.LoadInt64((*int64)(t))
|
|
if nowNano := time.Now().UnixNano(); nowNano > nextOutputTimeUnixNano { //nolint:forbidigo
|
|
if atomic.CompareAndSwapInt64((*int64)(t), nextOutputTimeUnixNano, nowNano+interval.Nanoseconds()) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Reset resets the throttle.
|
|
func (t *Throttle) Reset() {
|
|
*t = 0
|
|
}
|