Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding socket resolver helper that will load the appropriate SocketChannel #293

Merged
merged 1 commit into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,21 @@
package software.amazon.awssdk.http.nio.netty;

import static io.netty.handler.ssl.SslContext.defaultClientProvider;
import static software.amazon.awssdk.http.nio.netty.internal.utils.SocketChannelResolver.resolveSocketChannelClass;
import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.pool.ChannelHealthChecker;
import io.netty.channel.pool.ChannelPool;
import io.netty.channel.pool.ChannelPoolMap;
import io.netty.channel.pool.FixedChannelPool;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.net.URI;
import java.util.Optional;
import software.amazon.awssdk.annotations.ReviewBeforeRelease;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.http.SdkHttpConfigurationOption;
import software.amazon.awssdk.http.SdkHttpRequest;
Expand All @@ -44,7 +40,6 @@
import software.amazon.awssdk.http.async.SdkHttpRequestProvider;
import software.amazon.awssdk.http.async.SdkHttpResponseHandler;
import software.amazon.awssdk.http.nio.netty.internal.ChannelPipelineInitializer;
import software.amazon.awssdk.http.nio.netty.internal.DelegatingEventLoopGroup;
import software.amazon.awssdk.http.nio.netty.internal.NettyConfiguration;
import software.amazon.awssdk.http.nio.netty.internal.NonManagedEventLoopGroup;
import software.amazon.awssdk.http.nio.netty.internal.RequestAdapter;
Expand Down Expand Up @@ -78,7 +73,7 @@ protected ChannelPool newPool(URI key) {
Bootstrap bootstrap =
new Bootstrap()
.group(group)
.channel(resolveSocketChannelClass())
.channel(resolveSocketChannelClass(group))
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configuration.connectionTimeout())
.option(ChannelOption.TCP_NODELAY, true)
.remoteAddress(key.getHost(), key.getPort());
Expand Down Expand Up @@ -117,20 +112,6 @@ public void close() {
group.shutdownGracefully();
}

/**
* Depending on the EventLoopGroup used we may need to use a different socket channel.
*/
@ReviewBeforeRelease("Perhaps we should make the customer provide both event loop group" +
"and channel in some kind of wrapper class to avoid having to do this.")
private Class<? extends Channel> resolveSocketChannelClass() {
EventLoopGroup unwrapped = group;
// Keep unwrapping until it's not a DelegatingEventLoopGroup
while (unwrapped instanceof DelegatingEventLoopGroup) {
unwrapped = ((DelegatingEventLoopGroup) unwrapped).getDelegate();
}
return unwrapped instanceof EpollEventLoopGroup ? EpollSocketChannel.class : NioSocketChannel.class;
}

private static URI poolKey(SdkHttpRequest sdkRequest) {
return invokeSafely(() -> new URI(sdkRequest.protocol(), null, sdkRequest.host(),
sdkRequest.port(), null, null, null));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.nio.netty.internal.utils;

import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely;

import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.http.nio.netty.internal.DelegatingEventLoopGroup;

public final class SocketChannelResolver {

private static final Map<String, String> KNOWN_EL_GROUPS = new HashMap<>();

static {
KNOWN_EL_GROUPS.put("io.netty.channel.kqueue.KQueueEventLoopGroup", "io.netty.channel.kqueue.KQueueSocketChannel");
KNOWN_EL_GROUPS.put("io.netty.channel.oio.OioEventLoopGroup", "io.netty.channel.socket.oio.OioSocketChannel");
}

private SocketChannelResolver() {
}

/**
* Attempts to determine the {@link Channel} class that corresponds to the given
* event loop group.
*
* @param eventLoopGroup the event loop group to determine the {@link Channel} for
* @return A {@link Channel} class for the given event loop group.
*/
public static Class<? extends Channel> resolveSocketChannelClass(EventLoopGroup eventLoopGroup) {
if (eventLoopGroup instanceof DelegatingEventLoopGroup) {
return resolveSocketChannelClass(((DelegatingEventLoopGroup) eventLoopGroup).getDelegate());
}
if (eventLoopGroup instanceof NioEventLoopGroup) {
return NioSocketChannel.class;
}
if (eventLoopGroup instanceof EpollEventLoopGroup) {
return EpollSocketChannel.class;
}
String socketFqcn = KNOWN_EL_GROUPS.get(eventLoopGroup.getClass().getName());
if (socketFqcn == null) {
throw new IllegalArgumentException("Unknown event loop group : " + eventLoopGroup.getClass());
}
return invokeSafely(() -> (Class<? extends Channel>) Class.forName(socketFqcn));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package software.amazon.awssdk.http.nio.netty.internal.utils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
import static software.amazon.awssdk.http.nio.netty.internal.utils.SocketChannelResolver.resolveSocketChannelClass;

import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;
import org.junit.Test;
import software.amazon.awssdk.http.nio.netty.internal.DelegatingEventLoopGroup;

public class SocketChannelResolverTest {

@Test
public void canDetectForStandardNioEventLoopGroup() {
assertThat(resolveSocketChannelClass(new NioEventLoopGroup())).isEqualTo(NioSocketChannel.class);
}

@Test
public void canDetectEpollEventLoopGroup() {
assumeTrue(Epoll.isAvailable());
assertThat(resolveSocketChannelClass(new EpollEventLoopGroup())).isEqualTo(EpollSocketChannel.class);
}

@Test
public void worksWithDelegateEventLoopGroups() {
assertThat(resolveSocketChannelClass(new DelegatingEventLoopGroup(new NioEventLoopGroup()) {})).isEqualTo(NioSocketChannel.class);
}

@Test
public void worksWithOioEventLoopGroup() {
assertThat(resolveSocketChannelClass(new OioEventLoopGroup())).isEqualTo(OioSocketChannel.class);
}
}