Commit 146af1e0 authored by Raphael Defosseux's avatar Raphael Defosseux

Merge branch 'giving-resolution-multiple-tries' into 'develop'

feat(fqdn): giving some time for FQDN resolution

See merge request oai/cn5g/oai-cn5g-smf!150
parents 75552725 50716511
...@@ -23,12 +23,21 @@ ...@@ -23,12 +23,21 @@
#include "logger.hpp" #include "logger.hpp"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <iostream> #include <iostream>
#include <chrono>
#include <thread>
#define MAX_NB_RESOLVE_TRIES 4
#define TIME_BETWEEN_TRIES 2
bool fqdn::resolve( bool fqdn::resolve(
const std::string& host_name, std::string& address, uint32_t& port, const std::string& host_name, std::string& address, uint32_t& port,
uint8_t& addr_type, const std::string& protocol) { uint8_t& addr_type, const std::string& protocol) {
int tries = 0;
Logger::smf_app().debug("Resolving a DNS (name %s)", host_name.c_str());
while (tries < MAX_NB_RESOLVE_TRIES) {
try { try {
boost::asio::io_context io_context = {}; boost::asio::io_context io_context = {};
Logger::smf_app().debug("Resolving DNS Try #%u", tries);
boost::asio::ip::tcp::resolver resolver{io_context}; boost::asio::ip::tcp::resolver resolver{io_context};
boost::asio::ip::tcp::resolver::results_type endpoints = boost::asio::ip::tcp::resolver::results_type endpoints =
...@@ -41,7 +50,7 @@ bool fqdn::resolve( ...@@ -41,7 +50,7 @@ bool fqdn::resolve(
address = endpoint.address().to_string(); address = endpoint.address().to_string();
port = endpoint.port(); port = endpoint.port();
Logger::smf_app().debug( Logger::smf_app().debug(
"Resolve a DNS (name %s, protocol %s): Ip Addr %s, port %u", "Resolved a DNS (name %s, protocol %s): Ip Addr %s, port %u",
host_name.c_str(), protocol.c_str(), address.c_str(), port); host_name.c_str(), protocol.c_str(), address.c_str(), port);
if (endpoint.address().is_v4()) if (endpoint.address().is_v4())
addr_type = 0; addr_type = 0;
...@@ -50,10 +59,16 @@ bool fqdn::resolve( ...@@ -50,10 +59,16 @@ bool fqdn::resolve(
return true; return true;
} }
} catch (std::exception& e) { } catch (std::exception& e) {
tries++;
if (tries == MAX_NB_RESOLVE_TRIES) {
throw std::runtime_error( throw std::runtime_error(
"Cannot resolve a DNS name " + std::string(e.what())); "Cannot resolve a DNS name " + std::string(e.what()) + " after " +
std::to_string(tries) + " tries");
return false; return false;
} }
std::this_thread::sleep_for(std::chrono::seconds(TIME_BETWEEN_TRIES));
}
}
return false; return false;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment