mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(wecom): correctly retain boundary message during dedupe map rotation
When the dedupe map rotates, the previous logic entirely cleared the map, meaning the message that triggered the rotation was immediately forgotten and could be duplicated immediately. This change seeds the new map with the current message to prevent that. Also adds a defensive nil check.
This commit is contained in:
@@ -14,14 +14,18 @@ func markMessageProcessed(msgMu *sync.RWMutex, processedMsgs *map[string]bool, m
|
||||
msgMu.Lock()
|
||||
defer msgMu.Unlock()
|
||||
|
||||
if *processedMsgs == nil {
|
||||
*processedMsgs = make(map[string]bool)
|
||||
}
|
||||
|
||||
if (*processedMsgs)[msgID] {
|
||||
return false
|
||||
}
|
||||
(*processedMsgs)[msgID] = true
|
||||
|
||||
// Keep existing behavior: when over limit, reset dedupe map entirely.
|
||||
// When over limit, reset dedupe map but keep the current message.
|
||||
if len(*processedMsgs) > maxEntries {
|
||||
*processedMsgs = make(map[string]bool)
|
||||
*processedMsgs = map[string]bool{msgID: true}
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
@@ -60,14 +60,19 @@ func TestMarkMessageProcessed_RotationClearsMapAtBoundary(t *testing.T) {
|
||||
t.Fatalf("expected map size 1 after first insert, got %d", len(processed))
|
||||
}
|
||||
|
||||
// Inserting second unique message exceeds maxEntries and should reset map.
|
||||
// Inserting second unique message exceeds maxEntries and should reset map, but keep the new message.
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-2", 1); !ok {
|
||||
t.Fatalf("second unique message should be accepted")
|
||||
}
|
||||
if len(processed) != 0 {
|
||||
t.Fatalf("expected map to be reset after rotation, got size %d", len(processed))
|
||||
if len(processed) != 1 {
|
||||
t.Fatalf("expected map to retain current message after rotation, got size %d", len(processed))
|
||||
}
|
||||
if processed["msg-2"] {
|
||||
t.Fatalf("expected current message marker to be cleared after rotation")
|
||||
if !processed["msg-2"] {
|
||||
t.Fatalf("expected current message marker to be retained after rotation")
|
||||
}
|
||||
|
||||
// Because msg-2 was retained, an immediate duplicate should be rejected.
|
||||
if ok := markMessageProcessed(&mu, &processed, "msg-2", 1); ok {
|
||||
t.Fatalf("duplicate message immediately after rotation should be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user