on master help sign in
Simplify server.knot: inline evictOldest, use existing helpers (hasPrefix, hasMessage, unique)
7687a69iko
server.knot+8−17
@@ -475,7 +475,7 @@ appendCapped = \ref items maxN getKey -> do
475-- Add `server` to the named list ref if absent. Returns the new total.
475-- Add `server` to the named list ref if absent. Returns the new total.
476addServerToList = \listRef server -> atomic do
476addServerToList = \listRef server -> atomic do
477 current <- *listRef
477 current <- *listRef
478− let updated = if elem current server then current else union current [server]
478+ let updated = unique (union current [server])
479 set *listRef = updated
479 set *listRef = updated
480 yield (count updated)
480 yield (count updated)
481
481
@@ -487,14 +487,9 @@ removeServerFromList = \listRef server -> atomic do
487
487
488headOf = \items -> fold (\_ x -> Just {value: x}) Nothing {} (take 1 items)
488headOf = \items -> fold (\_ x -> Just {value: x}) Nothing {} (take 1 items)
489
489
490−-- Sort then take head: O(n log n) but no fold-with-Maybe boilerplate.
491−minBy = \items getKey -> headOf (sortBy getKey items)
492−
493−-- LRU-style eviction: drop the single entry with the smallest key.
490+-- Cap to maxN by dropping the single entry with the smallest key (LRU-style).
494−evictOldest = \items getKey -> drop 1 (sortBy getKey items)
495−
496capList = \items maxN getKey ->
491capList = \items maxN getKey ->
497− if count items > maxN then evictOldest items getKey else items
492+ if count items > maxN then drop 1 (sortBy getKey items) else items
498
493
499-- Integer percentage with zero-denominator guard.
494-- Integer percentage with zero-denominator guard.
500pct = \cur maxN -> if maxN > 0 then cur * 100 / maxN else 0
495pct = \cur maxN -> if maxN > 0 then cur * 100 / maxN else 0
@@ -518,7 +513,7 @@ removeForKey = \pred -> do
518capPerKey = \items pred maxN getKey ->
513capPerKey = \items pred maxN getKey ->
519 let mine = filter items pred in
514 let mine = filter items pred in
520 if count mine >= maxN
515 if count mine >= maxN
521− then union (removeWhere items pred) (evictOldest mine getKey)
516+ then union (removeWhere items pred) (drop 1 (sortBy getKey mine))
522 else items
517 else items
523
518
524-- Shared empty/error shape for /poll. Only a few fields ever vary.
519-- Shared empty/error shape for /poll. Only a few fields ever vary.
@@ -600,7 +595,7 @@ matchIp = \clientIp sessionIp -> sessionIp == "" || clientIp == sessionIp
600
595
601extractToken = \authHeader ->
596extractToken = \authHeader ->
602 let token = drop 7 authHeader in
597 let token = drop 7 authHeader in
603− if take 7 authHeader == "Bearer " && token != "" && not (tooLong token maxTokenLen)
598+ if hasPrefix "Bearer " authHeader && token != "" && not (tooLong token maxTokenLen)
604 then Just {value: token}
599 then Just {value: token}
605 else Nothing {}
600 else Nothing {}
606
601
@@ -666,7 +661,7 @@ generateFederationAuth = \scope requestTimestamp ->
666
661
667-- Verify a federation request: signed (Ed25519 over scope|ts|fromServer|nonce) or callback Bearer token.
662-- Verify a federation request: signed (Ed25519 over scope|ts|fromServer|nonce) or callback Bearer token.
668verifyFederationAuth = \authorization fromServer scope requestTimestamp -> do
663verifyFederationAuth = \authorization fromServer scope requestTimestamp -> do
669− if take 7 authorization == "Signed " then do
664+ if hasPrefix "Signed " authorization then do
670 let payload = drop 7 authorization
665 let payload = drop 7 authorization
671 if payload == "" || tooLong payload (maxSignatureLen + 25) then yield False
666 if payload == "" || tooLong payload (maxSignatureLen + 25) then yield False
672 else do
667 else do
@@ -1211,9 +1206,7 @@ sendMessageCore = \senderKey to encryptedBlob clampedTs effMsgId expiresInMs msg
1211 msgs <- *messages
1206 msgs <- *messages
1212 aids <- *ackedIds
1207 aids <- *ackedIds
1213 dids <- *deliveredIds
1208 dids <- *deliveredIds
1214− let isDup = (effMsgId != "" && any msgs (\m -> m.messageId == effMsgId && m.toKey == to))
1215− || hasMessage effMsgId to aids
1209+ let isDup = hasMessage effMsgId to msgs || hasMessage effMsgId to aids || hasMessage effMsgId to dids
1216− || hasMessage effMsgId to dids
1217 srLim <- chargeRate sendRates senderKey maxSendRatePerKey sendRateWindowMs t
1210 srLim <- chargeRate sendRates senderKey maxSendRatePerKey sendRateWindowMs t
1218 prLim <- chargeRate pairRates (senderKey ++ "|" ++ to) maxPairRatePerPair pairRateWindowMs t
1211 prLim <- chargeRate pairRates (senderKey ++ "|" ++ to) maxPairRatePerPair pairRateWindowMs t
1219 rrLim <- chargeRate recvRates to maxRecvRatePerKey recvRateWindowMs t
1212 rrLim <- chargeRate recvRates to maxRecvRatePerKey recvRateWindowMs t
@@ -1471,9 +1464,7 @@ storeFederated = \toKey encryptedBlob timestamp originServer messageId expiresAt
1471 msgs <- *messages
1464 msgs <- *messages
1472 aids <- *ackedIds
1465 aids <- *ackedIds
1473 dids <- *deliveredIds
1466 dids <- *deliveredIds
1474− if any msgs (\m -> m.messageId == messageId && m.toKey == toKey)
1475− || hasMessage messageId toKey aids
1467+ if hasMessage messageId toKey msgs || hasMessage messageId toKey aids || hasMessage messageId toKey dids
1476− || hasMessage messageId toKey dids
1477 then yield ""
1468 then yield ""
1478 else if count msgs >= maxMessages then yield "global_full"
1469 else if count msgs >= maxMessages then yield "global_full"
1479 else if countWhere msgs (\m -> m.toKey == toKey) >= maxMessagesPerRecipient then yield "mailbox_full"
1470 else if countWhere msgs (\m -> m.toKey == toKey) >= maxMessagesPerRecipient then yield "mailbox_full"