mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
fix: let a resubscribing central recover from the announce rate limit (#1669)
* Document the subscription backoff ceiling Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate * Let subscription announcements recover after sustained retries Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate * Test subscription limiter recovery and admission bounds Split the existing PR change into a smaller reviewable piece. Original change: Pin subscription recovery boundary and steady-state rate
This commit is contained in:
1 parent
e09d84fa18
commit
202e13cddf
3 files changed
+73
-1
No files matched your search
@@ -41,8 +41,18 @@ struct BLESubscriptionAnnounceLimiter {
|
||||
existing.currentBackoffSeconds * TransportConfig.bleSubscriptionRateLimitBackoffFactor,
|
||||
TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds
|
||||
)
|
||||
// `lastAnnounceTime` deliberately keeps the time of the last *allowed*
|
||||
// announce. It is the reference for both the backoff and
|
||||
// `pruneStaleEntries`, so refreshing it on a rejection restarts the
|
||||
// tracking window on every attempt — a central resubscribing faster
|
||||
// than the maximum backoff could then never reach the window and
|
||||
// stayed suppressed for as long as it kept trying.
|
||||
// 9cd955a intentionally reset blocked attempts after the remediation
|
||||
// in 99896bc; 193cfdc extracted the limiter without that rationale.
|
||||
// Keeping the allowed timestamp preserves backoff while permitting
|
||||
// recovery during a continuous retry stream.
|
||||
states[centralID] = State(
|
||||
lastAnnounceTime: now,
|
||||
lastAnnounceTime: existing.lastAnnounceTime,
|
||||
attemptCount: newAttemptCount,
|
||||
currentBackoffSeconds: newBackoff
|
||||
)
|
||||
|
||||
@@ -301,6 +301,8 @@ enum TransportConfig {
|
||||
// Prevents rapid enumeration attacks by rate-limiting announce responses
|
||||
static let bleSubscriptionRateLimitMinSeconds: TimeInterval = 2.0 // Minimum interval between announces per central
|
||||
static let bleSubscriptionRateLimitBackoffFactor: Double = 2.0 // Exponential backoff multiplier
|
||||
// Keep the maximum backoff below the tracking window: otherwise pruning
|
||||
// resets the budget before the capped backoff can elapse.
|
||||
static let bleSubscriptionRateLimitMaxBackoffSeconds: TimeInterval = 30.0 // Maximum backoff period
|
||||
static let bleSubscriptionRateLimitWindowSeconds: TimeInterval = 60.0 // Window for tracking subscription attempts
|
||||
static let bleSubscriptionRateLimitMaxAttempts: Int = 5 // Max attempts before extended cooldown
|
||||
|
||||
@@ -58,4 +58,64 @@ struct BLESubscriptionAnnounceLimiterTests {
|
||||
#expect(limiter.decision(for: freshCentralID, now: afterWindow) == .allowed)
|
||||
#expect(limiter.trackedCentralCount == 1)
|
||||
}
|
||||
|
||||
@Test("a central that keeps resubscribing recovers once the backoff elapses")
|
||||
func flappingCentralRecoversAfterBackoff() {
|
||||
// A legitimate central whose BLE link flaps resubscribes faster than the
|
||||
// maximum backoff. Every rejection used to refresh `lastAnnounceTime`,
|
||||
// which is the reference for both the backoff and `pruneStaleEntries`,
|
||||
// so the elapsed time never grew: the central stayed suppressed for as
|
||||
// long as it kept trying. It must instead be re-admitted once the
|
||||
// capped backoff has passed since its last *allowed* announce.
|
||||
var limiter = BLESubscriptionAnnounceLimiter()
|
||||
let centralID = "central-flapping"
|
||||
let start = Date(timeIntervalSince1970: 1_000)
|
||||
|
||||
#expect(limiter.decision(for: centralID, now: start) == .allowed)
|
||||
|
||||
let maxBackoff = TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds
|
||||
var admittedAt: Double?
|
||||
for step in stride(from: 1.0, through: maxBackoff * 2, by: 1.0) {
|
||||
if limiter.decision(for: centralID, now: start.addingTimeInterval(step)) == .allowed {
|
||||
admittedAt = step
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
#expect(admittedAt == maxBackoff, "re-admission must occur exactly at the capped backoff")
|
||||
}
|
||||
|
||||
@Test("a hammering central is still held off for the whole backoff")
|
||||
func flappingCentralIsNotAdmittedEarly() {
|
||||
// The counterpart: relaxing the reference timestamp must not let a
|
||||
// hammering central back in before the capped backoff has elapsed.
|
||||
var limiter = BLESubscriptionAnnounceLimiter()
|
||||
let centralID = "central-flapping"
|
||||
let start = Date(timeIntervalSince1970: 2_000)
|
||||
let maxBackoff = TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds
|
||||
|
||||
#expect(limiter.decision(for: centralID, now: start) == .allowed)
|
||||
|
||||
for step in stride(from: 1.0, to: maxBackoff, by: 1.0) {
|
||||
let decision = limiter.decision(for: centralID, now: start.addingTimeInterval(step))
|
||||
#expect(decision != .allowed, "admitted early at +\(step)s")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("continuous retries admit exactly once per capped backoff")
|
||||
func continuousRetriesPreserveSteadyStateRate() {
|
||||
var limiter = BLESubscriptionAnnounceLimiter()
|
||||
let start = Date(timeIntervalSince1970: 3_000)
|
||||
var admissions: [Int] = []
|
||||
|
||||
#expect(TransportConfig.bleSubscriptionRateLimitMaxBackoffSeconds
|
||||
< TransportConfig.bleSubscriptionRateLimitWindowSeconds)
|
||||
for second in 0..<90 {
|
||||
if limiter.decision(for: "central-flapping", now: start.addingTimeInterval(Double(second))) == .allowed {
|
||||
admissions.append(second)
|
||||
}
|
||||
}
|
||||
#expect(admissions == [0, 30, 60])
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user