提交 d74a7f61 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Improve PortManager.cpp:

- Do not limit the number of failed bind() attempts to 20 since it does
  not work well in scenarios that launch tons of Workers with same port
  range.
- Instead iterate all ports in the range given to the Worker.
上级 04c516ef
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
### 3.4.7 (WIP) ### 3.4.7 (WIP)
* `PortManager.cpp`: Do not limit the number of failed `bind()` attempts to 20 since it does not work well in scenarios that launch tons of `Workers` with same port range. Instead iterate all ports in the range given to the Worker.
* Do not copy `catch.hpp` into `test/include/` but make the GYP `mediasoup-worker-test` target include the corresponding folder in `deps/catch`. * Do not copy `catch.hpp` into `test/include/` but make the GYP `mediasoup-worker-test` target include the corresponding folder in `deps/catch`.
......
...@@ -24,10 +24,6 @@ inline static void onFakeConnection(uv_stream_t* /*handle*/, int /*status*/) ...@@ -24,10 +24,6 @@ inline static void onFakeConnection(uv_stream_t* /*handle*/, int /*status*/)
namespace RTC namespace RTC
{ {
/* Static. */
static constexpr size_t MaxBindAttempts{ 20 };
/* Class variables. */ /* Class variables. */
std::unordered_map<std::string, std::vector<bool>> PortManager::mapUdpIpPorts; std::unordered_map<std::string, std::vector<bool>> PortManager::mapUdpIpPorts;
...@@ -45,12 +41,11 @@ namespace RTC ...@@ -45,12 +41,11 @@ namespace RTC
int err; int err;
int family = Utils::IP::GetFamily(ip); int family = Utils::IP::GetFamily(ip);
struct sockaddr_storage bindAddr; // NOLINT(cppcoreguidelines-pro-type-member-init) struct sockaddr_storage bindAddr; // NOLINT(cppcoreguidelines-pro-type-member-init)
size_t initialPortIdx;
size_t portIdx; size_t portIdx;
size_t attempt{ 0 };
size_t bindAttempt{ 0 };
int flags{ 0 }; int flags{ 0 };
std::vector<bool>& ports = PortManager::GetPorts(transport, ip); std::vector<bool>& ports = PortManager::GetPorts(transport, ip);
size_t attempt{ 0u };
size_t numAttempts = ports.size();
uv_handle_t* uvHandle{ nullptr }; uv_handle_t* uvHandle{ nullptr };
uint16_t port; uint16_t port;
std::string transportStr; std::string transportStr;
...@@ -98,42 +93,55 @@ namespace RTC ...@@ -98,42 +93,55 @@ namespace RTC
} }
} }
// Choose a random port to start from. // Choose a random port index to start from.
initialPortIdx = static_cast<size_t>(Utils::Crypto::GetRandomUInt( portIdx = static_cast<size_t>(Utils::Crypto::GetRandomUInt(
static_cast<uint32_t>(0), static_cast<uint32_t>(ports.size() - 1))); static_cast<uint32_t>(0), static_cast<uint32_t>(ports.size() - 1)));
portIdx = initialPortIdx;
// Iterate all ports until getting one available. Fail if none found and also // Iterate all ports until getting one available. Fail if none found and also
// if bind() fails N times in theorically available ports. // if bind() fails N times in theorically available ports.
while (true) while (true)
{ {
// Increase attempt number.
++attempt; ++attempt;
if (portIdx < ports.size() - 1) // If we have tried all the ports in the range throw.
++portIdx; if (attempt > numAttempts)
else {
portIdx = 0; MS_THROW_ERROR(
"no more available ports [transport:%s, ip:%s, numAttempt:%zu]",
transportStr.c_str(),
ip.c_str(),
numAttempts);
}
// Increase current port index.
portIdx = (portIdx + 1) % ports.size();
// So the corresponding port is the vector position plus the RTC minimum port.
port = static_cast<uint16_t>(portIdx + Settings::configuration.rtcMinPort);
MS_DEBUG_DEV(
"testing port [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]",
transportStr.c_str(),
ip.c_str(),
port,
attempt,
numAttempts);
// Check whether this port is not available. // Check whether this port is not available.
if (ports[portIdx]) if (ports[portIdx])
{ {
MS_DEBUG_DEV( MS_DEBUG_DEV(
"port in use, trying again [%s:%s, attempt:%zu]", transportStr.c_str(), ip.c_str(), attempt); "port in use, trying again [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]",
transportStr.c_str(),
// If we have tried all ports in the vector throw. ip.c_str(),
if (portIdx == initialPortIdx) port,
{ attempt,
MS_THROW_ERROR( numAttempts);
"no available port [%s:%s, attempt:%zu]", transportStr.c_str(), ip.c_str(), attempt);
}
continue; continue;
} }
// So the found port is the vector position plus the RTC minimum port.
port = static_cast<uint16_t>(portIdx + Settings::configuration.rtcMinPort);
// Here we already have a theorically available port. Now let's check // Here we already have a theorically available port. Now let's check
// whether no other process is binding into it. // whether no other process is binding into it.
...@@ -150,8 +158,6 @@ namespace RTC ...@@ -150,8 +158,6 @@ namespace RTC
} }
// Try to bind on it. // Try to bind on it.
++bindAttempt;
switch (transport) switch (transport)
{ {
case Transport::UDP: case Transport::UDP:
...@@ -191,10 +197,12 @@ namespace RTC ...@@ -191,10 +197,12 @@ namespace RTC
flags); flags);
MS_WARN_DEV( MS_WARN_DEV(
"uv_udp_bind() failed [%s:%s, attempt:%zu]: %s", "uv_udp_bind() failed [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]: %s",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
port,
attempt, attempt,
numAttempts,
uv_strerror(err)); uv_strerror(err));
break; break;
...@@ -210,10 +218,12 @@ namespace RTC ...@@ -210,10 +218,12 @@ namespace RTC
if (err) if (err)
{ {
MS_WARN_DEV( MS_WARN_DEV(
"uv_tcp_bind() failed [%s:%s, attempt:%zu]: %s", "uv_tcp_bind() failed [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]: %s",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
port,
attempt, attempt,
numAttempts,
uv_strerror(err)); uv_strerror(err));
} }
...@@ -227,10 +237,12 @@ namespace RTC ...@@ -227,10 +237,12 @@ namespace RTC
static_cast<uv_connection_cb>(onFakeConnection)); static_cast<uv_connection_cb>(onFakeConnection));
MS_WARN_DEV( MS_WARN_DEV(
"uv_listen() failed [%s:%s, attempt:%zu]: %s", "uv_listen() failed [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]: %s",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
port,
attempt, attempt,
numAttempts,
uv_strerror(err)); uv_strerror(err));
} }
...@@ -238,62 +250,62 @@ namespace RTC ...@@ -238,62 +250,62 @@ namespace RTC
} }
} }
// If it succeeded, stop here. // If it succeeded, exit the loop here.
if (err == 0) if (err == 0)
break; break;
// If it failed, close the handle and check the reason. // If it failed, close the handle and check the reason.
uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onClose)); uv_close(reinterpret_cast<uv_handle_t*>(uvHandle), static_cast<uv_close_cb>(onClose));
// If bind() fails due to "too many open files" throw. switch (err)
if (err == UV_EMFILE)
{ {
MS_THROW_ERROR( // If bind() fails due to "too many open files" just throw.
"port bind failed due to too many open files [%s:%s, attempt:%zu]", case UV_EMFILE:
transportStr.c_str(),
ip.c_str(),
attempt);
}
// If cannot bind in the given IP, throw.
else if (err == UV_EADDRNOTAVAIL)
{ {
MS_THROW_ERROR( MS_THROW_ERROR(
"port bind failed due to address not available [%s:%s, attempt:%zu]", "port bind failed due to too many open files [transport:%s, ip:%s, port:%" PRIu16
", attempt:%zu/%zu]",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
attempt); port,
attempt,
numAttempts);
break;
} }
// If bind() fails for more that MaxBindAttempts then throw.
else if (bindAttempt > MaxBindAttempts) // If cannot bind in the given IP, throw.
case UV_EADDRNOTAVAIL:
{ {
MS_THROW_ERROR( MS_THROW_ERROR(
"port bind failed too many times [%s:%s, attempt:%zu]", "port bind failed due to address not available [transport:%s, ip:%s, port:%" PRIu16
", attempt:%zu/%zu]",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
attempt); port,
attempt,
numAttempts);
break;
} }
// If we have tried all the ports in the range throw.
else if (portIdx == initialPortIdx) default:
{ {
MS_THROW_ERROR( // Otherwise continue in the loop to try again with next port.
"no more available ports [%s:%s, attempt:%zu]", transportStr.c_str(), ip.c_str(), attempt);
} }
// Otherwise try again.
else
{
continue;
} }
} }
// Mark the port as unavailable. // If here, we got an available port. Mark it as unavailable.
ports[portIdx] = true; ports[portIdx] = true;
MS_DEBUG_DEV( MS_DEBUG_DEV(
"bind succeeded [%s:%s, port:%" PRIu16 ", attempt:%zu]", "bind succeeded [transport:%s, ip:%s, port:%" PRIu16 ", attempt:%zu/%zu]",
transportStr.c_str(), transportStr.c_str(),
ip.c_str(), ip.c_str(),
port, port,
attempt); attempt,
numAttempts);
return static_cast<uv_handle_t*>(uvHandle); return static_cast<uv_handle_t*>(uvHandle);
} }
...@@ -363,7 +375,11 @@ namespace RTC ...@@ -363,7 +375,11 @@ namespace RTC
// If the IP is already handled, return its ports vector. // If the IP is already handled, return its ports vector.
if (it != PortManager::mapUdpIpPorts.end()) if (it != PortManager::mapUdpIpPorts.end())
return it->second; {
auto& ports = it->second;
return ports;
}
// Otherwise add an entry in the map and return it. // Otherwise add an entry in the map and return it.
uint16_t numPorts = uint16_t numPorts =
...@@ -375,7 +391,9 @@ namespace RTC ...@@ -375,7 +391,9 @@ namespace RTC
std::piecewise_construct, std::make_tuple(ip), std::make_tuple(numPorts, false)); std::piecewise_construct, std::make_tuple(ip), std::make_tuple(numPorts, false));
// pair.first is an iterator to the inserted value. // pair.first is an iterator to the inserted value.
return pair.first->second; auto& ports = pair.first->second;
return ports;
} }
case Transport::TCP: case Transport::TCP:
...@@ -384,7 +402,11 @@ namespace RTC ...@@ -384,7 +402,11 @@ namespace RTC
// If the IP is already handled, return its ports vector. // If the IP is already handled, return its ports vector.
if (it != PortManager::mapTcpIpPorts.end()) if (it != PortManager::mapTcpIpPorts.end())
return it->second; {
auto& ports = it->second;
return ports;
}
// Otherwise add an entry in the map and return it. // Otherwise add an entry in the map and return it.
uint16_t numPorts = uint16_t numPorts =
...@@ -396,7 +418,9 @@ namespace RTC ...@@ -396,7 +418,9 @@ namespace RTC
std::piecewise_construct, std::make_tuple(ip), std::make_tuple(numPorts, false)); std::piecewise_construct, std::make_tuple(ip), std::make_tuple(numPorts, false));
// pair.first is an iterator to the inserted value. // pair.first is an iterator to the inserted value.
return pair.first->second; auto& ports = pair.first->second;
return ports;
} }
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论