use our own dataloader and simplify batch replaceable requests, removing bugs.

This commit is contained in:
fiatjaf
2025-03-20 19:37:37 -03:00
parent 25838a024e
commit 78dbf9def5
11 changed files with 321 additions and 173 deletions

View File

@@ -2,7 +2,6 @@ package sdk
import (
"slices"
"time"
jsoniter "github.com/json-iterator/go"
)
@@ -20,39 +19,3 @@ func appendUnique[I comparable](arr []I, item ...I) []I {
}
return arr
}
// doThisNotMoreThanOnceAnHour checks if an operation with the given key
// has been performed in the last hour. If not, it returns true and records
// the operation to prevent it from running again within the hour.
func doThisNotMoreThanOnceAnHour(key string) (doItNow bool) {
_dtnmtoahLock.Lock()
defer _dtnmtoahLock.Unlock()
if _dtnmtoah == nil {
// this runs only once for the lifetime of this library and
// starts a long-running process of checking for expired items
// and deleting them from this map every 10 minutes.
_dtnmtoah = make(map[string]time.Time)
go func() {
for {
time.Sleep(time.Minute * 10)
_dtnmtoahLock.Lock()
now := time.Now()
for k, v := range _dtnmtoah {
if v.Before(now) {
delete(_dtnmtoah, k)
}
}
_dtnmtoahLock.Unlock()
}
}()
}
_, hasBeenPerformedInTheLastHour := _dtnmtoah[key]
if hasBeenPerformedInTheLastHour {
return false
}
_dtnmtoah[key] = time.Now()
return true
}