@@ -80,6 +80,8 @@ struct TopologyState {
8080 servers : HashMap < ServerAddress , Arc < Server > > ,
8181 #[ cfg( test) ]
8282 mocked : bool ,
83+ options : ClientOptions ,
84+ id : ObjectId ,
8385}
8486
8587impl Topology {
@@ -110,6 +112,8 @@ impl Topology {
110112 servers : Default :: default ( ) ,
111113 http_client : http_client. clone ( ) ,
112114 mocked : true ,
115+ id,
116+ options : options. clone ( ) ,
113117 } ;
114118
115119 let topology = Self {
@@ -183,13 +187,17 @@ impl Topology {
183187 servers : Default :: default ( ) ,
184188 http_client,
185189 mocked : false ,
190+ id,
191+ options : options. clone ( ) ,
186192 } ;
187193
188194 #[ cfg( not( test) ) ]
189195 let topology_state = TopologyState {
190196 description,
191197 servers : Default :: default ( ) ,
192198 http_client,
199+ options : options. clone ( ) ,
200+ id,
193201 } ;
194202
195203 let state = Arc :: new ( RwLock :: new ( topology_state) ) ;
@@ -232,12 +240,6 @@ impl Topology {
232240
233241 pub ( crate ) fn close ( & self ) {
234242 self . common . is_alive . store ( false , Ordering :: SeqCst ) ;
235- if let Some ( ref handler) = self . common . options . sdam_event_handler {
236- let event = TopologyClosedEvent {
237- topology_id : self . common . id ,
238- } ;
239- handler. handle_topology_closed_event ( event) ;
240- }
241243 }
242244
243245 /// Gets the addresses of the servers in the cluster.
@@ -534,6 +536,26 @@ impl Topology {
534536 }
535537}
536538
539+ impl Drop for TopologyState {
540+ fn drop ( & mut self ) {
541+ if let Some ( ref handler) = self . options . sdam_event_handler {
542+ if matches ! ( self . description. topology_type, TopologyType :: LoadBalanced ) {
543+ for host in self . servers . keys ( ) {
544+ let event = ServerClosedEvent {
545+ address : host. clone ( ) ,
546+ topology_id : self . id ,
547+ } ;
548+ handler. handle_server_closed_event ( event) ;
549+ }
550+ }
551+ let event = TopologyClosedEvent {
552+ topology_id : self . id ,
553+ } ;
554+ handler. handle_topology_closed_event ( event) ;
555+ }
556+ }
557+ }
558+
537559impl WeakTopology {
538560 /// Attempts to convert the WeakTopology to a strong reference.
539561 pub ( crate ) fn upgrade ( & self ) -> Option < Topology > {
0 commit comments