Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Last active June 22, 2018 00:41
Show Gist options
  • Save JohnMurray/f31eeff6c379fac19a18003c03371de1 to your computer and use it in GitHub Desktop.
Save JohnMurray/f31eeff6c379fac19a18003c03371de1 to your computer and use it in GitHub Desktop.
class ApiService[T] {
// other code ...
def cleanupRequests() {
val now = LocalDateTime.now()
if (now.isAfter(nextCleanupTime)) {
requestQueue.removeIf((req) => {
val expired = req.startDeadline.exists(now.isAfter)
if (expired) {
req.p.complete(Failure(new TimeoutException))
}
expired
})
nextCleanupTime = now.plusSeconds(cleanupTimeFrame.toSeconds)
}
}
private def tryRequest(): Unit = this.synchronized {
cleanupRequests()
// other code ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment