Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to check the callback queue length for a loop #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

atheriel
Copy link

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.

This introduces loop_queue_length(), which 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.

Includes documentation and tests.
@atheriel
Copy link
Author

atheriel commented May 6, 2020

Rebased to fix merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant