-
Notifications
You must be signed in to change notification settings - Fork 4
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
Use condition variables instead of nanonext::stat() to query NNG connections #57
Comments
To be clear, this will affect the custom bus sockets that {crew} creates, not the data req/rep sockets internal to {mirai}. |
I hope the below gives you a clear example of how condition variables can be signalled by pipe events and how to read them. You are not doing any waits, so slightly more efficient not to set the flag for Without resetting, you can easily track the state by taking For your case you'd need a cv for each socket. library(nanonext)
s <- socket("bus")
s1 <- socket("bus")
cv <- cv()
pipe_notify(s, cv = cv, flag = FALSE)
listen(s)
cv_value(cv)
dial(s1) # connection established
cv_value(cv)
close(s1) # connection dropped
cv_value(cv)
cv_reset(cv)
cv_value(cv) |
Thanks, this is really helpful! So the condition variable can live on the client and it gets updated based on connection events? |
Yes, you just pair a cv with your bus socket on the client. Call Then you don't need to stat |
Awesome! Just to be safe, I think it might also be nice to check |
No need to check It doesn't matter if your listener dies or the other side, it's the same - your cv is notified on pipe events - a pipe requires both sides! |
That's really neat. I do wonder though, suppose I connect the listener and the dialer, but then something goes wrong with the listener and it accidentally disconnects. cv_value() is then 2, but yet the dialer is still connected. Do you see what I mean? |
And should I worry about a scenario where the listener disconnects and reconnects behind the scenes, silently incrementing the cv by 2? |
Nice try! I like how you think these through, but no listener, no connection. It is not possible for the dialer to still be connected. Connection requires both endpoints. |
I am pretty sure we're safe here. If the listener drops there is no reconnect mechanism. It is only a dialer which has the 'async' dial capability. |
Awesome! Then I think I can entirely rely on the condition variable as you say. If I set it up correctly, 0 means the worker has not yet connected, 1 means it is currently connected, and 2 means it connected and then disconnected.
Ah, that makes me even more glad I disabled |
This should improve performance at scale. cc @shikokuchuo.
The text was updated successfully, but these errors were encountered: