later
later copied to clipboard
Adds the ability to check the callback queue length for a loop
This PR proposes adding a way to check the total number of queued callbacks for a loop.
One of the issues we've encountered with httpuv
/Plumber APIs (and a another later
-based event loop) is figuring out how "busy" they are. In event-based languages like Node you can easily count how many requests are "in-flight" to determine if they are being serviced fast enough, but I'm not aware of a way to do this in R at present.
To enable these kinds of measurements, this PR introduces loop_queue_length()
(and corresponding C++ APIs). It is similar to loop_empty()
but provides a more granular measure of what remains to be executed. Unlike list_queue()
, it is an exported function meant to be used externally. I'm happy to take naming suggestions, too.
As an example of how it might be used, here is a simple httpuv
server that emulates an expensive 500ms operation and prints out the length of the remaining queue after each request:
httpuv::runServer(
"127.0.0.1", 8081,
list(call = function(req) {
Sys.sleep(0.5)
cat("\rqueued:", later::loop_queue_length())
list(
status = 200L,
headers = list("Content-Type" = "text/plain"),
body = raw(0)
)
})
)
You can generate "load" to test this with e.g. $ wrk -c 5 -t 1 -d 5 http://127.0.0.1:8081/
.
The changes include documentation and tests.
Rebased to fix merge conflicts.