Hi,
I’m trying to make a tokio based library for the xi editor. It’s basically json-rpc, but the enpoints need to behave both as clients (sending requests/notifications) and as servers (handling incoming requests/notifications). I have an Endpoint
struct that implements Future
like this (the actual code is here):
impl<S, T: AsyncRead + AsyncWrite> Future for Endpoint<S, T>
where
S: Service,
{
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
// do some non-blocking stuff, mostly polling streams and channels, and spawning futures
// [...]
// notify the reactor to reschedule current the endpoint for polling
futures::task::current().notify();
Ok(Async::NotReady)
}
}
The problem with this code is that the CPU jumps to 100% because the future keep being polled.
I could just add a sleep (but what would a good value be?), but it does not feel right: if the endpoint has a lot of work to do, I probably don’t want it to sleep.
Is there a better strategy?