Skip to content

Commit

Permalink
Allow to setup http2settings in client options
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Mar 13, 2016
1 parent 90dfdd9 commit f903715
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/main/asciidoc/dataobjects.adoc
Expand Up @@ -1279,6 +1279,7 @@ Set the default port to be used by this client in requests if none is provided w
+++ +++
Add an enabled cipher suite Add an enabled cipher suite
+++ +++
|[[http2Settings]]`http2Settings`|`link:dataobjects.html#Http2Settings[Http2Settings]`|-
|[[idleTimeout]]`idleTimeout`|`Number (int)`| |[[idleTimeout]]`idleTimeout`|`Number (int)`|
+++ +++
Set the idle timeout, in seconds. zero means don't timeout. Set the idle timeout, in seconds. zero means don't timeout.
Expand Down
Expand Up @@ -36,6 +36,9 @@ public static void fromJson(JsonObject json, HttpClientOptions obj) {
if (json.getValue("defaultPort") instanceof Number) { if (json.getValue("defaultPort") instanceof Number) {
obj.setDefaultPort(((Number)json.getValue("defaultPort")).intValue()); obj.setDefaultPort(((Number)json.getValue("defaultPort")).intValue());
} }
if (json.getValue("http2Settings") instanceof JsonObject) {
obj.setHttp2Settings(new io.vertx.core.http.Http2Settings((JsonObject)json.getValue("http2Settings")));
}
if (json.getValue("keepAlive") instanceof Boolean) { if (json.getValue("keepAlive") instanceof Boolean) {
obj.setKeepAlive((Boolean)json.getValue("keepAlive")); obj.setKeepAlive((Boolean)json.getValue("keepAlive"));
} }
Expand Down Expand Up @@ -73,6 +76,9 @@ public static void toJson(HttpClientOptions obj, JsonObject json) {
json.put("defaultHost", obj.getDefaultHost()); json.put("defaultHost", obj.getDefaultHost());
} }
json.put("defaultPort", obj.getDefaultPort()); json.put("defaultPort", obj.getDefaultPort());
if (obj.getHttp2Settings() != null) {
json.put("http2Settings", obj.getHttp2Settings().toJson());
}
json.put("keepAlive", obj.isKeepAlive()); json.put("keepAlive", obj.isKeepAlive());
json.put("maxChunkSize", obj.getMaxChunkSize()); json.put("maxChunkSize", obj.getMaxChunkSize());
json.put("maxPoolSize", obj.getMaxPoolSize()); json.put("maxPoolSize", obj.getMaxPoolSize());
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/vertx/core/http/HttpClientOptions.java
Expand Up @@ -111,6 +111,7 @@ public class HttpClientOptions extends ClientOptionsBase {
private int maxChunkSize; private int maxChunkSize;
private int maxWaitQueueSize; private int maxWaitQueueSize;
private HttpVersion alpnFallbackProtocolVersion; private HttpVersion alpnFallbackProtocolVersion;
private Http2Settings http2Settings;


/** /**
* Default constructor * Default constructor
Expand Down Expand Up @@ -139,6 +140,7 @@ public HttpClientOptions(HttpClientOptions other) {
this.maxChunkSize = other.maxChunkSize; this.maxChunkSize = other.maxChunkSize;
this.maxWaitQueueSize = other.maxWaitQueueSize; this.maxWaitQueueSize = other.maxWaitQueueSize;
this.alpnFallbackProtocolVersion = other.alpnFallbackProtocolVersion; this.alpnFallbackProtocolVersion = other.alpnFallbackProtocolVersion;
this.http2Settings = other.http2Settings != null ? new Http2Settings(other.http2Settings) : null;
} }


/** /**
Expand Down Expand Up @@ -522,6 +524,15 @@ public HttpClientOptions setAlpnFallbackProtocolVersion(HttpVersion alpnFallback
return this; return this;
} }


public Http2Settings getHttp2Settings() {
return http2Settings;
}

public HttpClientOptions setHttp2Settings(Http2Settings http2Settings) {
this.http2Settings = http2Settings;
return this;
}

@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/io/vertx/core/http/impl/Http2Pool.java
Expand Up @@ -409,7 +409,27 @@ protected VertxClientHandler build(Http2ConnectionDecoder decoder, Http2Connecti


public VertxClientHandler build(Http2Connection conn) { public VertxClientHandler build(Http2Connection conn) {
connection(conn); connection(conn);
initialSettings(new Http2Settings()); io.vertx.core.http.Http2Settings initialSettings = client.getOptions().getHttp2Settings();
if (initialSettings != null) {
if (initialSettings.getHeaderTableSize() != null) {
initialSettings().headerTableSize(initialSettings.getHeaderTableSize());
}
if (initialSettings.getInitialWindowSize() != null) {
initialSettings().initialWindowSize(initialSettings.getInitialWindowSize());
}
if (initialSettings.getMaxConcurrentStreams() != null) {
initialSettings().maxConcurrentStreams(initialSettings.getMaxConcurrentStreams());
}
if (initialSettings.getMaxFrameSize() != null) {
initialSettings().maxFrameSize(initialSettings.getMaxFrameSize());
}
if (initialSettings.getMaxHeaderListSize() != null) {
initialSettings().maxHeaderListSize(initialSettings.getMaxHeaderListSize());
}
if (initialSettings.getEnablePush() != null) {
initialSettings().pushEnabled(initialSettings.getEnablePush());
}
}
frameListener(new Http2EventAdapter() { frameListener(new Http2EventAdapter() {
@Override @Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception { public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
Expand Down
49 changes: 47 additions & 2 deletions src/test/java/io/vertx/test/core/Http2ClientTest.java
Expand Up @@ -23,6 +23,7 @@
import io.netty.handler.codec.http2.Http2Error; import io.netty.handler.codec.http2.Http2Error;
import io.netty.handler.codec.http2.Http2Exception; import io.netty.handler.codec.http2.Http2Exception;
import io.netty.handler.codec.http2.Http2FrameAdapter; import io.netty.handler.codec.http2.Http2FrameAdapter;
import io.netty.handler.codec.http2.Http2Settings;
import io.vertx.core.Context; import io.vertx.core.Context;
import io.vertx.core.Future; import io.vertx.core.Future;
import io.vertx.core.Handler; import io.vertx.core.Handler;
Expand Down Expand Up @@ -50,13 +51,57 @@
*/ */
public class Http2ClientTest extends Http2TestBase { public class Http2ClientTest extends Http2TestBase {


HttpClientOptions clientOptions;

@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
client = vertx.createHttpClient(new HttpClientOptions(). clientOptions = new HttpClientOptions().
setUseAlpn(true). setUseAlpn(true).
setTrustStoreOptions((JksOptions) getClientTrustOptions(Trust.JKS)). setTrustStoreOptions((JksOptions) getClientTrustOptions(Trust.JKS)).
setProtocolVersion(HttpVersion.HTTP_2)); setProtocolVersion(HttpVersion.HTTP_2);
client = vertx.createHttpClient(clientOptions);
}

@Test
public void testClientSettings() throws Exception {
Http2Settings initialSettings = randomSettings();
// Http2Settings updatedSettings = randomSettings();
// Future<Void> settingsRead = Future.future();
server.requestHandler(req -> {
io.vertx.core.http.Http2Settings settings = req.connection().clientSettings();
assertEquals(initialSettings.maxHeaderListSize(), settings.getMaxHeaderListSize());
assertEquals(initialSettings.maxFrameSize(), settings.getMaxFrameSize());
assertEquals(initialSettings.initialWindowSize(), settings.getInitialWindowSize());
assertEquals(initialSettings.maxConcurrentStreams(), settings.getMaxConcurrentStreams());
assertEquals((long) initialSettings.headerTableSize(), (long) settings.getHeaderTableSize());
/*
req.connection().clientSettingsHandler(update -> {
assertOnIOContext(ctx);
assertEquals(updatedSettings.maxHeaderListSize(), update.getMaxHeaderListSize());
assertEquals(updatedSettings.maxFrameSize(), update.getMaxFrameSize());
assertEquals(updatedSettings.initialWindowSize(), update.getInitialWindowSize());
assertEquals(updatedSettings.maxConcurrentStreams(), update.getMaxConcurrentStreams());
assertEquals((long) updatedSettings.headerTableSize(), (long) update.getHeaderTableSize());
testComplete();
});
settingsRead.complete();
*/
testComplete();
});
startServer();
client.close();
client = vertx.createHttpClient(clientOptions.setHttp2Settings(new io.vertx.core.http.Http2Settings().
setEnablePush(initialSettings.pushEnabled()).
setHeaderTableSize((int)(long)initialSettings.headerTableSize()).
setInitialWindowSize(initialSettings.initialWindowSize()).
setMaxConcurrentStreams(initialSettings.maxConcurrentStreams()).
setMaxFrameSize(initialSettings.maxFrameSize()).
setMaxHeaderListSize(initialSettings.maxHeaderListSize())));
client.getNow(4043, "localhost", "/somepath", resp -> {

});
await();
} }


@Test @Test
Expand Down
17 changes: 0 additions & 17 deletions src/test/java/io/vertx/test/core/Http2ServerTest.java
Expand Up @@ -316,23 +316,6 @@ public void testClientSettings() throws Exception {
await(); await();
} }


