Author: markt
Date: Tue Oct 22 19:38:33 2013
New Revision: 1534752
URL: http://svn.apache.org/r1534752
Log:
Add some timouts rather for IO operations while establishing a WebSocket client
connection.
Modified:
tomcat/tc7.0.x/trunk/ (props changed)
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
tomcat/tc7.0.x/trunk/webapps/docs/web-socket-howto.xml
Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
Merged /tomcat/trunk:r1534744
Modified:
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
URL:
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java?rev=1534752&r1=1534751&r2=1534752&view=diff
==============================================================================
---
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
(original)
+++
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
Tue Oct 22 19:38:33 2013
@@ -47,6 +47,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import javax.net.ssl.SSLContext;
@@ -87,6 +88,16 @@ public class WsWebSocketContainer
"org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD";
public static final String SSL_TRUSTSTORE_PWD_DEFAULT = "changeit";
+ /**
+ * Property name to set to configure the timeout (in milliseconds) when
+ * establishing a WebSocket connection to server. The default is
+ * {@link #IO_TIMEOUT_MS_DEFAULT}.
+ */
+ public static final String IO_TIMEOUT_MS_PROPERTY =
+ "org.apache.tomcat.websocket.IO_TIMEOUT_MS";
+
+ public static final long IO_TIMEOUT_MS_DEFAULT = 5000;
+
private static final StringManager sm =
StringManager.getManager(Constants.PACKAGE_NAME);
private static final Random random = new Random();
@@ -292,30 +303,38 @@ public class WsWebSocketContainer
channel = new AsyncChannelWrapperNonSecure(socketChannel);
}
+ // Get the connection timeout
+ long timeout = IO_TIMEOUT_MS_DEFAULT;
+ String timeoutValue = (String)
clientEndpointConfiguration.getUserProperties().get(
+ IO_TIMEOUT_MS_PROPERTY);
+ if (timeoutValue != null) {
+ timeout = Long.valueOf(timeoutValue).intValue();
+ }
+
ByteBuffer response;
String subProtocol;
try {
- fConnect.get();
+ fConnect.get(timeout, TimeUnit.MILLISECONDS);
Future<Void> fHandshake = channel.handshake();
- fHandshake.get();
+ fHandshake.get(timeout, TimeUnit.MILLISECONDS);
int toWrite = request.limit();
Future<Integer> fWrite = channel.write(request);
- Integer thisWrite = fWrite.get();
+ Integer thisWrite = fWrite.get(timeout, TimeUnit.MILLISECONDS);
toWrite -= thisWrite.intValue();
while (toWrite > 0) {
fWrite = channel.write(request);
- thisWrite = fWrite.get();
+ thisWrite = fWrite.get(timeout, TimeUnit.MILLISECONDS);
toWrite -= thisWrite.intValue();
}
// Same size as the WsFrame input buffer
response = ByteBuffer.allocate(maxBinaryMessageBufferSize);
HandshakeResponse handshakeResponse =
- processResponse(response, channel);
+ processResponse(response, channel, timeout);
clientEndpointConfiguration.getConfigurator().
afterResponse(handshakeResponse);
@@ -343,6 +362,9 @@ public class WsWebSocketContainer
} catch (EOFException e) {
throw new DeploymentException(
sm.getString("wsWebSocketContainer.httpRequestFailed"), e);
+ } catch (TimeoutException e) {
+ throw new DeploymentException(
+ sm.getString("wsWebSocketContainer.httpRequestFailed"), e);
}
// Switch to WebSocket
@@ -552,10 +574,12 @@ public class WsWebSocketContainer
* @throws ExecutionException
* @throws InterruptedException
* @throws DeploymentException
+ * @throws TimeoutException
*/
private HandshakeResponse processResponse(ByteBuffer response,
- AsyncChannelWrapper channel) throws InterruptedException,
- ExecutionException, DeploymentException, EOFException {
+ AsyncChannelWrapper channel, long timeout) throws
InterruptedException,
+ ExecutionException, DeploymentException, EOFException,
+ TimeoutException {
Map<String,List<String>> headers = new HashMap<String, List<String>>();
@@ -565,7 +589,7 @@ public class WsWebSocketContainer
while (!readHeaders) {
// Blocking read
Future<Integer> read = channel.read(response);
- Integer bytesRead = read.get();
+ Integer bytesRead = read.get(timeout, TimeUnit.MILLISECONDS);
if (bytesRead.intValue() == -1) {
throw new EOFException();
}
Modified: tomcat/tc7.0.x/trunk/webapps/docs/web-socket-howto.xml
URL:
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/web-socket-howto.xml?rev=1534752&r1=1534751&r2=1534752&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/web-socket-howto.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/web-socket-howto.xml Tue Oct 22 19:38:33
2013
@@ -85,6 +85,14 @@
property to <code>true</code> but any explicit setting on the servlet
context
will always take priority.</p>
+<p>When using the WebSocket client to connect to server endpoints, the timeout
+ for IO operations while establishing the connection is controlled by the
+ <code>userProperties</code> of the provided
+ <code>javax.websocket.ClientEndpointConfig</code>. The property is
+ <code>org.apache.tomcat.websocket.IO_TIMEOUT_MS</code> and is the
+ timeout as a <code>String</code> in milliseconds. The default is 5000 (5
+ seconds).</p>
+
<p>When using the WebSocket client to connect to secure server endpoints, the
client SSL configuration is controlled by the <code>userProperties</code>
of the provided <code>javax.websocket.ClientEndpointConfig</code>. The
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]