问题
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?
回答1:
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. TheFuture::poll
implementation for your struct thenpoll
s all the containedFuture
states, and returnsAsync::Ready
if you want to exit early. It might help performance if the containedFuture
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 toNone
.- 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 thefor_each
loop)
The latter two methods only stop after seeing/handling an incoming connection, so they only work in a busy environment.
回答2:
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).
There are also suggestions to make this easier in the futures crate itself.
来源:https://stackoverflow.com/questions/48334079/is-it-possible-to-close-a-tcplistener-in-tokio