Skip to content

Commit 044e94a

Browse files
committed
[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)
1 parent 4daade3 commit 044e94a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

remoting/src/main/java/org/apache/rocketmq/remoting/common/RemotingHelper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.apache.rocketmq.remoting.common;
1818

1919
import io.netty.channel.Channel;
20+
import io.netty.util.Attribute;
21+
import io.netty.util.AttributeKey;
22+
2023
import java.io.IOException;
2124
import java.net.InetSocketAddress;
2225
import java.net.SocketAddress;
@@ -34,6 +37,7 @@ public class RemotingHelper {
3437
public static final String DEFAULT_CHARSET = "UTF-8";
3538

3639
private static final InternalLogger log = InternalLoggerFactory.getLogger(ROCKETMQ_REMOTING);
40+
private static final AttributeKey<String> REMOTE_ADDR_KEY = AttributeKey.valueOf("RemoteAddr");
3741

3842
public static String exceptionSimpleDesc(final Throwable e) {
3943
StringBuilder sb = new StringBuilder();
@@ -155,6 +159,16 @@ public static String parseChannelRemoteAddr(final Channel channel) {
155159
if (null == channel) {
156160
return "";
157161
}
162+
Attribute<String> att = channel.attr(REMOTE_ADDR_KEY);
163+
String addr = att.get();
164+
if (addr == null) {
165+
addr = parseChannelRemoteAddr0(channel);
166+
att.set(addr);
167+
}
168+
return addr;
169+
}
170+
171+
private static String parseChannelRemoteAddr0(final Channel channel) {
158172
SocketAddress remote = channel.remoteAddress();
159173
final String addr = remote != null ? remote.toString() : "";
160174

0 commit comments

Comments
 (0)