@@ -44,8 +44,7 @@ class ServerStateNotifier implements Runnable {
44
44
private long elapsedNanosSum ;
45
45
private volatile ServerDescription serverDescription ;
46
46
private volatile boolean isClosed ;
47
- private DBPort connection ;
48
- private volatile boolean isRunning ;
47
+ private volatile DBPort connection ;
49
48
50
49
ServerStateNotifier (final ServerAddress serverAddress , final ChangeListener <ServerDescription > serverStateListener ,
51
50
final SocketSettings socketSettings , final Mongo mongo ) {
@@ -63,64 +62,53 @@ public void run() {
63
62
return ;
64
63
}
65
64
66
- synchronized (this ) {
67
- if (isRunning ) {
68
- return ;
69
- }
70
- isRunning = true ;
71
- }
72
-
65
+ final ServerDescription currentServerDescription = serverDescription ;
66
+ Throwable throwable = null ;
73
67
try {
74
- final ServerDescription currentServerDescription = serverDescription ;
75
- Throwable throwable = null ;
68
+ if (connection == null ) {
69
+ connection = new DBPort (serverAddress , null , getOptions (), 0 );
70
+ }
76
71
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 ;
79
81
}
82
+ connection = new DBPort (serverAddress , null , getOptions (), 0 );
80
83
try {
81
84
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 ;
99
89
}
100
- } catch (Throwable t ) {
101
- throwable = t ;
102
- serverDescription = getUnconnectedServerDescription ();
103
90
}
91
+ } catch (Throwable t ) {
92
+ throwable = t ;
93
+ serverDescription = getUnconnectedServerDescription ();
94
+ }
104
95
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 ));
116
106
}
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 );
120
107
}
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 );
121
111
}
122
- } finally {
123
- isRunning = false ;
124
112
}
125
113
}
126
114
0 commit comments