Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 967
sdlkjslfie Avatar asked Jan 19 '18 04:01

sdlkjslfie


2 Answers

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.

like image 189
Stefan Avatar answered Nov 01 '22 04:11

Stefan


Here's another option I recently found out about.

You basically need to drop the TcpListener/for_each future to stop listening. Since it never completes on its own, simply combine it with another future you control (e.g. a oneshot::Receiver<()>) using select(). The returned future will complete if either the listener completes (never) or the oneshot receives a value (which you can trigger by sending something into the other end).

For more complex programs which need to shut down multiple things, there is also a watch channel with one Sender and multiple Receiver<()>s.

There are also suggestions to make this easier in the futures crate itself.

like image 35
Zargony Avatar answered Nov 01 '22 04:11

Zargony