fix(ip-sync): scope the flat IP identity guard to polled rows

This commit is contained in:
Hamed Sanaei
2026-12-02 18:59:08 +03:30
parent 596f807ed7
commit c2831ca52f
+9 -5
View File
@@ -160,7 +160,7 @@ func mergeInboundClientIps(db *gorm.DB, reporter *model.Node, incomingIps []mode
// check_client_ip_job rewrites this blob too; an unconditional Update loses
// whichever writer commits second and its remote IPs with it (#6587).
err := mergeExistingClientIps(tx, current, incomingEntries, cutoff)
err := mergeExistingClientIps(tx, current, incomingEntries, cutoff, reporter != nil)
// The observation belongs to the email in the report. A concurrent
// delete or rename can remove it while the poll waits for the row.
if reporter != nil && errors.Is(err, gorm.ErrRecordNotFound) {
@@ -199,8 +199,12 @@ func CasUpdateInboundClientIps(tx *gorm.DB, id int, expectedIps, newIps string)
}
// Seed the first CAS from the preload; only a competing write requires a re-read.
func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incoming []model.ClientIpEntry, cutoff int64) error {
email := current.ClientEmail
func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incoming []model.ClientIpEntry, cutoff int64, polled bool) error {
if polled {
// Keep the observed email in every CAS attempt even if the ID is
// still present after a concurrent rename.
tx = tx.Where("client_email = ?", current.ClientEmail).Session(&gorm.Session{})
}
for attempt := range ClientIpCasRetries {
var oldEntries []model.ClientIpEntry
if current.Ips != "" {
@@ -211,7 +215,7 @@ func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incomi
if current.Ips == mergedStr {
return nil
}
ok, err := CasUpdateInboundClientIps(tx.Where("client_email = ?", email), current.Id, current.Ips, mergedStr)
ok, err := CasUpdateInboundClientIps(tx, current.Id, current.Ips, mergedStr)
if err != nil {
return err
}
@@ -220,7 +224,7 @@ func mergeExistingClientIps(tx *gorm.DB, current *model.InboundClientIps, incomi
return nil
}
if attempt+1 < ClientIpCasRetries {
if err := tx.Where("id = ? AND client_email = ?", current.Id, email).First(current).Error; err != nil {
if err := tx.Where("id = ?", current.Id).First(current).Error; err != nil {
return err
}
}