Stiffstream / restinio

Cross-platform, efficient, customizable, and robust asynchronous HTTP(S)/WebSocket server C++ library with the right balance between performance and ease of use
Other
1.15k stars 93 forks source link

random port #126

Closed qwertzui11 closed 3 years ago

qwertzui11 commented 3 years ago

hi. Thx for your great on this awesome framework.

I'd like to host the server on a random port. Aka using port 0 and then call some method like get_port(). asio::*::acceptor assigns on port 0 a random port. This is useful when it comes to unittests, that may run in parallel. I could not find any information on that topic. Is there a possibility to set the port to 0 and then retreive it?

so far I got:

using server_t = restinio::http_server_t<>;
using settings_t = restinio::server_settings_t<>;

server_t srv1{
    restinio::external_io_context(*context),
    settings_t{}.port(0).address("localhost").request_handler([](auto) {
      return restinio::request_accepted();
    })};
#if 0
srv1.get_port(); // does not work. desperate experiment ;-)
#endif

thx for ur time!

eao197 commented 3 years ago

Hi! Thanks for reporting this use-case.

We've never used RESTinio in such scenarios. I'll investigate the behavior of RESTinio's acceptor and reply to you back later.

eao197 commented 3 years ago

At this time I don't see an easy way to pass the actual local_endpoint of the acceptor to the outside of http_server_t instance. One of the reasons is the fact that that local_endpoint will be known only after completion of http_server_t::open_sync method, but this moment is hard to detect from the outside of http_server_t instance.

One of the current ideas is to add another callback to server_settings_t. Something like:

asio::ip::tcp::endpoint server_endpoint;
...
server_t srv1{
    restinio::external_io_context(*context),
    settings_t{}.port(0).address("localhost")
      .post_acceptor_bind_hook([&server_endpoint](asio::ip::tcp::acceptor & acceptor) {
            server_endpoint = acceptor.local_endpoint();
         })
      .request_handler([](auto) {
         return restinio::request_accepted();
      })};
...

But I'm not sure that's a good idea yet. Will think further.

qwertzui11 commented 3 years ago

I don't know this library well enough, nor its design.

hoooowever some ideas :laughing:

eao197 commented 3 years ago

Hi @qwertzui11 !

A new post-bind callback is added to RESTinio. You can see an example of its usage here: https://github.com/Stiffstream/restinio/blob/f3bdbfc8d16efdc4ff42b92def01b1a5339ced9d/dev/test/handle_requests/acceptor_post_bind_hook/main.cpp#L23-L66

The new functionality is in 0.6-dev-0.6.11 branch now. I plan to add yet more functionality into v.0.6.11 before the public release of that version. Hope I can complete it in a week, but it hard to predict. So if you can't wait please let us know.

qwertzui11 commented 3 years ago

excellent. I'll wait and try it out as soon as it's available on conan. Thx for the great work! :heart_eyes:

eao197 commented 3 years ago

It seems that the scope of v.0.6.11 is completed. We have to update to docs and then the release will be published.

qwertzui11 commented 3 years ago

works! awesome, thx!