on master help sign in
Simplify server: use min/max composition, collapse case patterns to predicates
commit8b43efd11af943ef0efbddd1deb9da58be9d1c40 authoriko <mail@iko.soy> dateMon 27 Apr 2026, 10:08 (6 months ago) parent52c50f5
1 file changed+6−11
server.knot+6−11
server.knot+6−11
@@ -454,9 +454,9 @@ all = \xs pred -> none xs (\x -> not (pred x))
454454elem = \xs x -> any xs (\y -> y == x)
455455findFirst = \xs pred -> headOf (filter xs pred)
456456when = \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
458457min = \a b -> if a < b then a else b
459458max = \a b -> if a > b then a else b
459+clamp = \lo hi x -> max lo (min hi x)
460460
461461-- Dedup: keeps first occurrence (O(n^2)).
462462unique = \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
490490
491491-- Sort then take head: O(n log n) but no fold-with-Maybe boilerplate.
492492minBy = \items getKey -> headOf (sortBy getKey items)
493−maxBy = \items getKey -> headOf (sortBy (\x -> 0 - getKey x) items)
494493
495−-- Drop entries with the oldest key (LRU-style eviction; ties drop together)
496−evictOldest = \items getKey ->
497− case minBy items getKey of
498− Nothing {} -> items
499− Just {value: oldest} -> filter items (\x -> getKey x != getKey oldest)
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 -> filter items (\x -> any items (\y -> getKey y < getKey x))
500497
501498capList = \items maxN getKey ->
502499 if count items > maxN then evictOldest items getKey else items
@@ -761,9 +758,7 @@ retryBackoff = \failures -> min retryBackoffMax (retryBackoffBase * max 1 failur
761758
762759shouldRetryServer : Text -> Timestamp -> [ForwardFailure] -> Bool
763760shouldRetryServer = \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)
767762
768763recordFailure = \server -> do
769764 t <- now
@@ -953,8 +948,8 @@ pruneAndRetry = \t -> do
953948 atomic do set *lastPrunedAt = t
954949 expiredLocal <- pruneExpired t
955950 fork (broadcastOfflineGossip t expiredLocal)
956− when federationEnabled (fork retryPendingForwards)
957951 when federationEnabled (do
952+ fork retryPendingForwards
958953 aids <- *ackedIds
959954 let pendingAcks = filter aids (\a -> not (isLocalServer a.originServer))
960955 fork (sendAcksAndClean pendingAcks))