Skip to content

Instantly share code, notes, and snippets.

@daehn
Last active December 4, 2023 12:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daehn/4afa3b4176d5d157db559406b68256bf to your computer and use it in GitHub Desktop.
Save daehn/4afa3b4176d5d157db559406b68256bf to your computer and use it in GitHub Desktop.
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