Skip to content

Commit c2005a7

Browse files
sushilamazondagnir
authored andcommitted
Allow Clients to set Socket channel option
1 parent cb25a1f commit c2005a7

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "feature",
4+
"description": "Allow clients to add Socket Channel Option"
5+
}

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/NettyNioAsyncHttpClient.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import software.amazon.awssdk.http.nio.netty.internal.RequestAdapter;
5757
import software.amazon.awssdk.http.nio.netty.internal.RequestContext;
5858
import software.amazon.awssdk.http.nio.netty.internal.RunnableRequest;
59+
import software.amazon.awssdk.http.nio.netty.internal.SdkChannelOptions;
5960
import software.amazon.awssdk.http.nio.netty.internal.SdkChannelPoolMap;
6061
import software.amazon.awssdk.http.nio.netty.internal.SharedSdkEventLoopGroup;
6162
import software.amazon.awssdk.http.nio.netty.internal.http2.HttpOrHttp2ChannelPool;
@@ -73,6 +74,7 @@ public final class NettyNioAsyncHttpClient implements SdkAsyncHttpClient {
7374
private final RequestAdapter requestAdapter = new RequestAdapter();
7475
private final SdkEventLoopGroup sdkEventLoopGroup;
7576
private final ChannelPoolMap<URI, ChannelPool> pools;
77+
private final SdkChannelOptions sdkChannelOptions;
7678
private final NettyConfiguration configuration;
7779
private final long maxStreams;
7880
private Protocol protocol;
@@ -83,6 +85,11 @@ public final class NettyNioAsyncHttpClient implements SdkAsyncHttpClient {
8385
this.maxStreams = 200;
8486
this.sdkEventLoopGroup = eventLoopGroup(builder);
8587
this.pools = createChannelPoolMap();
88+
this.sdkChannelOptions = channelOptions(builder);
89+
}
90+
91+
private SdkChannelOptions channelOptions(DefaultBuilder builder) {
92+
return builder.sdkChannelOptions;
8693
}
8794

8895
private SdkEventLoopGroup eventLoopGroup(DefaultBuilder builder) {
@@ -144,8 +151,8 @@ protected ChannelPool newPool(URI key) {
144151
.channelFactory(sdkEventLoopGroup.channelFactory())
145152
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.connectTimeoutMillis())
146153
// TODO run some performance tests with and without this.
147-
.option(ChannelOption.TCP_NODELAY, true)
148154
.remoteAddress(key.getHost(), key.getPort());
155+
sdkChannelOptions.channelOptions().forEach(bootstrap::option);
149156
AtomicReference<ChannelPool> channelPoolRef = new AtomicReference<>();
150157
ChannelPipelineInitializer handler =
151158
new ChannelPipelineInitializer(protocol, sslContext, maxStreams, channelPoolRef);
@@ -279,6 +286,16 @@ public interface Builder extends SdkAsyncHttpClient.Builder<NettyNioAsyncHttpCli
279286
* @return This builder for method chaining.
280287
*/
281288
Builder protocol(Protocol protocol);
289+
290+
/**
291+
* Add new socket channel option which will be used to create Netty Http client. This allows custom configuration
292+
* for Netty.
293+
* @param channelOption {@link ChannelOption} to set
294+
* @param value See {@link ChannelOption} to find the type of value for each option
295+
* @return This builder for method chaining.
296+
* @see SdkEventLoopGroup.Builder
297+
*/
298+
Builder putChannelOption(ChannelOption channelOption, Object value);
282299
}
283300

284301
/**
@@ -288,6 +305,9 @@ public interface Builder extends SdkAsyncHttpClient.Builder<NettyNioAsyncHttpCli
288305
private static final class DefaultBuilder implements Builder {
289306

290307
private final AttributeMap.Builder standardOptions = AttributeMap.builder();
308+
309+
private SdkChannelOptions sdkChannelOptions = new SdkChannelOptions();
310+
291311
private SdkEventLoopGroup eventLoopGroup;
292312
private SdkEventLoopGroup.Builder eventLoopGroupBuilder;
293313

@@ -423,11 +443,18 @@ public void setProtocol(Protocol protocol) {
423443
protocol(protocol);
424444
}
425445

446+
@Override
447+
public Builder putChannelOption(ChannelOption channelOption, Object value) {
448+
this.sdkChannelOptions.putOption(channelOption, value);
449+
return this;
450+
}
451+
426452
@Override
427453
public SdkAsyncHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
428454
return new NettyNioAsyncHttpClient(this, standardOptions.build()
429455
.merge(serviceDefaults)
430456
.merge(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS));
457+
431458
}
432459
}
433460
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.nio.netty.internal;
17+
18+
import io.netty.channel.ChannelOption;
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import software.amazon.awssdk.annotations.SdkProtectedApi;
23+
24+
@SdkProtectedApi
25+
public class SdkChannelOptions {
26+
27+
private Map<ChannelOption, Object> options;
28+
29+
public SdkChannelOptions() {
30+
options = new HashMap<>();
31+
options.put(ChannelOption.TCP_NODELAY, Boolean.TRUE);
32+
}
33+
34+
public <T> SdkChannelOptions putOption(ChannelOption<T> channelOption, T channelOptionValue) {
35+
channelOption.validate(channelOptionValue);
36+
options.put(channelOption, channelOptionValue);
37+
return this;
38+
}
39+
40+
public Map<ChannelOption, Object> channelOptions() {
41+
return Collections.unmodifiableMap(options);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.http.nio.netty.internal;
17+
18+
import static org.junit.Assert.assertEquals;
19+
20+
import io.netty.channel.ChannelOption;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import org.junit.Test;
24+
25+
public class SdkChannelOptionsTest {
26+
27+
@Test
28+
public void defaultSdkSocketOptionPresent() {
29+
SdkChannelOptions channelOptions = new SdkChannelOptions();
30+
31+
Map<ChannelOption, Object> expectedOptions = new HashMap<>();
32+
expectedOptions.put(ChannelOption.TCP_NODELAY, Boolean.TRUE);
33+
assertEquals(expectedOptions, channelOptions.channelOptions());
34+
}
35+
36+
@Test
37+
public void additionalSdkSocketOptionsPresent() {
38+
SdkChannelOptions channelOptions = new SdkChannelOptions();
39+
channelOptions.putOption(ChannelOption.SO_LINGER, 0);
40+
41+
Map<ChannelOption, Object> expectedOptions = new HashMap<>();
42+
expectedOptions.put(ChannelOption.TCP_NODELAY, Boolean.TRUE);
43+
expectedOptions.put(ChannelOption.SO_LINGER, 0);
44+
45+
assertEquals(expectedOptions, channelOptions.channelOptions());
46+
}
47+
}

0 commit comments

Comments
 (0)