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?
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 poll
s all the contained Future
states, and returns Async::Ready
if you want to exit early. It might help performance if the contained Future
s 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
.
incoming
stream by checking some "global" (Arc<AtomicBool>
) flag whether to continue listening using Stream::take_while
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With