lotus/lib/passcall/every.go
Łukasz Magiera c785e59371
feat: curio: storage index gc task (#11884)
* curio storage path gc: lay out the structure

* curio gc: Implement storage metadata gc

* move bored singleton task impl to harmonytask

* curio: run storage gc task on storage node

* make gen
2024-04-16 16:34:48 -05:00

29 lines
584 B
Go

package passcall
import (
"sync"
"time"
)
// Every is a helper function that will call the provided callback
// function at most once every `passEvery` duration. If the function is called
// more frequently than that, it will return nil and not call the callback.
func Every[P, R any](passInterval time.Duration, cb func(P) R) func(P) R {
var lastCall time.Time
var lk sync.Mutex
return func(param P) R {
lk.Lock()
defer lk.Unlock()
if time.Since(lastCall) < passInterval {
return *new(R)
}
defer func() {
lastCall = time.Now()
}()
return cb(param)
}
}