docstrings for many functions.

This commit is contained in:
fiatjaf
2025-03-04 11:08:31 -03:00
parent a82780e82e
commit 5bfaed2740
22 changed files with 293 additions and 66 deletions

View File

@@ -9,6 +9,8 @@ import (
var json = jsoniter.ConfigFastest
// appendUnique adds items to an array only if they don't already exist in the array.
// Returns the modified array.
func appendUnique[I comparable](arr []I, item ...I) []I {
for _, item := range item {
if slices.Contains(arr, item) {
@@ -19,10 +21,19 @@ 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() {
_dtnmtoah = make(map[string]time.Time)
for {
time.Sleep(time.Minute * 10)
_dtnmtoahLock.Lock()
@@ -37,9 +48,11 @@ func doThisNotMoreThanOnceAnHour(key string) (doItNow bool) {
}()
}
_dtnmtoahLock.Lock()
defer _dtnmtoahLock.Unlock()
_, hasBeenPerformedInTheLastHour := _dtnmtoah[key]
if hasBeenPerformedInTheLastHour {
return false
}
_, exists := _dtnmtoah[key]
return !exists
_dtnmtoah[key] = time.Now()
return true
}