Is it possible to close a TcpListener in Tokio?

会有一股神秘感。 提交于 2019-12-10 04:54:39

问题


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. 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.




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!