netty: push sslContext initialization into transportCreationParamsFilterFactory

This commit is contained in:
Eric Gribkoff 2017-06-19 23:20:59 -07:00 committed by GitHub
parent d3d30e1c3a
commit e01cec5c2c
1 changed files with 48 additions and 40 deletions

View File

@ -432,7 +432,6 @@ public final class NettyChannelBuilder
private final Class<? extends Channel> channelType; private final Class<? extends Channel> channelType;
private final Map<ChannelOption<?>, ?> channelOptions; private final Map<ChannelOption<?>, ?> channelOptions;
private final NegotiationType negotiationType; private final NegotiationType negotiationType;
private final SslContext sslContext;
private final EventLoopGroup group; private final EventLoopGroup group;
private final boolean usingSharedGroup; private final boolean usingSharedGroup;
private final int flowControlWindow; private final int flowControlWindow;
@ -452,23 +451,10 @@ public final class NettyChannelBuilder
this.channelType = channelType; this.channelType = channelType;
this.negotiationType = negotiationType; this.negotiationType = negotiationType;
this.channelOptions = new HashMap<ChannelOption<?>, Object>(channelOptions); this.channelOptions = new HashMap<ChannelOption<?>, Object>(channelOptions);
if (negotiationType == NegotiationType.TLS && sslContext == null) {
try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException ex) {
throw new RuntimeException(ex);
}
}
this.sslContext = sslContext;
if (transportCreationParamsFilterFactory == null) { if (transportCreationParamsFilterFactory == null) {
transportCreationParamsFilterFactory = new TransportCreationParamsFilterFactory() { transportCreationParamsFilterFactory =
@Override new DefaultNettyTransportCreationParamsFilterFactory(sslContext);
public TransportCreationParamsFilter create(
SocketAddress targetServerAddress, String authority, String userAgent) {
return new DynamicNettyTransportParams(targetServerAddress, authority, userAgent);
}
};
} }
this.transportCreationParamsFilterFactory = transportCreationParamsFilterFactory; this.transportCreationParamsFilterFactory = transportCreationParamsFilterFactory;
@ -523,38 +509,60 @@ public final class NettyChannelBuilder
} }
} }
@CheckReturnValue private final class DefaultNettyTransportCreationParamsFilterFactory
private final class DynamicNettyTransportParams implements TransportCreationParamsFilter { implements TransportCreationParamsFilterFactory {
private final SslContext sslContext;
private final SocketAddress targetServerAddress; private DefaultNettyTransportCreationParamsFilterFactory(SslContext sslContext) {
private final String authority; if (negotiationType == NegotiationType.TLS && sslContext == null) {
@Nullable private final String userAgent; try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException ex) {
throw new RuntimeException(ex);
}
}
this.sslContext = sslContext;
}
private DynamicNettyTransportParams( @Override
public TransportCreationParamsFilter create(
SocketAddress targetServerAddress, String authority, String userAgent) { SocketAddress targetServerAddress, String authority, String userAgent) {
this.targetServerAddress = targetServerAddress; return new DynamicNettyTransportParams(targetServerAddress, authority, userAgent);
this.authority = authority;
this.userAgent = userAgent;
} }
@Override @CheckReturnValue
public SocketAddress getTargetServerAddress() { private final class DynamicNettyTransportParams implements TransportCreationParamsFilter {
return targetServerAddress;
}
@Override private final SocketAddress targetServerAddress;
public String getAuthority() { private final String authority;
return authority; @Nullable private final String userAgent;
}
@Override private DynamicNettyTransportParams(
public String getUserAgent() { SocketAddress targetServerAddress, String authority, String userAgent) {
return userAgent; this.targetServerAddress = targetServerAddress;
} this.authority = authority;
this.userAgent = userAgent;
}
@Override @Override
public ProtocolNegotiator getProtocolNegotiator() { public SocketAddress getTargetServerAddress() {
return createProtocolNegotiator(authority, negotiationType, sslContext); return targetServerAddress;
}
@Override
public String getAuthority() {
return authority;
}
@Override
public String getUserAgent() {
return userAgent;
}
@Override
public ProtocolNegotiator getProtocolNegotiator() {
return createProtocolNegotiator(authority, negotiationType, sslContext);
}
} }
} }
} }