From ca2a1d63d7f1d9266a61496834516ecea3394f47 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Tue, 10 Nov 2026 11:50:49 +0100 Subject: [PATCH] fix(store): count only hard bounces of the last 30 days - Add members.bounce_at (unix time of the last counted bounce) with an additive migration; existing counts get 0, so they are treated as old. - RecordHardBounce restarts the count at 1 when the last bounce is older than BounceWindow (30 days); a disabled member stays disabled. - Tests for the window boundary, the restart and the migration of an old database. --- internal/store/bounce_test.go | 76 +++++++++++++++++++++++++++++++++++ internal/store/store.go | 21 +++++++++- 2 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 internal/store/bounce_test.go diff --git a/internal/store/bounce_test.go b/internal/store/bounce_test.go new file mode 100644 index 0000000..69212a8 --- /dev/null +++ b/internal/store/bounce_test.go @@ -0,0 +1,76 @@ +package store + +import ( + "testing" + "time" +) + +func TestHardBouncesCountOnlyWithinTheWindow(t *testing.T) { + s := newTestStore(t) + now := time.Unix(1_900_000_000, 0) + s.Now = func() time.Time { return now } + l, _ := s.CreateList("dev", "example.org", "Dev") + s.AddMember(l.ID, "a@x.com", "") + ms, _ := s.Members(l.ID) + id := ms[0].ID + count := func() int { + t.Helper() + m, err := s.MemberByID(id) + if err != nil { + t.Fatal(err) + } + return m.BounceCount + } + + s.RecordHardBounce(id, 3) + now = now.Add(BounceWindow) // exactly the window: still recent + s.RecordHardBounce(id, 3) + if n := count(); n != 2 { + t.Fatalf("a bounce within the window adds up: %d", n) + } + now = now.Add(BounceWindow + time.Second) // the last bounce is now too old + if disabled, err := s.RecordHardBounce(id, 3); disabled || err != nil { + t.Fatalf("an old count must not disable: %v %v", disabled, err) + } + if n := count(); n != 1 { + t.Fatalf("a bounce after a quiet window restarts the count: %d", n) + } + now = now.Add(time.Hour) + s.RecordHardBounce(id, 3) + if disabled, _ := s.RecordHardBounce(id, 3); !disabled { + t.Fatal("three recent bounces disable") + } + // A disabled member stays disabled, even when a late bounce restarts the count. + now = now.Add(2 * BounceWindow) + if disabled, _ := s.RecordHardBounce(id, 3); !disabled { + t.Fatal("only re-adding re-enables a member") + } +} + +func TestMigrationAddsTheBounceTimeColumn(t *testing.T) { + path := t.TempDir() + "/old.db" + s, _ := Open(path) + l, _ := s.CreateList("dev", "example.org", "Dev") + s.AddMember(l.ID, "a@x.com", "") + s.db.Exec(`UPDATE members SET bounce_count=2`) + if _, err := s.db.Exec(`ALTER TABLE members DROP COLUMN bounce_at`); err != nil { + t.Fatal(err) + } + s.Close() + s2, err := Open(path) + if err != nil { + t.Fatalf("migrate: %v", err) + } + defer s2.Close() + ms, err := s2.Members(l.ID) + if err != nil || len(ms) != 1 || ms[0].BounceCount != 2 { + t.Fatalf("members survive the migration: %+v %v", ms, err) + } + // An old count has no date: it is older than the window, so the next bounce restarts it. + if disabled, err := s2.RecordHardBounce(ms[0].ID, 3); disabled || err != nil { + t.Fatalf("%v %v", disabled, err) + } + if m, _ := s2.MemberByID(ms[0].ID); m.BounceCount != 1 { + t.Fatalf("count: %d", m.BounceCount) + } +} diff --git a/internal/store/store.go b/internal/store/store.go index 5abaedf..0683169 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -58,6 +58,9 @@ var ( // BounceThreshold is the number of hard bounces after which a member is disabled. const BounceThreshold = 3 +// BounceWindow is how long a hard bounce counts: a bounce after a quiet window restarts the count. +const BounceWindow = 30 * 24 * time.Hour + const ( pendingTTL = 72 * time.Hour pendingRateLimit = time.Hour @@ -146,6 +149,7 @@ CREATE TABLE IF NOT EXISTS members ( display_name TEXT NOT NULL DEFAULT '', disabled INTEGER NOT NULL DEFAULT 0, bounce_count INTEGER NOT NULL DEFAULT 0, + bounce_at INTEGER NOT NULL DEFAULT 0, UNIQUE(list_id, address) ); CREATE TABLE IF NOT EXISTS held ( @@ -278,6 +282,15 @@ func migrate(db *sql.DB) error { } } } + if ok, err := hasColumn(db, "members", "id"); err != nil { + return err + } else if ok { + if has, _ := hasColumn(db, "members", "bounce_at"); !has { + if _, err := db.Exec(`ALTER TABLE members ADD COLUMN bounce_at INTEGER NOT NULL DEFAULT 0`); err != nil { + return err + } + } + } if ok, err := hasColumn(db, "outbox", "msg_id"); err != nil { return err } else if ok { @@ -545,9 +558,13 @@ func (s *Store) DeleteMember(id int64) error { } // RecordHardBounce counts a hard bounce and disables the member once the count -// reaches threshold. It reports whether the member is now disabled. +// reaches threshold. The count holds recent bounces only: when the last one is +// older than BounceWindow it restarts at 1. It reports whether the member is now disabled. func (s *Store) RecordHardBounce(memberID int64, threshold int) (bool, error) { - if err := affected(s.db.Exec(`UPDATE members SET bounce_count=bounce_count+1 WHERE id=?`, memberID)); err != nil { + now := s.Now() + if err := affected(s.db.Exec(`UPDATE members SET + bounce_count=CASE WHEN bounce_at=?`, memberID, threshold); err != nil {