You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2020/11/20 07:05:15 UTC

[GitHub] [rocketmq] lwclover edited a comment on issue #1108: Concurrent problems with client-side connection creation

lwclover edited a comment on issue #1108:
URL: https://github.com/apache/rocketmq/issues/1108#issuecomment-730950788


   This is like Double Check lock, my code is below:
   private Channel createChannel(final String addr) throws InterruptedException {
           ChannelWrapper cw = this.channelTables.get(addr);
           if (cw != null && cw.isOK()) {
               return cw.getChannel();
           }
   
           if (this.lockChannelTables.tryLock(LOCK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
               try {
                   cw = this.channelTables.get(addr);
                   if (cw == null) {
                       ChannelFuture channelFuture = this.bootstrap.connect(RemotingHelper.string2SocketAddress(addr));
                       log.info("createChannel: begin to connect remote host[{}] asynchronously", addr);
                       cw = new ChannelWrapper(channelFuture);
                       this.channelTables.put(addr, cw);
                   }
               } catch (Exception e) {
                   log.error("createChannel: create channel exception", e);
               } finally {
                   this.lockChannelTables.unlock();
               }
           } else {
               log.warn("createChannel: try to lock channel table, but timeout, {}ms", LOCK_TIMEOUT_MILLIS);
           }
   
           if (cw != null) {
               ChannelFuture channelFuture = cw.getChannelFuture();
               if (channelFuture.awaitUninterruptibly(this.nettyClientConfig.getConnectTimeoutMillis())) {
                   if (cw.isOK()) {
                       log.info("createChannel: connect remote host[{}] success, {}", addr, channelFuture.toString());
                       return cw.getChannel();
                   } else {
                       log.warn("createChannel: connect remote host[" + addr + "] failed, " + channelFuture.toString(), channelFuture.cause());
                   }
               } else {
                   log.warn("createChannel: connect remote host[{}] timeout {}ms, {}", addr, this.nettyClientConfig.getConnectTimeoutMillis(),
                       channelFuture.toString());
               }
           }
   
           return null;
       }


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org