Repository: thrift Updated Branches: refs/heads/master 542830271 -> 195142426
THRIFT-1954 Allow for a separate connection timeout value This closes #472 commit 0548c01742d8fd3a55de1d516c7911720c08debc Author: Roshan George <[email protected]> Date: 2015-04-30T07:07:56Z Make it possible to use separate socket and connection timeouts Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/19514242 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/19514242 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/19514242 Branch: refs/heads/master Commit: 195142426dd0fc300fae59c49a91a51b16a2ffde Parents: 5428302 Author: Roger Meier <[email protected]> Authored: Thu Apr 30 18:08:53 2015 +0200 Committer: Roger Meier <[email protected]> Committed: Thu Apr 30 18:08:53 2015 +0200 ---------------------------------------------------------------------- .../org/apache/thrift/transport/TSocket.java | 55 +++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/19514242/lib/java/src/org/apache/thrift/transport/TSocket.java ---------------------------------------------------------------------- diff --git a/lib/java/src/org/apache/thrift/transport/TSocket.java b/lib/java/src/org/apache/thrift/transport/TSocket.java index c340dd2..bc44209 100644 --- a/lib/java/src/org/apache/thrift/transport/TSocket.java +++ b/lib/java/src/org/apache/thrift/transport/TSocket.java @@ -53,9 +53,14 @@ public class TSocket extends TIOStreamTransport { private int port_ = 0; /** - * Socket timeout + * Socket timeout - read timeout on the socket */ - private int timeout_ = 0; + private int socketTimeout_ = 0; + + /** + * Connection timeout + */ + private int connectTimeout_ = 0; /** * Constructor that takes an already created socket. @@ -101,12 +106,27 @@ public class TSocket extends TIOStreamTransport { * * @param host Remote host * @param port Remote port - * @param timeout Socket timeout + * @param timeout Socket timeout and connection timeout */ public TSocket(String host, int port, int timeout) { + this(host, port, timeout, timeout); + } + + /** + * Creates a new unconnected socket that will connect to the given host + * on the given port, with a specific connection timeout and a + * specific socket timeout. + * + * @param host Remote host + * @param port Remote port + * @param socketTimeout Socket timeout + * @param connectTimeout Connection timeout + */ + public TSocket(String host, int port, int socketTimeout, int connectTimeout) { host_ = host; port_ = port; - timeout_ = timeout; + socketTimeout_ = socketTimeout; + connectTimeout_ = connectTimeout; initSocket(); } @@ -119,19 +139,38 @@ public class TSocket extends TIOStreamTransport { socket_.setSoLinger(false, 0); socket_.setTcpNoDelay(true); socket_.setKeepAlive(true); - socket_.setSoTimeout(timeout_); + socket_.setSoTimeout(socketTimeout_); } catch (SocketException sx) { LOGGER.error("Could not configure socket.", sx); } } /** - * Sets the socket timeout + * Sets the socket timeout and connection timeout. * * @param timeout Milliseconds timeout */ public void setTimeout(int timeout) { - timeout_ = timeout; + this.setConnectTimeout(timeout); + this.setSocketTimeout(timeout); + } + + /** + * Sets the time after which the connection attempt will time out + * + * @param timeout Milliseconds timeout + */ + public void setConnectTimeout(int timeout) { + connectTimeout_ = timeout; + } + + /** + * Sets the socket timeout + * + * @param timeout Milliseconds timeout + */ + public void setSocketTimeout(int timeout) { + socketTimeout_ = timeout; try { socket_.setSoTimeout(timeout); } catch (SocketException sx) { @@ -179,7 +218,7 @@ public class TSocket extends TIOStreamTransport { } try { - socket_.connect(new InetSocketAddress(host_, port_), timeout_); + socket_.connect(new InetSocketAddress(host_, port_), connectTimeout_); inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024); outputStream_ = new BufferedOutputStream(socket_.getOutputStream(), 1024); } catch (IOException iox) {
