@@ -202,8 +202,7 @@ public synchronized void ensureOpen()
202202 _open ();
203203 }
204204
205- boolean _open ()
206- throws IOException {
205+ void _open () throws IOException {
207206
208207 long sleepTime = 100 ;
209208
@@ -212,11 +211,9 @@ boolean _open()
212211 maxAutoConnectRetryTime = _options .maxAutoConnectRetryTime ;
213212 }
214213
214+ boolean successfullyConnected = false ;
215215 final long start = System .currentTimeMillis ();
216- while ( true ){
217-
218- IOException lastError = null ;
219-
216+ do {
220217 try {
221218 _socket = _options .socketFactory .createSocket ();
222219 _socket .connect ( _addr , _options .connectTimeout );
@@ -226,30 +223,28 @@ boolean _open()
226223 _socket .setSoTimeout ( _options .socketTimeout );
227224 _in = new BufferedInputStream ( _socket .getInputStream () );
228225 _out = _socket .getOutputStream ();
229- return true ;
226+ successfullyConnected = true ;
230227 }
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 ){
234229 close ();
235- }
236-
237- if ( ! _options .autoConnectRetry || ( _pool != null && ! _pool ._everWorked ) )
238- throw lastError ;
239-
240- long sleptSoFar = System .currentTimeMillis () - start ;
241230
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 ;
247233
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 );
253248 }
254249
255250 @ Override
0 commit comments