Skip to content

Instantly share code, notes, and snippets.

@daehn

daehn/RateLimit.swift

Last active Jun 28, 2019
Embed
What would you like to do?
import Foundation
private let log = Log(topic: .rateLimit)
/// Timeout based rate limit helper class.
final class RateLimit {
// MARK: - Private
private let timeout: TimeInterval
private var lastExecutionTimestamp: Date?
// MARK: - Public
init(timeout: TimeInterval) {
self.timeout = timeout
}
/// Calls the passed in block in case the timeout
/// is reached since the last call to this method.
///
/// - Parameter block: The block that should be executed.
/// - Returns: Whether the timeout has been reached and the block was called.
@discardableResult
func perform(_ block: () -> Void) -> Bool {
if let lastTimestamp = lastExecutionTimestamp {
let timeoutDiff = abs(lastTimestamp.timeIntervalSinceNow) - timeout
log.info("Difference until timeout: \(timeoutDiff)")
if timeoutDiff < 0 {
log.info("Skipping perform, timeout not reached.")
return false
} else {
lastExecutionTimestamp = Date()
block()
log.info("Timeout reached, updating timestamp.")
return true
}
} else {
lastExecutionTimestamp = Date()
block()
log.info("First execution, storing initial timestamp.")
return true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment