3535import java .net .URISyntaxException ;
3636import java .util .List ;
3737import java .util .concurrent .TimeUnit ;
38+ import java .util .concurrent .atomic .AtomicInteger ;
3839
3940/**
4041 * HTTP and WebSocket client implementation based on netty.io.
@@ -71,7 +72,7 @@ public class NettyHttpClient implements HttpClient, WsClient {
7172 private String wsEventsUrl ;
7273 private List <HttpParam > wsEventsParamQuery ;
7374 private WsClientConnection wsClientConnection ;
74- private int reconnectCount = 0 ;
75+ private final AtomicInteger reconnectCount = new AtomicInteger ( 0 ) ;
7576 private int maxReconnectCount = 10 ; // -1 = infinite reconnect attempts
7677 private ChannelFuture wsChannelFuture ;
7778 private ScheduledFuture <?> wsPingTimer = null ;
@@ -481,7 +482,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
481482 logger .debug ("WS connected..." );
482483 // start a ping and reset reconnect counter
483484 startPing ();
484- reconnectCount = 0 ;
485+ reconnectCount . set ( 0 ) ;
485486 if (!group .isShuttingDown ()) {
486487 group .execute (callback ::onChReadyToWrite );
487488 }
@@ -612,7 +613,7 @@ public void reconnectWs(Throwable cause) {
612613 wsPingTimer = null ;
613614 }
614615
615- if (!autoReconnect || (maxReconnectCount > -1 && reconnectCount >= maxReconnectCount )) {
616+ if (!autoReconnect || (maxReconnectCount > -1 && reconnectCount . get () >= maxReconnectCount )) {
616617 logger .warn ("Cannot connect: {} - executing failure callback" , cause .getMessage ());
617618 if (!group .isShuttingDown ()) {
618619 group .execute (() -> wsCallback .onFailure (cause ));
@@ -624,9 +625,9 @@ public void reconnectWs(Throwable cause) {
624625 if (!group .isShuttingDown ()) {
625626 // schedule reconnect after a 2,5,10 seconds
626627 long [] timeouts = {2L , 5L , 10L };
627- long timeout = reconnectCount >= timeouts .length ? timeouts [timeouts .length - 1 ] : timeouts [reconnectCount ];
628- reconnectCount ++ ;
629- logger .error ("WS Connect Error: {}, reconnecting in {} seconds... try: {}" , cause .getMessage (), timeout , reconnectCount );
628+ long timeout = reconnectCount . get () >= timeouts .length ? timeouts [timeouts .length - 1 ] : timeouts [reconnectCount . get () ];
629+ reconnectCount . incrementAndGet () ;
630+ logger .error ("WS Connect Error: {}, reconnecting in {} seconds... try: {}" , cause .getMessage (), timeout , reconnectCount . get () );
630631 shutDownGroup .schedule (() -> {
631632 try {
632633 // 1st close up
0 commit comments