From 767c1a90fed87d36aaf077322bb9225eff98ee52 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 9 Mar 2010 05:20:28 +0000 Subject: [PATCH] cpp: Minor TSocketPool cleanups - Make sure the underlying socket is always closed. - Don't reconnect on repeated calls to open. - Move some code outside of a try block. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@920691 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/transport/TSocketPool.cpp | 37 +++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/cpp/src/transport/TSocketPool.cpp b/lib/cpp/src/transport/TSocketPool.cpp index 76eba4ed..160c5a3c 100644 --- a/lib/cpp/src/transport/TSocketPool.cpp +++ b/lib/cpp/src/transport/TSocketPool.cpp @@ -167,12 +167,20 @@ void TSocketPool::setCurrentServer(const shared_ptr &server) socket_ = server->socket_; } +/** + * This function throws an exception if socket open fails. When socket + * opens fails, the socket in the current server is reset. + */ /* TODO: without apc we ignore a lot of functionality from the php version */ void TSocketPool::open() { unsigned int numServers = servers_.size(); - if (numServers == 1 && isOpen()) { - // only one server that is already connected to + if (numServers == 0) { + socket_ = -1; + throw TTransportException(TTransportException::NOT_OPEN); + } + + if (isOpen()) { return; } @@ -206,22 +214,19 @@ void TSocketPool::open() { for (int j = 0; j < numRetries_; ++j) { try { TSocket::open(); - - // Copy over the opened socket so that we can keep it persistent - server->socket_ = socket_; - - // reset lastFailTime_ is required - if (server->lastFailTime_) { - server->lastFailTime_ = 0; - } - - // success - return; } catch (TException e) { string errStr = "TSocketPool::open failed "+getSocketInfo()+": "+e.what(); GlobalOutput(errStr.c_str()); - // connection failed + socket_ = -1; + continue; } + + // Copy over the opened socket so that we can keep it persistent + server->socket_ = socket_; + // reset lastFailTime_ is required + server->lastFailTime_ = 0; + // success + return; } ++server->consecutiveFailures_; @@ -238,8 +243,8 @@ void TSocketPool::open() { } void TSocketPool::close() { - if (isOpen()) { - TSocket::close(); + TSocket::close(); + if (currentServer_) { currentServer_->socket_ = -1; } } -- 2.17.1