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.