Skip to content

Commit

Permalink
Merge pull request #86 from newrelic/feature/random-ws-reconnect-inte…
Browse files Browse the repository at this point in the history
…rval

Introduced random reconnection delay in WS connection
  • Loading branch information
lovesh-ap authored Jul 27, 2023
2 parents 375a068 + 2f5be85 commit 85b3d11
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collection;
import java.util.Stack;
Expand All @@ -34,6 +35,8 @@ public class CommonUtils {
public static final String POLICY_WRITE_FAILED = "policy write failed : ";
public static final String POLICY_WRITTEN_TO_FILE = "policy written to file : ";

public static SecureRandom secureRandom = new SecureRandom();

public static boolean validateCollectorPolicyParameterSchema(AgentPolicyParameters policyParameters) {

try {
Expand Down Expand Up @@ -149,4 +152,15 @@ public static void deleteRolloverLogFiles(String fileName, int max) {
}
}
}


/**
* Generate random int between range start to end. Both inclusive.
* @param start lower bound
* @param end upper bound
* @return random int
*/
public static int generateSecureRandomBetween(int start, int end) {
return secureRandom.nextInt(end-start) + start;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ public void onClose(int code, String reason, boolean remote) {
}

if (code != CloseFrame.POLICY_VALIDATION && code != CloseFrame.NORMAL) {
WSReconnectionST.getInstance().submitNewTaskSchedule(15);
int delay = CommonUtils.generateSecureRandomBetween(5, 15);
logger.log(LogLevel.INFO, String.format(WSUtils.NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS, delay), WSReconnectionST.class.getName());
WSReconnectionST.getInstance().submitNewTaskSchedule(delay);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.newrelic.agent.security.intcodeagent.filelogging.FileLoggerThreadPool;
import com.newrelic.agent.security.intcodeagent.filelogging.LogLevel;
import com.newrelic.agent.security.intcodeagent.logging.IAgentConstants;
import com.newrelic.agent.security.intcodeagent.utils.CommonUtils;

import java.security.SecureRandom;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -31,7 +33,9 @@ public void run() {
logger.postLogMessageIfNecessary(LogLevel.SEVERE, ERROR_WHILE_WS_RECONNECTION + e.getMessage() + COLON_SEPARATOR + e.getCause(), e, WSClient.class.getName());
} finally {
if (!WSUtils.isConnected()) {
futureTask = scheduledService.schedule(runnable, 15, TimeUnit.SECONDS);
int delay = CommonUtils.generateSecureRandomBetween(5, 15);
logger.log(LogLevel.INFO, String.format(WSUtils.NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS, delay), WSReconnectionST.class.getName());
futureTask = scheduledService.schedule(runnable, delay, TimeUnit.SECONDS);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

public class WSUtils {
public static final String NEXT_WS_CONNECTION_ATTEMPT_WILL_BE_IN_S_SECONDS = "Next WS connection attempt will be in %s seconds";
private static WSUtils instance;
private static final Object lock = new Object();

Expand Down

0 comments on commit 85b3d11

Please sign in to comment.