Skip to content

Commit

Permalink
Merge pull request #46233 from mkouba/ws-next-max-frame-config
Browse files Browse the repository at this point in the history
WebSockets Next: make it possible to configure max frame size
  • Loading branch information
mkouba authored Feb 12, 2025
2 parents 660ae7a + 1f82857 commit 046dc99
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.websockets.next.test.maxframesize;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.netty.handler.codec.http.websocketx.CorruptedWebSocketFrameException;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.websockets.next.OnError;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.test.utils.WSClient;
import io.vertx.core.Vertx;
import io.vertx.core.http.WebSocketFrame;

public class MaxFrameSizeTest {

@RegisterExtension
public static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(root -> {
root.addClasses(Echo.class, WSClient.class);
})
.overrideConfigKey("quarkus.websockets-next.server.max-frame-size", "10");

@Inject
Vertx vertx;

@TestHTTPResource("/echo")
URI echoUri;

@Test
void testMaxFrameSize() throws InterruptedException, ExecutionException, TimeoutException {
WSClient client = WSClient.create(vertx).connect(echoUri);
client.socket().writeFrame(WebSocketFrame.textFrame("foo".repeat(10), false));
assertTrue(Echo.CORRUPTED_LATCH.await(5, TimeUnit.SECONDS));
}

@WebSocket(path = "/echo")
public static class Echo {

static final CountDownLatch CORRUPTED_LATCH = new CountDownLatch(1);

@OnTextMessage
String process(String message) {
return message;
}

@OnError
void onError(CorruptedWebSocketFrameException e) {
// Note that connection is automatically closed when CorruptedWebSocketFrameException is thrown
CORRUPTED_LATCH.countDown();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ public void close() {
disconnect();
}

public WebSocket socket() {
return socket.get();
}

public enum ReceiverMode {
BINARY,
TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ protected WebSocketClientOptions populateClientOptions() {
if (config.maxMessageSize().isPresent()) {
clientOptions.setMaxMessageSize(config.maxMessageSize().getAsInt());
}
if (config.maxFrameSize().isPresent()) {
clientOptions.setMaxFrameSize(config.maxFrameSize().getAsInt());
}

Optional<TlsConfiguration> maybeTlsConfiguration = TlsConfiguration.from(tlsConfigurationRegistry,
config.tlsConfigurationName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ private void customize(HttpServerOptions options) {
if (config.maxMessageSize().isPresent()) {
options.setMaxWebSocketMessageSize(config.maxMessageSize().getAsInt());
}
if (config.maxFrameSize().isPresent()) {
options.setMaxWebSocketFrameSize(config.maxFrameSize().getAsInt());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public interface WebSocketsClientRuntimeConfig {
*/
OptionalInt maxMessageSize();

/**
* The maximum size of a frame in bytes. The default values is
* {@value io.vertx.core.http.HttpClientOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZEX}.
*/
OptionalInt maxFrameSize();

/**
* The interval after which, when set, the client sends a ping message to a connected server automatically.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public interface WebSocketsServerRuntimeConfig {
*/
OptionalInt maxMessageSize();

/**
* The maximum size of a frame in bytes. The default values is
* {@value io.vertx.core.http.HttpServerOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZE}.
*/
OptionalInt maxFrameSize();

/**
* The interval after which, when set, the server sends a ping message to a connected client automatically.
* <p>
Expand Down

0 comments on commit 046dc99

Please sign in to comment.