carlos / platform Public

Burst notices: drop a departed peer's notice at dispatch (#491)

A follow-up on #491: a peer that left the edges files, stopped
advertising notices or lost its dial address was pruned only on the next
trip, so a notice already queued for it, or waiting for a budget slot,
was still sent once the slot freed, and its worker lived on until then
or its 10-minute idle. The worker now checks the peer against the
current home map before taking each slot, prunes every departed peer,
and drops the notice as gone without spending a slot. New test
TestBurstNoticeDepartedPeerStopsAtDispatch, watched to fail without it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4d004aaf31ceba1a12ba5dac30f1d9b6978ca503 parent 5524bb2
2 files changed, +73 −0
  • internal/edge/burst_notice.go +24 −0
  • internal/edge/burst_notice_test.go +49 −0
diff --git a/internal/edge/burst_notice.go b/internal/edge/burst_notice.go
index a5a518d75..cb95d65db 100644
--- a/internal/edge/burst_notice.go
+++ b/internal/edge/burst_notice.go
@@ -385,6 +385,25 @@ func (e *Edge) runBurstPeer(n *burstNotices, p *burstPeer) {
}
}
+// burstPeerCurrent reports whether p is still one of this box's notice
+// peers in the home map as it stands now, pruning every peer that is not.
+// A box that left the edges files, stopped advertising notices or lost its
+// dial address is otherwise pruned only on the next trip, so a notice
+// queued for it, or waiting for a slot in the budget, would still be sent
+// to it (#491 follow-up). Checked before every slot is taken, so a dropped
+// notice never spends one.
+func (e *Edge) burstPeerCurrent(n *burstNotices, p *burstPeer) bool {
+ m := e.homes.Load()
+ if m == nil {
+ return false
+ }
+ snap := m.snapshot()
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ n.pruneLocked(n.targetsLocked(snap, e.box, m.privateDial))
+ return n.peers[p.box] == p
+}
+
// retire removes an idle peer, unless a notice arrived for it meanwhile.
func (n *burstNotices) retire(p *burstPeer) bool {
n.mu.Lock()
@@ -405,6 +424,11 @@ func (n *burstNotices) retire(p *burstPeer) bool {
func (e *Edge) dispatchBurstNotice(n *burstNotices, p *burstPeer, nt burstNotice) {
var at time.Time
for {
+ if !e.burstPeerCurrent(n, p) {
+ n.count(func(s *burstNoticeStats) { s.Gone++ })
+ e.logThrottled("burst-notice-gone", fmt.Sprintf("probe-burst notice to %s dropped: the box left the edges files", p.box))
+ return
+ }
now := e.burstNow()
if now.Sub(nt.trip) > burstNoticeMaxAge || !nt.until.After(now) {
n.count(func(s *burstNoticeStats) { s.Stale++ })
diff --git a/internal/edge/burst_notice_test.go b/internal/edge/burst_notice_test.go
index a57226d55..452b48524 100644
--- a/internal/edge/burst_notice_test.go
+++ b/internal/edge/burst_notice_test.go
@@ -553,6 +553,55 @@ func TestBurstNoticeOnlyToAdvertisingPeers(t *testing.T) {
}
}
+// #491 follow-up: a peer that leaves while a notice for it waits for a slot
+// in the budget is dropped at dispatch, not sent once the slot frees, and
+// its worker stops then rather than on the next trip. A peer that stayed
+// still gets its notice.
+func TestBurstNoticeDepartedPeerStopsAtDispatch(t *testing.T) {
+ s := newNoticeScene(t, true)
+ a := s.add(t, "annie", "127.0.0.11", "shop.example.com", "bdk", "shop", true)
+ b := s.add(t, "betty", "127.0.0.12", "blog.example.com", "bab", "blog", true)
+ c := s.add(t, "carol", "127.0.0.13", "docs.example.com", "bab", "docs", true)
+ s.publish(t)
+ stats := func() burstNoticeStats { return a.edge().notices.stats() }
+ peers := func() int {
+ n := a.edge().notices
+ n.mu.Lock()
+ defer n.mu.Unlock()
+ return len(n.peers)
+ }
+
+ // 60 trips 50 s in use the whole budget; one more 105 s in waits for the
+ // slots that free at 110 s, one notice per peer.
+ s.clock.advance(50 * time.Second)
+ for i := range 60 {
+ s.trip(t, a, noticeClient(i))
+ }
+ waitFor(t, 10*time.Second, "120 notices sent", func() bool { return stats().Sent == 120 })
+ s.clock.advance(55 * time.Second)
+ s.trip(t, a, noticeClient(60))
+ time.Sleep(300 * time.Millisecond)
+ if st := stats(); st.Sent != 120 || st.Gone != 0 || peers() != 2 {
+ t.Fatalf("counts %+v, %d peers while two notices wait for a slot", st, peers())
+ }
+
+ // Betty stops advertising notices while hers waits.
+ b.file.advertise = false
+ s.publish(t)
+ s.clock.advance(5*time.Second + 100*time.Millisecond)
+ waitFor(t, 5*time.Second, "carol's notice sent and betty's dropped", func() bool {
+ st := stats()
+ return st.Sent == 121 && st.Gone == 1
+ })
+ if n := len(b.arrivals()); n != 60 {
+ t.Fatalf("betty saw %d notices, want the 60 sent before she left", n)
+ }
+ if n := len(c.arrivals()); n != 61 {
+ t.Fatalf("carol saw %d notices, want 61", n)
+ }
+ waitFor(t, 5*time.Second, "betty's worker gone", func() bool { return peers() == 1 })
+}
+
// R3-2: the budget is taken at dispatch, over any 60 s and not per
// calendar minute, and a notice that cannot be sent within 10 s of its
// trip is dropped and counted.