From: David Reiss Date: Tue, 8 Apr 2008 06:26:27 +0000 (+0000) Subject: Use poll instead of select in TServerSocket X-Git-Tag: 0.2.0~823 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=1677ac9cc7550ea3eb4a4e46fbcf16790c2cb70c;p=common%2Fthrift.git Use poll instead of select in TServerSocket Summary: - select has a restricted max fd set size. While this should not be an issue in the normal case for TServerSocket because it is started when the process starts, it could be a problem if someone wanted to start a new server socket at a later point in time when socket values could be greater than max. Reviewed By: dreiss Test Plan: - Compiled. - Deployed search tier and made sure it could serve queries. Revert: OK TracCamp Project: Thrift DiffCamp Revision: 11076 git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665649 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/transport/TServerSocket.cpp b/lib/cpp/src/transport/TServerSocket.cpp index 2b4e7c34..1bfc56bc 100644 --- a/lib/cpp/src/transport/TServerSocket.cpp +++ b/lib/cpp/src/transport/TServerSocket.cpp @@ -5,7 +5,7 @@ // http://developers.facebook.com/thrift/ #include -#include +#include #include #include #include @@ -246,18 +246,20 @@ shared_ptr TServerSocket::acceptImpl() { throw TTransportException(TTransportException::NOT_OPEN, "TServerSocket not listening"); } - fd_set fds; + struct pollfd fds[2]; int maxEintrs = 5; int numEintrs = 0; while (true) { - FD_ZERO(&fds); - FD_SET(serverSocket_, &fds); + memset(fds, 0 , sizeof(fds)); + fds[0].fd = serverSocket_; + fds[0].events = POLLIN; if (intSock2_ >= 0) { - FD_SET(intSock2_, &fds); + fds[1].fd = intSock2_; + fds[1].fd = POLLIN; } - int ret = select(serverSocket_+1, &fds, NULL, NULL, NULL); + int ret = poll(fds, 2, -1); if (ret < 0) { // error cases @@ -272,7 +274,7 @@ shared_ptr TServerSocket::acceptImpl() { throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy); } else if (ret > 0) { // Check for an interrupt signal - if (intSock2_ >= 0 && FD_ISSET(intSock2_, &fds)) { + if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) { int8_t buf; if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { int errno_copy = errno; @@ -283,11 +285,11 @@ shared_ptr TServerSocket::acceptImpl() { } // Check for the actual server socket being ready - if (FD_ISSET(serverSocket_, &fds)) { + if (fds[0].revents & POLLIN) { break; } } else { - GlobalOutput("TServerSocket::acceptImpl() select 0"); + GlobalOutput("TServerSocket::acceptImpl() poll 0"); throw TTransportException(TTransportException::UNKNOWN); } }