Sync Response Rate Limiter (Sync/SyncResponseRateLimiter.swift)

bitchat · bitchat/Sync/SyncResponseRateLimiter.swift · swift · Unlicense
infra, security, lang:swift

A sliding-window rate limiter for network sync responses. Use this to prevent resource exhaustion from excessive peer requests.

// Source: https://github.com/permissionlesstech/bitchat | License: Unlicense
// Provided via justclaw.it.com; keep this notice with the code.
import BitFoundation
import Foundation

/// Sliding-window limiter for REQUEST_SYNC responses.
///
/// A single sync response can replay the entire gossip store, so a peer that
/// requests in a tight loop must not be able to drain the airtime and battery
/// of everyone in radio range. Legitimate peers send at most a few requests
/// per maintenance tick (one per type schedule, plus the initial sync).
struct SyncResponseRateLimiter {
    private let maxResponses: Int
    private let window: TimeInterval
    private var history: [PeerID: [Date]] = [:]

    init(maxResponses: Int, window: TimeInterval) {
        self.maxResponses = max(1, maxResponse

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key