Pil0tXia commented on code in PR #4696:
URL: https://github.com/apache/eventmesh/pull/4696#discussion_r1438121724
##########
eventmesh-sdks/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java:
##########
@@ -181,10 +181,12 @@ protected void send(Package msg) throws Exception {
protected Package io(Package msg, long timeout) throws Exception {
Object key = RequestContext.key(msg);
RequestContext context = RequestContext.context(key, msg);
- if (!contexts.containsValue(context)) {
- contexts.put(key, context);
- } else {
- LogUtils.info(log, "duplicate key : {}", key);
+ synchronized (context) {
+ if (!contexts.containsValue(context)) {
+ contexts.put(key, context);
+ } else {
+ LogUtils.info(log, "duplicate key : {}", key);
+ }
Review Comment:
When the client sends two identical messages due to network issues, the
existing logic can avoid duplicate keys. However, if the client sends two
messages with the same content but different `seq` due to business issues, the
existing logic, when running in a thread-unsafe manner, will retain the key and
message from the first received package and will not be able to output a
duplicate key log. Therefore, it is still necessary to use `putIfAbsent`.
```suggestion
RequestContext previousContext = contexts.putIfAbsent(key, context);
if (previousContext != null) {
LogUtils.info(log, "duplicate key : {}", key);
}
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]