浏览代码

Catch exception from Socket::getPeerInfo() when pooling connection

Socket::getPeerInfo() may fail if its TCP connection has already
disconnected. In this case, we log this error. The success or failure
of pooling connection should not affect the later execution of the
program.
Tatsuhiro Tsujikawa 13 年之前
父节点
当前提交
2a51949132
共有 1 个文件被更改,包括 24 次插入6 次删除
  1. 24 6
      src/DownloadEngine.cc

+ 24 - 6
src/DownloadEngine.cc

@@ -347,6 +347,22 @@ void DownloadEngine::poolSocket
   poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL,proxyhost,proxyport),e);
 }
 
+namespace {
+bool getPeerInfo(std::pair<std::string, uint16_t>& res,
+                 const SharedHandle<SocketCore>& socket)
+{
+  try {
+    socket->getPeerInfo(res);
+    return true;
+  } catch(RecoverableException& e) {
+    // socket->getPeerInfo() can fail if the socket has been
+    // disconnected.
+    A2_LOG_INFO_EX("Getting peer info failed. Pooling socket canceled.", e);
+    return false;
+  }
+}
+} // namespace
+
 void DownloadEngine::poolSocket(const SharedHandle<Request>& request,
                                 const SharedHandle<Request>& proxyRequest,
                                 const SharedHandle<SocketCore>& socket,
@@ -354,9 +370,10 @@ void DownloadEngine::poolSocket(const SharedHandle<Request>& request,
 {
   if(!proxyRequest) {
     std::pair<std::string, uint16_t> peerInfo;
-    socket->getPeerInfo(peerInfo);
-    poolSocket(peerInfo.first, peerInfo.second,
-               A2STR::NIL, 0, socket, timeout);
+    if(getPeerInfo(peerInfo, socket)) {
+      poolSocket(peerInfo.first, peerInfo.second,
+                 A2STR::NIL, 0, socket, timeout);
+    }
   } else {
     // If proxy is defined, then pool socket with its hostname.
     poolSocket(request->getHost(), request->getPort(),
@@ -375,9 +392,10 @@ void DownloadEngine::poolSocket
 {
   if(!proxyRequest) {
     std::pair<std::string, uint16_t> peerInfo;
-    socket->getPeerInfo(peerInfo);
-    poolSocket(peerInfo.first, peerInfo.second, username,
-               A2STR::NIL, 0, socket, options, timeout);
+    if(getPeerInfo(peerInfo, socket)) {
+      poolSocket(peerInfo.first, peerInfo.second, username,
+                 A2STR::NIL, 0, socket, options, timeout);
+    }
   } else {
     // If proxy is defined, then pool socket with its hostname.
     poolSocket(request->getHost(), request->getPort(), username,