THRIFT-1954 Allow for a separate connection timeout value
This closes #472
commit 0548c01742d8fd3a55de1d516c7911720c08debc
Author: Roshan George <roshan@arjie.com>
Date: 2015-04-30T07:07:56Z
Make it possible to use separate socket and connection timeouts
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 @@
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 @@
*
* @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 @@
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 and connection timeout.
+ *
+ * @param timeout Milliseconds timeout
+ */
+ public void setTimeout(int 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 setTimeout(int timeout) {
- timeout_ = timeout;
+ public void setSocketTimeout(int timeout) {
+ socketTimeout_ = timeout;
try {
socket_.setSoTimeout(timeout);
} catch (SocketException sx) {
@@ -179,7 +218,7 @@
}
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) {