From: David Reiss Date: Tue, 9 Mar 2010 05:19:54 +0000 (+0000) Subject: cpp: TSocket: call a second gettimeofday only for error checking X-Git-Tag: 0.3.0~82 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=a1a15112fed5f7ec934a352920b4c535941fd1ab;p=common%2Fthrift.git cpp: TSocket: call a second gettimeofday only for error checking Previously, we called gettimeofday twice for every send, which is costly. Now, we only make the second call if send fails with EAGAIN. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920677 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp index 55e8dc32..d27c8681 100644 --- a/lib/cpp/src/transport/TSocket.cpp +++ b/lib/cpp/src/transport/TSocket.cpp @@ -308,16 +308,17 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) { gettimeofday(&begin, NULL); int got = recv(socket_, buf, len, 0); int errno_copy = errno; //gettimeofday can change errno - struct timeval end; - gettimeofday(&end, NULL); - uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) - + (((uint64_t)(end.tv_usec - begin.tv_usec)))); ++g_socket_syscalls; // Check for error on read if (got < 0) { if (errno_copy == EAGAIN) { // check if this is the lack of resources or timeout case + struct timeval end; + gettimeofday(&end, NULL); + uint32_t readElapsedMicros = (((end.tv_sec - begin.tv_sec) * 1000 * 1000) + + (((uint64_t)(end.tv_usec - begin.tv_usec)))); + if (!eagainThresholdMicros || (readElapsedMicros < eagainThresholdMicros)) { if (retries++ < maxRecvRetries_) { usleep(50);