@@ -44,8 +44,7 @@ class ServerStateNotifier implements Runnable {
4444 private long elapsedNanosSum ;
4545 private volatile ServerDescription serverDescription ;
4646 private volatile boolean isClosed ;
47- private DBPort connection ;
48- private volatile boolean isRunning ;
47+ private volatile DBPort connection ;
4948
5049 ServerStateNotifier (final ServerAddress serverAddress , final ChangeListener <ServerDescription > serverStateListener ,
5150 final SocketSettings socketSettings , final Mongo mongo ) {
@@ -63,64 +62,53 @@ public void run() {
6362 return ;
6463 }
6564
66- synchronized (this ) {
67- if (isRunning ) {
68- return ;
69- }
70- isRunning = true ;
71- }
72-
65+ final ServerDescription currentServerDescription = serverDescription ;
66+ Throwable throwable = null ;
7367 try {
74- final ServerDescription currentServerDescription = serverDescription ;
75- Throwable throwable = null ;
68+ if (connection == null ) {
69+ connection = new DBPort (serverAddress , null , getOptions (), 0 );
70+ }
7671 try {
77- if (connection == null ) {
78- connection = new DBPort (serverAddress , null , getOptions (), 0 );
72+ serverDescription = lookupServerDescription ();
73+ } catch (IOException e ) {
74+ // in case the connection has been reset since the last run, do one retry immediately before reporting that the server is
75+ // down
76+ count = 0 ;
77+ elapsedNanosSum = 0 ;
78+ if (connection != null ) {
79+ connection .close ();
80+ connection = null ;
7981 }
82+ connection = new DBPort (serverAddress , null , getOptions (), 0 );
8083 try {
8184 serverDescription = lookupServerDescription ();
82- } catch (IOException e ) {
83- // in case the connection has been reset since the last run, do one retry immediately before reporting that the server is
84- // down
85- count = 0 ;
86- elapsedNanosSum = 0 ;
87- if (connection != null ) {
88- connection .close ();
89- connection = null ;
90- }
91- connection = new DBPort (serverAddress , null , getOptions (), 0 );
92- try {
93- serverDescription = lookupServerDescription ();
94- } catch (IOException e1 ) {
95- connection .close ();
96- connection = null ;
97- throw e1 ;
98- }
85+ } catch (IOException e1 ) {
86+ connection .close ();
87+ connection = null ;
88+ throw e1 ;
9989 }
100- } catch (Throwable t ) {
101- throwable = t ;
102- serverDescription = getUnconnectedServerDescription ();
10390 }
91+ } catch (Throwable t ) {
92+ throwable = t ;
93+ serverDescription = getUnconnectedServerDescription ();
94+ }
10495
105- if (!isClosed ) {
106- try {
107- // Note that the ServerDescription.equals method does not include the average ping time as part of the comparison,
108- // so this will not spam the logs too hard.
109- if (!currentServerDescription .equals (serverDescription )) {
110- if (throwable != null ) {
111- LOGGER .log (Level .INFO , format ("Exception in monitor thread while connecting to server %s" , serverAddress ),
112- throwable );
113- } else {
114- LOGGER .info (format ("Monitor thread successfully connected to server with description %s" , serverDescription ));
115- }
96+ if (!isClosed ) {
97+ try {
98+ // Note that the ServerDescription.equals method does not include the average ping time as part of the comparison,
99+ // so this will not spam the logs too hard.
100+ if (!currentServerDescription .equals (serverDescription )) {
101+ if (throwable != null ) {
102+ LOGGER .log (Level .INFO , format ("Exception in monitor thread while connecting to server %s" , serverAddress ),
103+ throwable );
104+ } else {
105+ LOGGER .info (format ("Monitor thread successfully connected to server with description %s" , serverDescription ));
116106 }
117- serverStateListener .stateChanged (new ChangeEvent <ServerDescription >(currentServerDescription , serverDescription ));
118- } catch (Throwable t ) {
119- LOGGER .log (Level .WARNING , "Exception in monitor thread during notification of server description state change" , t );
120107 }
108+ serverStateListener .stateChanged (new ChangeEvent <ServerDescription >(currentServerDescription , serverDescription ));
109+ } catch (Throwable t ) {
110+ LOGGER .log (Level .WARNING , "Exception in monitor thread during notification of server description state change" , t );
121111 }
122- } finally {
123- isRunning = false ;
124112 }
125113 }
126114
0 commit comments