mirror of
https://github.com/kopia/kopia.git
synced 2026-04-04 22:33:20 -04:00
Almost all were easy to replace, except ones exposed via JSON which have been left as-is. The linter has a cool behavior where it flags attempts to pass `atomic.Int32` for example by value , which is always a mistake, say as an argument to `fmt.Sprintf()`
29 lines
638 B
Go
29 lines
638 B
Go
package timetrack
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// Throttle throttles UI updates to a specific interval.
|
|
type Throttle struct {
|
|
v atomic.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 := t.v.Load()
|
|
if nowNano := time.Now().UnixNano(); nowNano > nextOutputTimeUnixNano { //nolint:forbidigo
|
|
if t.v.CompareAndSwap(nextOutputTimeUnixNano, nowNano+interval.Nanoseconds()) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Reset resets the throttle.
|
|
func (t *Throttle) Reset() {
|
|
t.v.Store(0)
|
|
}
|