Last active
December 4, 2023 12:32
-
-
Save daehn/4afa3b4176d5d157db559406b68256bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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