You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2023/05/04 03:13:52 UTC

[eventmesh] branch master updated: [ISSUE #3466] Save and re-use this "Random" (#3784)

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

mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new c5b7cf44e [ISSUE #3466] Save and re-use this "Random" (#3784)
c5b7cf44e is described below

commit c5b7cf44e526f01016e2f05af7fde3e7c0534462
Author: Piyush <11...@users.noreply.github.com>
AuthorDate: Thu May 4 08:43:47 2023 +0530

    [ISSUE #3466] Save and re-use this "Random" (#3784)
    
    * [Enhancement]  #3466
    
    * Update TcpClient.java
    
    * Update TcpClient.java
    
    * Update TcpClient.java
    
    ---------
    
    Co-authored-by: mike_xwm <mi...@126.com>
---
 .../org/apache/eventmesh/client/tcp/common/TcpClient.java  | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java
index 96aab4fef..d904d55fb 100644
--- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java
+++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/tcp/common/TcpClient.java
@@ -24,10 +24,10 @@ import org.apache.eventmesh.common.protocol.tcp.Package;
 import org.apache.eventmesh.common.protocol.tcp.UserAgent;
 import org.apache.eventmesh.common.protocol.tcp.codec.Codec;
 
-
 import java.io.Closeable;
 import java.net.InetSocketAddress;
-import java.util.Random;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
@@ -60,7 +60,15 @@ import lombok.extern.slf4j.Slf4j;
 @Slf4j
 public abstract class TcpClient implements Closeable {
 
-    protected static final transient int CLIENTNO = (new Random()).nextInt(1000);
+    protected static transient int CLIENTNO = 0;
+    
+    static {
+        try {
+            CLIENTNO = SecureRandom.getInstanceStrong().nextInt(1000);
+        } catch (NoSuchAlgorithmException e) {
+            log.error("Failed to generate a random number!", e);
+        }
+    }
 
     protected final transient ConcurrentHashMap<Object, RequestContext> contexts = new ConcurrentHashMap<>();
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org