Skip to content

Commit 566df6d

Browse files
baranowbfl4via
authored andcommitted
[UNDERTOW-2333] introduce WebSocket IO specific timeouts
1 parent e577596 commit 566df6d

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

core/src/main/java/io/undertow/UndertowLogger.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,8 @@ void nodeConfigCreated(URI connectionURI, String balancer, String domain, String
484484
@LogMessage(level = WARN)
485485
@Message(id = 5106, value = "Content mismatch for '%s'. Expected length '%s', but was '%s'.")
486486
void contentEntryMismatch(Object key, long indicatedSize, long written);
487-
}
487+
488+
@LogMessage(level = WARN)
489+
@Message(id = 5107, value = "Failed to set web socket timeout.")
490+
void failedToSetWSTimeout(@Cause Exception e);
491+
}

core/src/main/java/io/undertow/server/handlers/cache/ResponseCachingStreamSinkConduit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ public long writeFinal(ByteBuffer[] srcs, int offset, int length) throws IOExcep
167167
public int writeFinal(ByteBuffer src) throws IOException {
168168
return Conduits.writeFinalBasic(this, src);
169169
}
170-
}
170+
}

core/src/main/java/io/undertow/websockets/core/WebSocketChannel.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package io.undertow.websockets.core;
1919

20+
import io.undertow.UndertowLogger;
2021
import io.undertow.conduits.IdleTimeoutConduit;
2122
import io.undertow.server.protocol.framed.AbstractFramedChannel;
2223
import io.undertow.server.protocol.framed.AbstractFramedStreamSourceChannel;
@@ -28,6 +29,8 @@
2829
import org.xnio.ChannelListeners;
2930
import org.xnio.IoUtils;
3031
import org.xnio.OptionMap;
32+
import org.xnio.Options;
33+
3134
import io.undertow.connector.ByteBufferPool;
3235
import io.undertow.connector.PooledByteBuffer;
3336
import org.xnio.StreamConnection;
@@ -50,7 +53,16 @@
5053
* @author Stuart Douglas
5154
*/
5255
public abstract class WebSocketChannel extends AbstractFramedChannel<WebSocketChannel, StreamSourceFrameChannel, StreamSinkFrameChannel> {
53-
56+
/**
57+
* Configure a read timeout for a web socket, in milliseconds. If its present it will override {@link org.xnio.Options.READ_TIMEOUT}. If the given amount of time elapses without
58+
* a successful read taking place, the socket's next read will throw a {@link ReadTimeoutException}.
59+
*/
60+
public static final String WEB_SOCKETS_READ_TIMEOUT = "io.undertow.websockets.core.read-timeout";
61+
/**
62+
* Configure a write timeout for a web socket, in milliseconds. If its present it will override {@link org.xnio.Options.WRITE_TIMEOUT}. If the given amount of time elapses without
63+
* a successful write taking place, the socket's next write will throw a {@link WriteTimeoutException}.
64+
*/
65+
public static final String WEB_SOCKETS_WRITE_TIMEOUT = "io.undertow.websockets.core.write-timeout";
5466
private final boolean client;
5567

5668
private final WebSocketVersion version;
@@ -106,6 +118,22 @@ protected WebSocketChannel(final StreamConnection connectedStreamChannel, ByteBu
106118
this.hasReservedOpCode = extensionFunction.hasExtensionOpCode();
107119
this.subProtocol = subProtocol;
108120
this.peerConnections = peerConnections;
121+
final String webSocketReadTimeout = System.getProperty(WEB_SOCKETS_READ_TIMEOUT);
122+
if(webSocketReadTimeout != null) {
123+
try {
124+
this.setOption(Options.READ_TIMEOUT, Integer.parseInt(webSocketReadTimeout));
125+
} catch (Exception e) {
126+
UndertowLogger.ROOT_LOGGER.failedToSetWSTimeout(e);
127+
}
128+
}
129+
final String webSocketWriteTimeout = System.getProperty(WEB_SOCKETS_WRITE_TIMEOUT);
130+
if(webSocketWriteTimeout != null) {
131+
try {
132+
this.setOption(Options.WRITE_TIMEOUT, Integer.parseInt(webSocketWriteTimeout));
133+
} catch (Exception e) {
134+
UndertowLogger.ROOT_LOGGER.failedToSetWSTimeout(e);
135+
}
136+
}
109137
addCloseTask(new ChannelListener<WebSocketChannel>() {
110138
@Override
111139
public void handleEvent(WebSocketChannel channel) {

0 commit comments

Comments
 (0)