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

Run I/O on separate thread #62

Merged
merged 20 commits into from
Jan 9, 2020
Merged

Run I/O on separate thread #62

merged 20 commits into from
Jan 9, 2020

Conversation

wch
Copy link
Contributor

@wch wch commented Jan 6, 2020

Closes #56. This PR runs the websocket I/O on a separate thread, instead of polling using later(). When a WebSocket exists but is idle, in a terminal it previously consumed about 2.5% CPU on my computer, and in RStudio it previously consumed 0.7%. Now it consumes 0% because the I/O thread's event loop works without using polling (I believe it uses something like epoll or select under the hood).

Also, with Chromote, it previously idled at ~6.5% CPU, but after this change and rstudio/chromote#16, it idles at 0%.

TODO:

  • Document that if even you lose the reference to an open WebSocket object, it will continue to exist until the websocket is closed; after that point, a gc() will GC it.
  • Update existing tests for new non-polling behavior.
  • Add test that if ref to ws is gone, and the websocket is closed from the other end, that a gc() will cause the finalizer to run.
  • Add test for running in private loop.
  • Add test that a failed connection gets finalized, even without a close()
  • Add substitutes for Rcerr and Rcout that are safe to run on background thread.

@wch wch marked this pull request as ready for review January 8, 2020 00:21
@wch wch requested a review from jcheng5 January 8, 2020 00:21
src/debug.h Outdated Show resolved Hide resolved
@jcheng5
Copy link
Member

jcheng5 commented Jan 9, 2020

Feel free to remove wsRestart and wsReset if you feel like it

@wch wch merged commit 78a553d into master Jan 9, 2020
@wch wch deleted the wch-threads branch January 9, 2020 21:07
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.

Should switch from polling to C-level select() or similar
2 participants