@@ -202,8 +202,7 @@ public synchronized void ensureOpen()
202
202
_open ();
203
203
}
204
204
205
- boolean _open ()
206
- throws IOException {
205
+ void _open () throws IOException {
207
206
208
207
long sleepTime = 100 ;
209
208
@@ -212,11 +211,9 @@ boolean _open()
212
211
maxAutoConnectRetryTime = _options .maxAutoConnectRetryTime ;
213
212
}
214
213
214
+ boolean successfullyConnected = false ;
215
215
final long start = System .currentTimeMillis ();
216
- while ( true ){
217
-
218
- IOException lastError = null ;
219
-
216
+ do {
220
217
try {
221
218
_socket = _options .socketFactory .createSocket ();
222
219
_socket .connect ( _addr , _options .connectTimeout );
@@ -226,30 +223,28 @@ boolean _open()
226
223
_socket .setSoTimeout ( _options .socketTimeout );
227
224
_in = new BufferedInputStream ( _socket .getInputStream () );
228
225
_out = _socket .getOutputStream ();
229
- return true ;
226
+ successfullyConnected = true ;
230
227
}
231
- catch ( IOException ioe ){
232
- lastError = new IOException ( "couldn't connect to [" + _addr + "] bc:" + ioe );
233
- _logger .log ( Level .INFO , "connect fail to : " + _addr , ioe );
228
+ catch ( IOException e ){
234
229
close ();
235
- }
236
-
237
- if ( ! _options .autoConnectRetry || ( _pool != null && ! _pool ._everWorked ) )
238
- throw lastError ;
239
-
240
- long sleptSoFar = System .currentTimeMillis () - start ;
241
230
242
- if ( sleptSoFar >= maxAutoConnectRetryTime )
243
- throw lastError ;
244
-
245
- if ( sleepTime + sleptSoFar > maxAutoConnectRetryTime )
246
- sleepTime = maxAutoConnectRetryTime - sleptSoFar ;
231
+ if (!_options .autoConnectRetry || (_pool != null && !_pool ._everWorked ))
232
+ throw e ;
247
233
248
- _logger .severe ( "going to sleep and retry. total sleep time after = " + ( sleptSoFar + sleptSoFar ) + "ms this time:" + sleepTime + "ms" );
249
- ThreadUtil .sleep ( sleepTime );
250
- sleepTime *= 2 ;
251
-
252
- }
234
+ long waitSoFar = System .currentTimeMillis () - start ;
235
+
236
+ if (waitSoFar >= maxAutoConnectRetryTime )
237
+ throw e ;
238
+
239
+ if (sleepTime + waitSoFar > maxAutoConnectRetryTime )
240
+ sleepTime = maxAutoConnectRetryTime - waitSoFar ;
241
+
242
+ _logger .log (Level .WARNING , "Exception connecting to " + serverAddress ().getHost () + ": " + e +
243
+ ". Total wait time so far is " + waitSoFar + " ms. Will retry after sleeping for " + sleepTime + " ms." );
244
+ ThreadUtil .sleep (sleepTime );
245
+ sleepTime *= 2 ;
246
+ }
247
+ } while (!successfullyConnected );
253
248
}
254
249
255
250
@ Override
0 commit comments