on master help sign in
Simplify server: extract chargeRate helper, remove redundant empty-check branches, inline intermediates
17842b5iko
server.knot+24−34
@@ -742,12 +742,16 @@ checkIpRateGate = \ip t ->
742 if ip == "" then yield False
742 if ip == "" then yield False
743 else checkPubkeyRateGate ipRates ip maxIpRatePerIp ipRateWindowMs t
743 else checkPubkeyRateGate ipRates ip maxIpRatePerIp ipRateWindowMs t
744
744
745+-- Non-atomic version: caller must wrap in `atomic do`.
745−checkPubkeyRateGate = \rateRef pubkey maxRate windowMs t -> atomic do
746+chargeRate = \rateRef key maxRate windowMs t -> do
746 rates <- *rateRef
747 rates <- *rateRef
747− let r = checkRate rates pubkey maxRate windowMs 5000 t
748+ let r = checkRate rates key maxRate windowMs 5000 t
748 set *rateRef = r.entries
749 set *rateRef = r.entries
749 yield r.limited
750 yield r.limited
750
751
752+checkPubkeyRateGate = \rateRef pubkey maxRate windowMs t -> atomic
753+ (chargeRate rateRef pubkey maxRate windowMs t)
754+
751checkServerRateGate = \server t ->
755checkServerRateGate = \server t ->
752 checkPubkeyRateGate federationRates server maxFederationRatePerServer federationRateWindowMs t
756 checkPubkeyRateGate federationRates server maxFederationRatePerServer federationRateWindowMs t
753
757
@@ -1069,11 +1073,9 @@ handleRevoke = \targetToken authorization clientIp respond -> do
1069 (\caller -> do
1073 (\caller -> do
1070 result <- atomic do
1074 result <- atomic do
1071 ses <- *sessions
1075 ses <- *sessions
1072− let found = any ses (\s -> s.pubkey == caller && s.token == targetToken)
1076+ let pred = \s -> s.pubkey == caller && s.token == targetToken
1073− if not found then yield False
1074− else do
1075− set *sessions = removeWhere ses (\s -> s.pubkey == caller && s.token == targetToken)
1077+ set *sessions = removeWhere ses pred
1076− yield True
1078+ yield (any ses pred)
1077 yield (respond {ok: result, authorized: True {}}))
1079 yield (respond {ok: result, authorized: True {}}))
1078
1080
1079handleLogout = \authorization clientIp respond -> do
1081handleLogout = \authorization clientIp respond -> do
@@ -1240,21 +1242,14 @@ sendMessageCore = \senderKey to encryptedBlob clampedTs effMsgId expiresInMs msg
1240 let isDup = (effMsgId != "" && any msgs (\m -> m.messageId == effMsgId && m.toKey == to))
1242 let isDup = (effMsgId != "" && any msgs (\m -> m.messageId == effMsgId && m.toKey == to))
1241 || hasMessage effMsgId to aids
1243 || hasMessage effMsgId to aids
1242 || hasMessage effMsgId to dids
1244 || hasMessage effMsgId to dids
1243− sendRates <- *sendRates
1244− let sr = checkRate sendRates senderKey maxSendRatePerKey sendRateWindowMs 5000 t
1245+ srLim <- chargeRate sendRates senderKey maxSendRatePerKey sendRateWindowMs t
1245− set *sendRates = sr.entries
1246− pairRates <- *pairRates
1247− let pr = checkRate pairRates (senderKey ++ "|" ++ to) maxPairRatePerPair pairRateWindowMs 5000 t
1246+ prLim <- chargeRate pairRates (senderKey ++ "|" ++ to) maxPairRatePerPair pairRateWindowMs t
1248− set *pairRates = pr.entries
1249− recvRates <- *recvRates
1250− let rr = checkRate recvRates to maxRecvRatePerKey recvRateWindowMs 5000 t
1247+ rrLim <- chargeRate recvRates to maxRecvRatePerKey recvRateWindowMs t
1251− set *recvRates = rr.entries
1252− let recipientCount = countWhere msgs (\m -> m.toKey == to)
1253 if senderKey == to then yield {status: MsRejected {}, remoteServers: []}
1248 if senderKey == to then yield {status: MsRejected {}, remoteServers: []}
1254 else if isDup then yield {status: MsDuplicate {}, remoteServers: []}
1249 else if isDup then yield {status: MsDuplicate {}, remoteServers: []}
1255− else if sr.limited || pr.limited || rr.limited then yield {status: MsRateLimited {}, remoteServers: []}
1250+ else if srLim || prLim || rrLim then yield {status: MsRateLimited {}, remoteServers: []}
1256 else if count msgs >= maxMessages then yield {status: MsRejected {}, remoteServers: []}
1251 else if count msgs >= maxMessages then yield {status: MsRejected {}, remoteServers: []}
1257− else if recipientCount >= maxMessagesPerRecipient then yield {status: MsQueued {}, remoteServers: []}
1252+ else if countWhere msgs (\m -> m.toKey == to) >= maxMessagesPerRecipient then yield {status: MsQueued {}, remoteServers: []}
1258 else do
1253 else do
1259 pres <- *presence
1254 pres <- *presence
1260 seq <- *seqCounter
1255 seq <- *seqCounter
@@ -1366,12 +1361,10 @@ handleAckMessages = \messageIds authorization clientIp respond -> do
1366 result <- atomic do
1361 result <- atomic do
1367 msgs <- *messages
1362 msgs <- *messages
1368 let toAck = filter msgs matches
1363 let toAck = filter msgs matches
1369− if count toAck == 0 then yield {ackedCount: 0, pendingAcks: []}
1370− else do
1371− set *messages = removeWhere msgs matches
1364+ set *messages = removeWhere msgs matches
1372− pruneRef forwards (\f -> not (matches f))
1365+ pruneRef forwards (\f -> not (matches f))
1373− ackedFed <- recordAcksAndDeliveries toAck t
1366+ ackedFed <- recordAcksAndDeliveries toAck t
1374− yield {ackedCount: count toAck, pendingAcks: ackedFed}
1367+ yield {ackedCount: count toAck, pendingAcks: ackedFed}
1375 fork (when federationEnabled (sendAcksAndClean result.pendingAcks))
1368 fork (when federationEnabled (sendAcksAndClean result.pendingAcks))
1376 yield (respond {ok: True {}, authorized: True {}, ackedCount: result.ackedCount, error: ""}))
1369 yield (respond {ok: True {}, authorized: True {}, ackedCount: result.ackedCount, error: ""}))
1377
1370
@@ -1402,13 +1395,11 @@ handleDeleteMessages = \messageIds authorization clientIp respond -> do
1402 result <- atomic do
1395 result <- atomic do
1403 msgs <- *messages
1396 msgs <- *messages
1404 let toDel = filter msgs matches
1397 let toDel = filter msgs matches
1405− if count toDel == 0 then yield {deletedCount: 0, pendingAcks: []}
1406− else do
1407− set *messages = removeWhere msgs matches
1398+ set *messages = removeWhere msgs matches
1408− pruneRef forwards (\f -> not (matches f))
1399+ pruneRef forwards (\f -> not (matches f))
1409− let ackedFed = mapWhere toDel (\m -> m.originServer != "") (\m -> mkAckedEntry m t)
1400+ let ackedFed = mapWhere toDel (\m -> m.originServer != "") (\m -> mkAckedEntry m t)
1410− appendCapped ackedIds ackedFed maxAckedIds (\a -> a.ackedAt)
1401+ appendCapped ackedIds ackedFed maxAckedIds (\a -> a.ackedAt)
1411− yield {deletedCount: count toDel, pendingAcks: ackedFed}
1402+ yield {deletedCount: count toDel, pendingAcks: ackedFed}
1412 fork (when federationEnabled (sendAcksAndClean result.pendingAcks))
1403 fork (when federationEnabled (sendAcksAndClean result.pendingAcks))
1413 yield (respond {ok: True {}, authorized: True {}, deletedCount: result.deletedCount, error: ""}))
1404 yield (respond {ok: True {}, authorized: True {}, deletedCount: result.deletedCount, error: ""}))
1414
1405
@@ -1508,8 +1499,7 @@ handleVerifyToken = \token scope clientIp respond -> do
1508 else do
1499 else do
1509 found <- atomic do
1500 found <- atomic do
1510 pending <- *pendingVerifications
1501 pending <- *pendingVerifications
1511− let candidates = filter pending (\p -> p.scope == scope && t - p.createdAt < verificationTokenExpiry)
1502+ let match = any pending (\p -> p.scope == scope && p.token == token && t - p.createdAt < verificationTokenExpiry)
1512− let match = any candidates (\p -> p.token == token)
1513 when match (set *pendingVerifications = filter pending (\p -> p.scope != scope || p.token != token))
1503 when match (set *pendingVerifications = filter pending (\p -> p.scope != scope || p.token != token))
1514 yield match
1504 yield match
1515 yield (respond {ok: found})
1505 yield (respond {ok: found})