3535import com .alipay .sofa .rpc .event .EventBus ;
3636import com .alipay .sofa .rpc .ext .Extension ;
3737import com .alipay .sofa .rpc .interceptor .ClientHeaderClientInterceptor ;
38- import com .alipay .sofa .rpc .log .Logger ;
39- import com .alipay .sofa .rpc .log .LoggerFactory ;
4038import com .alipay .sofa .rpc .message .ResponseFuture ;
4139import com .alipay .sofa .rpc .server .triple .TripleContants ;
4240import com .alipay .sofa .rpc .transport .AbstractChannel ;
6361@ Extension ("tri" )
6462public class TripleClientTransport extends ClientTransport {
6563
66- private final static Logger LOGGER = LoggerFactory .getLogger (TripleClientTransport .class );
67-
64+ /**
65+ * use clientTransportConfig.getProviderInfo() instead of
66+ */
67+ @ Deprecated
6868 protected ProviderInfo providerInfo ;
6969
70+ protected ClientTransportConfig clientTransportConfig ;
71+
7072 protected ManagedChannel channel ;
7173
7274 protected InetSocketAddress localAddress ;
@@ -76,8 +78,14 @@ public class TripleClientTransport extends ClientTransport {
7678 protected TripleInvoker tripleClientInvoker ;
7779
7880 /* <address, gRPC channels> */
81+ /**
82+ * use URL_CONNECTION_MAP instead to cache ReferenceCountManagedChannel
83+ */
84+ @ Deprecated
7985 protected final static ConcurrentMap <String , ReferenceCountManagedChannel > channelMap = new ConcurrentHashMap <>();
8086
87+ protected final static ConcurrentMap <ClientTransportConfig , ReferenceCountManagedChannel > URL_CONNECTION_MAP = new ConcurrentHashMap <>();
88+
8189 protected final Object lock = new Object ();
8290
8391 protected static int KEEP_ALIVE_INTERVAL = SofaConfigs .getOrCustomDefault (
@@ -92,6 +100,7 @@ public class TripleClientTransport extends ClientTransport {
92100 public TripleClientTransport (ClientTransportConfig transportConfig ) {
93101 super (transportConfig );
94102 providerInfo = transportConfig .getProviderInfo ();
103+ clientTransportConfig = transportConfig ;
95104 connect ();
96105 remoteAddress = InetSocketAddress .createUnresolved (providerInfo .getHost (), providerInfo .getPort ());
97106 localAddress = InetSocketAddress .createUnresolved (NetUtils .getLocalIpv4 (), 0 );// 端口不准
@@ -102,8 +111,7 @@ public void connect() {
102111 if (isAvailable ()) {
103112 return ;
104113 }
105- ProviderInfo providerInfo = transportConfig .getProviderInfo ();
106- channel = getSharedChannel (providerInfo );
114+ channel = getSharedChannel (transportConfig );
107115 tripleClientInvoker = buildClientInvoker ();
108116 }
109117
@@ -114,17 +122,13 @@ protected TripleClientInvoker buildClientInvoker() {
114122 @ Override
115123 public void disconnect () {
116124 if (channel != null ) {
117- try {
118- channel .shutdown ().awaitTermination (5 , TimeUnit .SECONDS );
119- } catch (InterruptedException e ) {
120- LOGGER .warn ("Triple channel shut down interrupted." );
121- }
125+ channel .shutdown ();
122126 if (channel .isShutdown ()) {
123127 channel = null ;
124- channelMap .remove (providerInfo . toString () );
128+ URL_CONNECTION_MAP .remove (clientTransportConfig );
125129 }
126130 } else {
127- channelMap .remove (providerInfo . toString () );
131+ URL_CONNECTION_MAP .remove (clientTransportConfig );
128132 }
129133 }
130134
@@ -251,9 +255,8 @@ public InetSocketAddress localAddress() {
251255 /**
252256 * Get shared channel connection
253257 */
254- private ReferenceCountManagedChannel getSharedChannel (ProviderInfo url ) {
255- String key = url .toString ();
256- ReferenceCountManagedChannel channel = channelMap .get (key );
258+ private ReferenceCountManagedChannel getSharedChannel (ClientTransportConfig config ) {
259+ ReferenceCountManagedChannel channel = URL_CONNECTION_MAP .get (config );
257260
258261 if (channelAvailable (channel )) {
259262 channel .incrementAndGetCount ();
@@ -263,13 +266,13 @@ private ReferenceCountManagedChannel getSharedChannel(ProviderInfo url) {
263266 }
264267
265268 synchronized (lock ) {
266- channel = channelMap .get (key );
269+ channel = URL_CONNECTION_MAP .get (config );
267270 // double check
268271 if (channelAvailable (channel )) {
269272 channel .incrementAndGetCount ();
270273 } else {
271- channel = new ReferenceCountManagedChannel (initChannel (url ));
272- channelMap .put (key , channel );
274+ channel = new ReferenceCountManagedChannel (initChannel (config . getProviderInfo () ));
275+ URL_CONNECTION_MAP .put (config , channel );
273276 }
274277 }
275278
0 commit comments