private Http2Settings randomSettings() {
int headerTableSize = 10 + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_HEADER_TABLE_SIZE - 10);
boolean enablePush = TestUtils.randomBoolean();
long maxConcurrentStreams = TestUtils.randomPositiveLong() % (Http2CodecUtil.MAX_CONCURRENT_STREAMS - 10);
int initialWindowSize = 10 + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_INITIAL_WINDOW_SIZE - 10);
int maxFrameSize = Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_FRAME_SIZE_UPPER_BOUND - Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
int maxHeaderListSize = 10 + TestUtils.randomPositiveInt() % (int) (Http2CodecUtil.MAX_HEADER_LIST_SIZE - 10);
Http2Settings settings = new Http2Settings();
settings.headerTableSize(headerTableSize);
settings.pushEnabled(enablePush);
settings.maxConcurrentStreams(maxConcurrentStreams);
settings.initialWindowSize(initialWindowSize);
settings.maxFrameSize(maxFrameSize);
settings.maxHeaderListSize(maxHeaderListSize);
return settings;
}

@Test @Test
public void testGet() throws Exception { public void testGet() throws Exception {
String expected = TestUtils.randomAlphaString(1000); String expected = TestUtils.randomAlphaString(1000);
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/io/vertx/test/core/Http2TestBase.java
Expand Up @@ -16,6 +16,8 @@


package io.vertx.test.core; package io.vertx.test.core;


import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2Settings;
import io.vertx.core.Context; import io.vertx.core.Context;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerOptions;
Expand Down Expand Up @@ -81,4 +83,20 @@ protected SSLContext createSSLContext() throws Exception {
return context; return context;
} }


protected Http2Settings randomSettings() {
int headerTableSize = 10 + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_HEADER_TABLE_SIZE - 10);
boolean enablePush = TestUtils.randomBoolean();
long maxConcurrentStreams = TestUtils.randomPositiveLong() % (Http2CodecUtil.MAX_CONCURRENT_STREAMS - 10);
int initialWindowSize = 10 + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_INITIAL_WINDOW_SIZE - 10);
int maxFrameSize = Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND + TestUtils.randomPositiveInt() % (Http2CodecUtil.MAX_FRAME_SIZE_UPPER_BOUND - Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND);
int maxHeaderListSize = 10 + TestUtils.randomPositiveInt() % (int) (Http2CodecUtil.MAX_HEADER_LIST_SIZE - 10);
Http2Settings settings = new Http2Settings();
settings.headerTableSize(headerTableSize);
settings.pushEnabled(enablePush);
settings.maxConcurrentStreams(maxConcurrentStreams);
settings.initialWindowSize(initialWindowSize);
settings.maxFrameSize(maxFrameSize);
settings.maxHeaderListSize(maxHeaderListSize);
return settings;
}
} }

0 comments on commit f903715

Please sign in to comment.