You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by du...@apache.org on 2021/12/20 13:01:02 UTC

[rocketmq] branch develop updated: [ISSUE 3585] [Part C] cache the result of parseChannelRemoteAddr() and eliminate this method in flame graph. (used 4.84% cpu before this commit in producer side) (#3589)

This is an automated email from the ASF dual-hosted git repository.

duhengforever pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new e4394b6  [ISSUE 3585] [Part C] cache the result of parseChannelRemoteAddr() and eliminate this method in flame graph. (used 4.84% cpu before this commit in producer side) (#3589)
e4394b6 is described below

commit e4394b6cbe682c233effc5b6c1ea7895699ad057
Author: huangli <ar...@gmail.com>
AuthorDate: Mon Dec 20 21:00:46 2021 +0800

    [ISSUE 3585] [Part C] cache the result of parseChannelRemoteAddr() and eliminate this method in flame graph. (used 4.84% cpu before this commit in producer side) (#3589)
---
 .../rocketmq/remoting/common/RemotingHelper.java       | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
index b738e17..74dfacc 100644
--- a/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
+++ b/remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java
@@ -17,6 +17,9 @@
 package org.apache.rocketmq.remoting.common;
 
 import io.netty.channel.Channel;
+import io.netty.util.Attribute;
+import io.netty.util.AttributeKey;
+
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
@@ -35,6 +38,7 @@ public class RemotingHelper {
     public static final String DEFAULT_CHARSET = "UTF-8";
 
     private static final InternalLogger log = InternalLoggerFactory.getLogger(ROCKETMQ_REMOTING);
+    private static final AttributeKey<String> REMOTE_ADDR_KEY = AttributeKey.valueOf("RemoteAddr");
 
     public static String exceptionSimpleDesc(final Throwable e) {
         StringBuilder sb = new StringBuilder();
@@ -156,6 +160,20 @@ public class RemotingHelper {
         if (null == channel) {
             return "";
         }
+        Attribute<String> att = channel.attr(REMOTE_ADDR_KEY);
+        if (att == null) {
+            // mocked in unit test
+            return parseChannelRemoteAddr0(channel);
+        }
+        String addr = att.get();
+        if (addr == null) {
+            addr = parseChannelRemoteAddr0(channel);
+            att.set(addr);
+        }
+        return addr;
+    }
+
+    private static String parseChannelRemoteAddr0(final Channel channel) {
         SocketAddress remote = channel.remoteAddress();
         final String addr = remote != null ? remote.toString() : "";