@@ -454,9 +454,9 @@ all = \xs pred -> none xs (\x -> not (pred x))
454elem = \xs x -> any xs (\y -> y == x)
454elem = \xs x -> any xs (\y -> y == x)
455findFirst = \xs pred -> headOf (filter xs pred)
455findFirst = \xs pred -> headOf (filter xs pred)
456when = \cond action -> if cond then action else yield {}
456when = \cond action -> if cond then action else yield {}
457−clamp = \lo hi x -> if x < lo then lo else if x > hi then hi else x
458min = \a b -> if a < b then a else b
457min = \a b -> if a < b then a else b
459max = \a b -> if a > b then a else b
458max = \a b -> if a > b then a else b
459+clamp = \lo hi x -> max lo (min hi x)
461-- Dedup: keeps first occurrence (O(n^2)).
461-- Dedup: keeps first occurrence (O(n^2)).
462unique = \items -> fold (\acc x -> if elem acc x then acc else union acc [x]) [] items
462unique = \items -> fold (\acc x -> if elem acc x then acc else union acc [x]) [] items
@@ -490,13 +490,10 @@ headOf = \items -> fold (\acc x -> case acc of
491-- Sort then take head: O(n log n) but no fold-with-Maybe boilerplate.
491-- Sort then take head: O(n log n) but no fold-with-Maybe boilerplate.
492minBy = \items getKey -> headOf (sortBy getKey items)
492minBy = \items getKey -> headOf (sortBy getKey items)
493−maxBy = \items getKey -> headOf (sortBy (\x -> 0 - getKey x) items)
495−-- Drop entries with the oldest key (LRU-style eviction; ties drop together)
494+-- Drop entries with the oldest key (LRU-style eviction; ties drop together).
495+-- O(n^2): keep entries that have something strictly smaller below them.
496−evictOldest = \items getKey ->
496+evictOldest = \items getKey -> filter items (\x -> any items (\y -> getKey y < getKey x))
497− case minBy items getKey of
499− Just {value: oldest} -> filter items (\x -> getKey x != getKey oldest)
501capList = \items maxN getKey ->
498capList = \items maxN getKey ->
502 if count items > maxN then evictOldest items getKey else items
499 if count items > maxN then evictOldest items getKey else items
@@ -761,9 +758,7 @@ retryBackoff = \failures -> min retryBackoffMax (retryBackoffBase * max 1 failur
762shouldRetryServer : Text -> Timestamp -> [ForwardFailure] -> Bool
759shouldRetryServer : Text -> Timestamp -> [ForwardFailure] -> Bool
763shouldRetryServer = \server t failures ->
760shouldRetryServer = \server t failures ->
764− case findFirst failures (\f -> f.server == server) of
765− Nothing {} -> True {}
766− Just {value: f} -> t - f.failedAt >= retryBackoff f.failures
761+ all failures (\f -> f.server != server || t - f.failedAt >= retryBackoff f.failures)
768recordFailure = \server -> do
763recordFailure = \server -> do
@@ -953,8 +948,8 @@ pruneAndRetry = \t -> do
953 atomic do set *lastPrunedAt = t
948 atomic do set *lastPrunedAt = t
954 expiredLocal <- pruneExpired t
949 expiredLocal <- pruneExpired t
955 fork (broadcastOfflineGossip t expiredLocal)
950 fork (broadcastOfflineGossip t expiredLocal)
956− when federationEnabled (fork retryPendingForwards)
957 when federationEnabled (do
951 when federationEnabled (do
952+ fork retryPendingForwards
958 aids <- *ackedIds
953 aids <- *ackedIds
959 let pendingAcks = filter aids (\a -> not (isLocalServer a.originServer))
954 let pendingAcks = filter aids (\a -> not (isLocalServer a.originServer))
960 fork (sendAcksAndClean pendingAcks))
955 fork (sendAcksAndClean pendingAcks))