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 55e8dc3..d27c868 100644
--- a/lib/cpp/src/transport/TSocket.cpp
+++ b/lib/cpp/src/transport/TSocket.cpp
@@ -308,16 +308,17 @@
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);