Skip to content

Commit

Permalink
[UNDERTOW-2333] introduce WebSocket IO specific timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
baranowb authored and fl4via committed Oct 16, 2024
1 parent e577596 commit 566df6d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/io/undertow/UndertowLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,8 @@ void nodeConfigCreated(URI connectionURI, String balancer, String domain, String
@LogMessage(level = WARN)
@Message(id = 5106, value = "Content mismatch for '%s'. Expected length '%s', but was '%s'.")
void contentEntryMismatch(Object key, long indicatedSize, long written);
}

@LogMessage(level = WARN)
@Message(id = 5107, value = "Failed to set web socket timeout.")
void failedToSetWSTimeout(@Cause Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ public long writeFinal(ByteBuffer[] srcs, int offset, int length) throws IOExcep
public int writeFinal(ByteBuffer src) throws IOException {
return Conduits.writeFinalBasic(this, src);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.undertow.websockets.core;

import io.undertow.UndertowLogger;
import io.undertow.conduits.IdleTimeoutConduit;
import io.undertow.server.protocol.framed.AbstractFramedChannel;
import io.undertow.server.protocol.framed.AbstractFramedStreamSourceChannel;
Expand All @@ -28,6 +29,8 @@
import org.xnio.ChannelListeners;
import org.xnio.IoUtils;
import org.xnio.OptionMap;
import org.xnio.Options;

import io.undertow.connector.ByteBufferPool;
import io.undertow.connector.PooledByteBuffer;
import org.xnio.StreamConnection;
Expand All @@ -50,7 +53,16 @@
* @author Stuart Douglas
*/
public abstract class WebSocketChannel extends AbstractFramedChannel<WebSocketChannel, StreamSourceFrameChannel, StreamSinkFrameChannel> {

/**
* 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
* a successful read taking place, the socket's next read will throw a {@link ReadTimeoutException}.
*/
public static final String WEB_SOCKETS_READ_TIMEOUT = "io.undertow.websockets.core.read-timeout";
/**
* 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
* a successful write taking place, the socket's next write will throw a {@link WriteTimeoutException}.
*/
public static final String WEB_SOCKETS_WRITE_TIMEOUT = "io.undertow.websockets.core.write-timeout";
private final boolean client;

private final WebSocketVersion version;
Expand Down Expand Up @@ -106,6 +118,22 @@ protected WebSocketChannel(final StreamConnection connectedStreamChannel, ByteBu
this.hasReservedOpCode = extensionFunction.hasExtensionOpCode();
this.subProtocol = subProtocol;
this.peerConnections = peerConnections;
final String webSocketReadTimeout = System.getProperty(WEB_SOCKETS_READ_TIMEOUT);
if(webSocketReadTimeout != null) {
try {
this.setOption(Options.READ_TIMEOUT, Integer.parseInt(webSocketReadTimeout));
} catch (Exception e) {
UndertowLogger.ROOT_LOGGER.failedToSetWSTimeout(e);
}
}
final String webSocketWriteTimeout = System.getProperty(WEB_SOCKETS_WRITE_TIMEOUT);
if(webSocketWriteTimeout != null) {
try {
this.setOption(Options.WRITE_TIMEOUT, Integer.parseInt(webSocketWriteTimeout));
} catch (Exception e) {
UndertowLogger.ROOT_LOGGER.failedToSetWSTimeout(e);
}
}
addCloseTask(new ChannelListener<WebSocketChannel>() {
@Override
public void handleEvent(WebSocketChannel channel) {
Expand Down

0 comments on commit 566df6d

Please sign in to comment.