Commit f02e3b60 authored by Steven Murray's avatar Steven Murray
Browse files

bug #104842: RFE: castor::io::ServerSocket::bind() should yield the CPU

Fixed in the trunk.
parent 27653be4
......@@ -203,8 +203,7 @@ void castor::io::ServerSocket::bind(int lowPort, int highPort)
//------------------------------------------------------------------------------
// bind
//------------------------------------------------------------------------------
void castor::io::ServerSocket::bind()
{
void castor::io::ServerSocket::bind() {
int rc = -1;
int port;
......@@ -222,6 +221,7 @@ void castor::io::ServerSocket::bind()
srand(tv.tv_usec * tv.tv_sec);
// randomly select a free port in the allowed port range
int nbFailedBinds = 0;
while (0 != rc) {
port = (rand() % (m_highPort - m_lowPort +1 )) + m_lowPort;
m_saddr.sin_port = htons(port);
......@@ -234,5 +234,15 @@ void castor::io::ServerSocket::bind()
e.getMessage() << "Failed to bind: Port in use: " << port;
throw e;
}
// Count the number of failed attempts to bind a listening socket
if(0 != rc) {
nbFailedBinds++;
}
// sleep every 10 attempts in order not to consume too much CPU
if(0 == nbFailedBinds % 10) {
::sleep(1);
}
}
}
Supports Markdown
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