fix(server): coalesce platform evict fanout

This commit is contained in:
richjohnrambo
2026-10-08 00:23:12 +08:00
parent e30b70a3a7
commit f24133f5de
3 changed files with 145 additions and 19 deletions
+66
View File
@@ -260,6 +260,61 @@ func TestRealtimeSessionControlStaleEvictDropped(t *testing.T) {
}
}
func TestRealtimePlatformEvictFanoutCoalescesToLatestSession(t *testing.T) {
oldPublish := publishRealtimeFanout
t.Cleanup(func() {
publishRealtimeFanout = oldPublish
resetPlatformEvictFanoutForTest()
})
resetPlatformEvictFanoutForTest()
type call struct {
target string
skipSid string
dedupKey string
}
calls := make(chan call, 3)
publishRealtimeFanout = func(_ string, _ types.TenantID, kind, targetType, target, skipSid, dedupKey string, _ any) error {
if kind == realtimeKindSessionControl && targetType == realtimeTargetPlatf {
calls <- call{target: target, skipSid: skipSid, dedupKey: dedupKey}
}
return nil
}
tenantID := types.TenantID(1)
uid := types.Uid(1001)
publishRealtimeEvictUserPlatform(tenantID, uid, "web", "replaced_by_new_login", "sid-old")
publishRealtimeEvictUserPlatform(tenantID, uid, "web", "replaced_by_new_login", "sid-new")
publishRealtimeEvictUserPlatform(tenantID, uid, "web", "replaced_by_new_login", "sid-final")
select {
case got := <-calls:
t.Fatalf("evict fanout published before debounce: %#v", got)
case <-time.After(platformEvictFanoutDebounce / 2):
}
select {
case got := <-calls:
if got.target != uid.UserId()+":web" {
t.Fatalf("target = %q", got.target)
}
if got.skipSid != "sid-final" {
t.Fatalf("skipSid = %q, want latest", got.skipSid)
}
if got.dedupKey != "evict:platform:"+uid.UserId()+":web:sid-final" {
t.Fatalf("dedupKey = %q", got.dedupKey)
}
case <-time.After(2 * platformEvictFanoutDebounce):
t.Fatal("timed out waiting for coalesced evict fanout")
}
select {
case got := <-calls:
t.Fatalf("expected one coalesced fanout, got extra %#v", got)
default:
}
}
func TestRealtimeP2PTopicFallbackRewritesVisibleInfoTopic(t *testing.T) {
oldStore := globals.sessionStore
oldHub := globals.hub
@@ -314,6 +369,17 @@ func TestRealtimeP2PTopicFallbackRewritesVisibleInfoTopic(t *testing.T) {
}
}
func resetPlatformEvictFanoutForTest() {
platformEvictFanout.Lock()
defer platformEvictFanout.Unlock()
for key, pending := range platformEvictFanout.pending {
if pending.timer != nil {
pending.timer.Stop()
}
delete(platformEvictFanout.pending, key)
}
}
func TestRealtimeCallEventRoutesToLocalTopic(t *testing.T) {
oldHub := globals.hub
defer func() { globals.hub = oldHub }()