Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created January 29, 2018 14:16
Show Gist options
  • Save JohnMurray/6c61a733513c0edd56c4cd2b7c73a2dc to your computer and use it in GitHub Desktop.
Save JohnMurray/6c61a733513c0edd56c4cd2b7c73a2dc to your computer and use it in GitHub Desktop.
class ApiService[T](limit: Long, timeFrame: FiniteDuration) {
// previous code ...
private def hasCapacity(): Boolean = {
val now = LocalDateTime.now()
if (now.isAfter(windowStopTime)) {
windowStopTime = now.plusSeconds(timeFrame.toSeconds)
requestCount = 0
true
} else {
requestCount < limit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment