mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-26 21:51:10 +02:00
docstrings for many functions.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user