1616package org .tikv .common .util ;
1717
1818import io .grpc .ManagedChannel ;
19- import io .grpc .ManagedChannelBuilder ;
2019import io .grpc .netty .GrpcSslContexts ;
2120import io .grpc .netty .NettyChannelBuilder ;
2221import io .netty .handler .ssl .SslContext ;
@@ -92,17 +91,19 @@ public ManagedChannel getChannel(String addressStr, HostMapping hostMapping) {
9291 } catch (Exception e ) {
9392 throw new IllegalArgumentException ("failed to get mapped address " + address , e );
9493 }
94+
9595 // Channel should be lazy without actual connection until first call
9696 // So a coarse grain lock is ok here
97+ NettyChannelBuilder builder =
98+ NettyChannelBuilder .forAddress (mappedAddr .getHost (), mappedAddr .getPort ())
99+ .maxInboundMessageSize (maxFrameSize )
100+ .keepAliveTime (keepaliveTime , TimeUnit .SECONDS )
101+ .keepAliveTimeout (keepaliveTimeout , TimeUnit .SECONDS )
102+ .keepAliveWithoutCalls (true )
103+ .idleTimeout (60 , TimeUnit .SECONDS );
104+
97105 if (sslContextBuilder == null ) {
98- return ManagedChannelBuilder .forAddress (mappedAddr .getHost (), mappedAddr .getPort ())
99- .maxInboundMessageSize (maxFrameSize )
100- .keepAliveTime (keepaliveTime , TimeUnit .SECONDS )
101- .keepAliveTimeout (keepaliveTimeout , TimeUnit .SECONDS )
102- .keepAliveWithoutCalls (true )
103- .usePlaintext (true )
104- .idleTimeout (60 , TimeUnit .SECONDS )
105- .build ();
106+ return builder .usePlaintext (true ).build ();
106107 } else {
107108 SslContext sslContext = null ;
108109 try {
@@ -111,14 +112,7 @@ public ManagedChannel getChannel(String addressStr, HostMapping hostMapping) {
111112 logger .error ("create ssl context failed!" , e );
112113 return null ;
113114 }
114- return NettyChannelBuilder .forAddress (mappedAddr .getHost (), mappedAddr .getPort ())
115- .maxInboundMessageSize (maxFrameSize )
116- .keepAliveTime (keepaliveTime , TimeUnit .SECONDS )
117- .keepAliveTimeout (keepaliveTimeout , TimeUnit .SECONDS )
118- .keepAliveWithoutCalls (true )
119- .sslContext (sslContext )
120- .idleTimeout (60 , TimeUnit .SECONDS )
121- .build ();
115+ return builder .sslContext (sslContext ).build ();
122116 }
123117 });
124118 }
0 commit comments