Is there a way to delete the queue of concurrent requests in R?
No. They must be managed.
Do you know how long the burst lasts? Ex: Everything happens in ~ 5 seconds.
Without getting into shared memory and forked processes, we can set up a flag to cause other routes that are being requested within X
seconds to be ignored / fail.
I believe we could lower the window
time down to 1 second and that would state that "any route that will attempt to be processed within 1 second after the first successful processing will fail." This would include all routes currently in the queue and any new routes within that time period, as they would flush through fairly quickly.
That being said, doesn't hurt to make the window
something larger, like 60
seconds.
# plumber.R
cache = list()
do_work <- function(id) {
cat("processing id: ", id, "\n")
str(cache)
Sys.sleep(10)
ans <- list(data = runif(10))
cat("done with id: ", id, "\n")
ans
}
#' @param id identification value to use when processing
#' @param window number of seconds after the first request finishes to not accept any more requests for a given id
#' @get /frontheavy
function(id = "abc", window = 10) {
if (!is.null(cache[[id]])) {
stop("already processing id: ", id, call. = FALSE)
}
# do the processing
ans <- do_work(id)
# like a semaphore, block the id from processing for a minimum amount of time
cache[[id]] <<- later::later(function() {
# Unblock the id
cache[[id]] <<- NULL
}, delay = window)
ans
}