]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Make deep nesting more shallow by early returns
authorOtto <otto.moerbeek@open-xchange.com>
Fri, 29 Jan 2021 09:05:08 +0000 (10:05 +0100)
committerOtto <otto.moerbeek@open-xchange.com>
Fri, 29 Jan 2021 12:45:41 +0000 (13:45 +0100)
pdns/pdns_recursor.cc

index fbd0f3076c1efcd99cdedf9e99a85b8ad599bccc..66fc158d2f8107fb5bb1ad3884ebde31f5f2a38e 100644 (file)
@@ -860,40 +860,40 @@ static void finishTCPReply(std::unique_ptr<DNSComboWriter>& dc, bool hadError, b
   if (hadError) {
     terminateTCPConnection(dc->d_socket);
     dc->d_socket = -1;
+    return;
   }
-  else {
-    dc->d_tcpConnection->queriesCount++;
-    if (g_tcpMaxQueriesPerConn && dc->d_tcpConnection->queriesCount >= g_tcpMaxQueriesPerConn) {
-      try {
-        t_fdm->removeReadFD(dc->d_socket);
-      }
-      catch (FDMultiplexerException &) {
-      }
-      dc->d_socket = -1;
+  dc->d_tcpConnection->queriesCount++;
+  if (g_tcpMaxQueriesPerConn && dc->d_tcpConnection->queriesCount >= g_tcpMaxQueriesPerConn) {
+    try {
+      t_fdm->removeReadFD(dc->d_socket);
     }
-    else {
-      Utility::gettimeofday(&g_now, 0); // needs to be updated
-      struct timeval ttd = g_now;
-      // If we cross from max to max-1 in flight requests, the fd was not listened to, add it back
-      if (updateInFlight && dc->d_tcpConnection->d_requestsInFlight == TCPConnection::s_maxInFlight - 1) {
-        // A read error might have happened. If we add the fd back, it will most likely error again.
-        // This is not a big issue, the next handleTCPClientReadable() will see another read error
-        // and take action.
-        ttd.tv_sec += g_tcpTimeout;
-        t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection, &ttd);
-      } else {
-        // fd might have been removed by read error code, or a read timeout, so expect an exception
-        try {
-          t_fdm->setReadTTD(dc->d_socket, ttd, g_tcpTimeout);
-        }
-        catch (const FDMultiplexerException &) {
-          // but if the FD was removed because of a timeout while we were sending a response,
-          // we need to re-arm it. If it was an error it will error again.
-          ttd.tv_sec += g_tcpTimeout;
-          t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection, &ttd);
-        }
-      }
+    catch (FDMultiplexerException &) {
     }
+    dc->d_socket = -1;
+    return;
+  }
+
+  Utility::gettimeofday(&g_now, nullptr); // needs to be updated
+  struct timeval ttd = g_now;
+
+  // If we cross from max to max-1 in flight requests, the fd was not listened to, add it back
+  if (updateInFlight && dc->d_tcpConnection->d_requestsInFlight == TCPConnection::s_maxInFlight - 1) {
+    // A read error might have happened. If we add the fd back, it will most likely error again.
+    // This is not a big issue, the next handleTCPClientReadable() will see another read error
+    // and take action.
+    ttd.tv_sec += g_tcpTimeout;
+    t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection, &ttd);
+    return;
+  }
+  // fd might have been removed by read error code, or a read timeout, so expect an exception
+  try {
+    t_fdm->setReadTTD(dc->d_socket, ttd, g_tcpTimeout);
+  }
+  catch (const FDMultiplexerException &) {
+    // but if the FD was removed because of a timeout while we were sending a response,
+    // we need to re-arm it. If it was an error it will error again.
+    ttd.tv_sec += g_tcpTimeout;
+    t_fdm->addReadFD(dc->d_socket, handleRunningTCPQuestion, dc->d_tcpConnection, &ttd);
   }
 }