slfan1989 commented on code in PR #1349:
URL: https://github.com/apache/ratis/pull/1349#discussion_r2820935286


##########
ratis-netty/src/main/java/org/apache/ratis/netty/client/NettyClientStreamRpc.java:
##########
@@ -191,21 +214,48 @@ public void operationComplete(ChannelFuture future) {
               if (!future.isSuccess()) {
                 scheduleReconnect(Connection.this + " failed", future.cause());
               } else {
+                resetReconnectAttempts();
                 LOG.trace("{} succeed.", Connection.this);
               }
             }
           });
     }
 
+    /**
+     * Schedules a reconnection attempt with exponential backoff and jitter.
+     *
+     * @param message description of the failure
+     * @param cause the exception that triggered reconnection (may be null)
+     */
     void scheduleReconnect(String message, Throwable cause) {
       if (isClosed()) {
         return;
       }
-      LOG.warn("{}: {}; schedule reconnecting to {} in {}", this, message, 
address, RECONNECT);
+      if (!reconnectScheduled.compareAndSet(false, true)) {
+        return;
+      }
+      // Use retry index starting at 0 so the first delay equals base sleep 
time.
+      final int attempt = reconnectAttempts.getAndIncrement();
+      final RetryPolicy.Action action = 
reconnectPolicy.handleAttemptFailure(() -> attempt);
+      if (!action.shouldRetry()) {
+        reconnectScheduled.set(false);
+        LOG.warn("{}: {}; no more retries to {} after attempt {}", this, 
message, address, attempt);
+        return;
+      }
+      final long delayMillis = Math.max(1L, 
action.getSleepTime().toLong(TimeUnit.MILLISECONDS));
+      final TimeDuration delay = TimeDuration.valueOf(delayMillis, 
TimeUnit.MILLISECONDS);

Review Comment:
   Agreed. I’ve updated the code to retrieve the delay directly from the action.



-- 
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]

Reply via email to