Skip to content

Commit

Permalink
address code review
Browse files Browse the repository at this point in the history
Signed-off-by: marsishandsome <marsishandsome@gmail.com>
  • Loading branch information
marsishandsome committed Sep 24, 2021
1 parent c0775f7 commit d7b60c0
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/main/java/org/tikv/common/util/ChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.tikv.common.util;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.handler.ssl.SslContext;
Expand Down Expand Up @@ -92,17 +91,19 @@ public ManagedChannel getChannel(String addressStr, HostMapping hostMapping) {
} catch (Exception e) {
throw new IllegalArgumentException("failed to get mapped address " + address, e);
}

// Channel should be lazy without actual connection until first call
// So a coarse grain lock is ok here
NettyChannelBuilder builder =
NettyChannelBuilder.forAddress(mappedAddr.getHost(), mappedAddr.getPort())
.maxInboundMessageSize(maxFrameSize)
.keepAliveTime(keepaliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepaliveTimeout, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.idleTimeout(60, TimeUnit.SECONDS);

if (sslContextBuilder == null) {
return ManagedChannelBuilder.forAddress(mappedAddr.getHost(), mappedAddr.getPort())
.maxInboundMessageSize(maxFrameSize)
.keepAliveTime(keepaliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepaliveTimeout, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.usePlaintext(true)
.idleTimeout(60, TimeUnit.SECONDS)
.build();
return builder.usePlaintext(true).build();
} else {
SslContext sslContext = null;
try {
Expand All @@ -111,14 +112,7 @@ public ManagedChannel getChannel(String addressStr, HostMapping hostMapping) {
logger.error("create ssl context failed!", e);
return null;
}
return NettyChannelBuilder.forAddress(mappedAddr.getHost(), mappedAddr.getPort())
.maxInboundMessageSize(maxFrameSize)
.keepAliveTime(keepaliveTime, TimeUnit.SECONDS)
.keepAliveTimeout(keepaliveTimeout, TimeUnit.SECONDS)
.keepAliveWithoutCalls(true)
.sslContext(sslContext)
.idleTimeout(60, TimeUnit.SECONDS)
.build();
return builder.sslContext(sslContext).build();
}
});
}
Expand Down

0 comments on commit d7b60c0

Please sign in to comment.