Search code examples
rustrust-tokio

Is it possible to close a TcpListener in Tokio?


I have a tokio_core::net::TcpListener that I create and then call the incoming method on to get a stream of incoming connections. I then use the for_each method on that stream to turn it into a future and run the future on an event loop. Once I do that is there any way to unbind from the port at some later time?

If not, is there any other API in Tokio that can be used to create a TCP server which can be closed?


Solution

  • In short, you need to drop the TcpListener / the Future returned by for_each.

    You could:

    • Write a Future implementation for some struct containing the rest of your application's state. The Future::poll implementation for your struct then polls all the contained Future states, and returns Async::Ready if you want to exit early. It might help performance if the contained Futures are wrapped in oneshot::spawn to run in their own task.

      Your struct would contain an Option<SpawnHandle<(), ...>>. If you want to stop listening just set it to None.

    • Filter the incoming stream by checking some "global" (Arc<AtomicBool>) flag whether to continue listening using Stream::take_while
    • Check at the end of your for_each handler whether to continue listening and return an error otherwise (which stops the for_each loop)

    The latter two methods only stop after seeing/handling an incoming connection, so they only work in a busy environment.