diff options
author | zpencer <spencerfang@google.com> | 2018-04-11 16:37:13 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-11 16:37:13 -0700 |
commit | 7c46bd93994175545d510e4a5ef308755c6a204f (patch) | |
tree | 0980bb09e7168405a63d94b5d62c26cf277f34aa /netty | |
parent | 137e759fda7f22f228fae438102840d2ca9b9376 (diff) | |
download | grpc-grpc-java-7c46bd93994175545d510e4a5ef308755c6a204f.tar.gz |
netty: prepare NettySocketSupport for internal import (#4311)
Delegate the actual heavy lifting to a helper class that can be easily swapped
at runtime.
Diffstat (limited to 'netty')
3 files changed, 62 insertions, 13 deletions
diff --git a/netty/src/main/java/io/grpc/netty/InternalNettySocketSupport.java b/netty/src/main/java/io/grpc/netty/InternalNettySocketSupport.java new file mode 100644 index 000000000..069fc3fb3 --- /dev/null +++ b/netty/src/main/java/io/grpc/netty/InternalNettySocketSupport.java @@ -0,0 +1,34 @@ +/* + * Copyright 2018, gRPC Authors 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License 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 io.grpc.netty; + +import io.grpc.Internal; + +/** + * An internal accessor. Do not use. + */ +@Internal +public final class InternalNettySocketSupport { + + public interface InternalHelper extends NettySocketSupport.Helper { } + + public static void setHelper(InternalHelper helper) { + NettySocketSupport.setHelper(helper); + } + + private InternalNettySocketSupport() {} +} diff --git a/netty/src/main/java/io/grpc/netty/NettySocketSupport.java b/netty/src/main/java/io/grpc/netty/NettySocketSupport.java index 597927b6f..e4de364a8 100644 --- a/netty/src/main/java/io/grpc/netty/NettySocketSupport.java +++ b/netty/src/main/java/io/grpc/netty/NettySocketSupport.java @@ -20,7 +20,6 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import io.grpc.internal.Channelz.TcpInfo; import io.netty.channel.Channel; -import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; @@ -28,22 +27,22 @@ import javax.annotation.Nullable; * An class for getting low level socket info. */ final class NettySocketSupport { + private static volatile Helper INSTANCE = new NettySocketHelperImpl(); - /** - * Returns the info on the socket if possible. Returns null if the info can not be discovered. - */ - @Nullable - public static NativeSocketOptions getNativeSocketOptions(Channel ch) { - // TODO(zpencer): if netty-epoll, use reflection to call EpollSocketChannel.tcpInfo() - // And/or if some other low level socket support library is available, call it now. - return null; + interface Helper { + /** + * Returns the info on the socket if possible. Returns null if the info can not be discovered. + */ + @Nullable + NativeSocketOptions getNativeSocketOptions(Channel ch); } /** * A TcpInfo and additional other info that will be turned into channelz socket options. */ public static final class NativeSocketOptions { - @Nullable public final TcpInfo tcpInfo; + @Nullable + public final TcpInfo tcpInfo; public final ImmutableMap<String, String> otherInfo; /** Creates an instance. */ @@ -52,9 +51,24 @@ final class NettySocketSupport { Map<String, String> otherInfo) { Preconditions.checkNotNull(otherInfo); this.tcpInfo = tcpInfo; - this.otherInfo = ImmutableMap.copyOf(new HashMap<String, String>(otherInfo)); + this.otherInfo = ImmutableMap.copyOf(otherInfo); } } - private NettySocketSupport() {} + public static NativeSocketOptions getNativeSocketOptions(Channel ch) { + return INSTANCE.getNativeSocketOptions(ch); + } + + static void setHelper(Helper helper) { + INSTANCE = Preconditions.checkNotNull(helper); + } + + private static final class NettySocketHelperImpl implements Helper { + @Override + public NativeSocketOptions getNativeSocketOptions(Channel ch) { + // TODO(zpencer): if netty-epoll, use reflection to call EpollSocketChannel.tcpInfo() + // And/or if some other low level socket support library is available, call it now. + return null; + } + } } diff --git a/netty/src/main/java/io/grpc/netty/Utils.java b/netty/src/main/java/io/grpc/netty/Utils.java index fc2203c9c..abd3c5dd4 100644 --- a/netty/src/main/java/io/grpc/netty/Utils.java +++ b/netty/src/main/java/io/grpc/netty/Utils.java @@ -237,7 +237,8 @@ class Utils { b.addOption(key.name(), String.valueOf(value)); } - NativeSocketOptions nativeOptions = NettySocketSupport.getNativeSocketOptions(channel); + NativeSocketOptions nativeOptions + = NettySocketSupport.getNativeSocketOptions(channel); if (nativeOptions != null) { b.setTcpInfo(nativeOptions.tcpInfo); // may be null for (Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) { |