Thank you, that worked once I added an ignoreResult() in the middle of it. I've updated my tutorial sample with these changes and added a proxy service to demonstrate that portion. It's working well except that shutting down the proxy isn't as clean as I'd like. Please leave feedback on the code review when you can.
‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ On Sunday, November 3, 2019 11:34 PM, 'Kenton Varda' via Cap'n Proto <[email protected]> wrote: > On Sun, Nov 3, 2019 at 1:56 PM 'Newbugreport' via Cap'n Proto > <[email protected]> wrote: > >> Using kj + capnp, when a service receives an RPC call and needs to call >> other services to form a reply, how can I do this without blocking or >> starving the event loop? > > When you make an outgoing call, you can use .then() to register a callback to > invoke when the RPC completes, instead of using .wait(). In fact, inside the > implementation of an RPC server, you cannot use .wait(); you can only use > .then(). The event loop continues to run in the meantime. > >> When I receive a terminate signal via linux signals or RPC, how can I >> cleanly stop the event loop? > > First, you'll need to arrange to catch the signal via UnixEventPort. When you > call kj::setupAsyncIo(), the returned struct contains a reference on a > UnixEventPort. You can use its onSignal() method to register interest in a > signal like SIGTERM. This method returns a kj::Promise<void> that resolves > when the signal is received. See `kj/async-unix.h` for full documentation. > > The trick to ending the event loop is to make sure that wherever your server > called .wait() to run the event loop, it is waiting for a promise that will > complete when you want to exit. > > So, instead of doing: > > kj::NEVER_DONE.wait(io.waitScope); > > You could instead do: > > io.unixEventPort.onSignal(SIGTERM).wait(io.waitScope); > > If you want to have multiple ways to terminate the loop, you should use > promise.exclusiveJoin() to join multiple promises, and then wait on the > result. When any of the joined promises completes, the wait will finish. > > In order to wait on a particular RPC call, you probably want to create a > Promise/Fulfiller pair. > > auto paf = kj::newPromiseAndFulfiller<void>(); > > Pass off paf.fulfiller to the RPC server implementation. Call its fulfill() > method when the shutdown RPC is received. > > Then your wait looks like this: > > io.unixEventPort.onSignal(SIGTERM) > .exclusiveJoin(kj::mv(paf.promise)) > .wait(io.waitScope); > >> I'll try to integrate the answers into the tutorial sample I'm working on. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Cap'n Proto" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> [https://groups.google.com/d/msgid/capnproto/mbrd86Ipd_VEEnlIK4MkqX0ZRpeJgQd6DvPOMh4rJO5Y8gVZpJL4NRfiQVzopbJqgUWr27jif9btR0LWBcj9Siik4TAFlR9EsyhtLYdN9Ek%3D%40protonmail.com](https://groups.google.com/d/msgid/capnproto/mbrd86Ipd_VEEnlIK4MkqX0ZRpeJgQd6DvPOMh4rJO5Y8gVZpJL4NRfiQVzopbJqgUWr27jif9btR0LWBcj9Siik4TAFlR9EsyhtLYdN9Ek%3D%40protonmail.com?utm_medium=email&utm_source=footer). > > -- > You received this message because you are subscribed to the Google Groups > "Cap'n Proto" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > [https://groups.google.com/d/msgid/capnproto/CAJouXQmfa%3DBHUO-tHwecJMj3mLdwvbtvjuyVtto0JmpSeoRnOg%40mail.gmail.com](https://groups.google.com/d/msgid/capnproto/CAJouXQmfa%3DBHUO-tHwecJMj3mLdwvbtvjuyVtto0JmpSeoRnOg%40mail.gmail.com?utm_medium=email&utm_source=footer). -- You received this message because you are subscribed to the Google Groups "Cap'n Proto" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/tSz4-uV0ErzIve-BOZnQKPbT5cQ-Idj4RrbJhi71UvpP42heI-Mxc2EEriiYVuRqXRLIBU4x6qQxp9y9I-hskGb6e2vohFm3OSHv7IdfpAE%3D%40protonmail.com.
