fix: protect publish path from mysql exhaustion

This commit is contained in:
richjohnrambo
2026-09-27 18:35:54 +08:00
parent 36eb76a5e3
commit 3bb6774786
17 changed files with 308 additions and 21 deletions
+26
View File
@@ -3147,6 +3147,29 @@ func (relationsMapper) GetRelationshipContext(tenantID types.TenantID, userID, p
}
func (relationsMapper) CanSendP2P(tenantID types.TenantID, userID, peerID types.Uid) (bool, string, error) {
if userID.IsZero() || peerID.IsZero() || userID == peerID {
return false, "", types.ErrMalformed
}
// A warm relationship cache is sufficient for the publish hot path. Block
// entries are checked first in both directions; friendship and block writes
// keep these sets coherent. Redis errors and cache misses fall back to the
// authoritative database path below.
blockCacheReady := true
if blocked, err := cachedBlockContains(tenantID, userID, peerID); err != nil {
blockCacheReady = false
} else if blocked {
return false, types.RelationshipBlockedByMe, nil
}
if blocked, err := cachedBlockContains(tenantID, peerID, userID); err != nil {
blockCacheReady = false
} else if blocked {
return false, types.RelationshipBlockedByPeer, nil
}
if blockCacheReady {
if friend, err := cachedFriendContains(tenantID, userID, peerID); err == nil && friend {
return true, types.RelationshipFriend, nil
}
}
ctx, err := (relationsMapper{}).GetRelationshipContext(tenantID, userID, peerID, "")
if err != nil {
return false, "", err
@@ -3168,6 +3191,9 @@ func (relationsMapper) BlockUser(tenantID types.TenantID, blockerID, blockedID t
block, err := tenantAdp.TenantUserBlockUpsert(tenantID, blockerID, blockedID, now)
if err == nil {
cacheBlockOneWay(tenantID, blockerID, blockedID)
// A positive friend cache must never outlive a new block. Removing both
// directions also makes a later block-cache eviction fall back to MySQL.
uncacheFriendPair(tenantID, blockerID, blockedID)
}
return block, err
}