You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
internal class RateLimiter {
private var timestamps: [Date?]
private var index: Int = 0
private let maxCallsInclusive: Int
let maxCalls: Int
let period: TimeInterval
init(maxCalls: Int, period: TimeInterval) {
self.maxCalls = maxCalls
self.maxCallsInclusive = self.maxCalls + 1
self.period = period
self.timestamps = Array(repeating: nil, count: maxCallsInclusive)
}
func shouldProceed() -> Bool {
let now = Date()
let oldestIndex = (index + 1) % maxCallsInclusive
let oldestTimestamp = timestamps[oldestIndex]
// Check if the oldest timestamp is outside the rate limiting period or if it's nil
if let oldestTimestamp = oldestTimestamp, now.timeIntervalSince(oldestTimestamp) <= period {
return false
} else {
timestamps[index] = now
index = oldestIndex
return true
}
}
}
Some alternatives here:
Make it an actor, so the shouldProceed signature changes to async
Use atomics in all properties, maybe is too much
Use a dispatchqueue as class property and use it in the whole shouldProceed method making it sync
Use a lock(.recursive)
The text was updated successfully, but these errors were encountered:
https://github.com/RevenueCat/purchases-ios/blame/72b06f69438eff747fe0d6baa408b3a7745d4087/Sources/Misc/RateLimiter.swift#L41
https://github.com/RevenueCat/purchases-ios/blame/72b06f69438eff747fe0d6baa408b3a7745d4087/Sources/Misc/RateLimiter.swift#L42
Some alternatives here:
The text was updated successfully, but these errors were encountered: