Kagome
Polkadot Runtime Engine in C++17
tuner.cpp
Go to the documentation of this file.
1 
7 
8 namespace kagome::api {
9 
10  std::unique_ptr<Acceptor> acceptOnFreePort(
11  std::shared_ptr<boost::asio::io_context> context,
12  Endpoint endpoint,
13  uint16_t port_tolerance,
14  const log::Logger &logger) {
15  for (;;) {
16  try {
17  auto acceptor = std::make_unique<Acceptor>(*context, endpoint);
18  return acceptor;
19  } catch (
20  const boost::wrapexcept<boost::system::system_error> &exception) {
21  if ((exception.code() == boost::asio::error::address_in_use)
22  and (port_tolerance > 0)) {
23  SL_INFO(logger,
24  "Port {} is already in use, trying next one. ({} attempt(s) "
25  "left)",
26  endpoint.port(),
27  port_tolerance);
28  --port_tolerance;
29  endpoint.port(endpoint.port() + 1);
30  } else {
31  throw exception;
32  }
33  }
34  }
35  }
36 
37 } // namespace kagome::api
std::unique_ptr< Acceptor > acceptOnFreePort(std::shared_ptr< boost::asio::io_context > context, Endpoint endpoint, uint16_t port_tolerance, const log::Logger &logger)
Definition: tuner.cpp:10
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
boost::asio::ip::tcp::endpoint Endpoint
Definition: tuner.hpp:20