@@ -80,6 +80,8 @@ struct TopologyState {
80
80
servers : HashMap < ServerAddress , Arc < Server > > ,
81
81
#[ cfg( test) ]
82
82
mocked : bool ,
83
+ options : ClientOptions ,
84
+ id : ObjectId ,
83
85
}
84
86
85
87
impl Topology {
@@ -110,6 +112,8 @@ impl Topology {
110
112
servers : Default :: default ( ) ,
111
113
http_client : http_client. clone ( ) ,
112
114
mocked : true ,
115
+ id,
116
+ options : options. clone ( ) ,
113
117
} ;
114
118
115
119
let topology = Self {
@@ -183,13 +187,17 @@ impl Topology {
183
187
servers : Default :: default ( ) ,
184
188
http_client,
185
189
mocked : false ,
190
+ id,
191
+ options : options. clone ( ) ,
186
192
} ;
187
193
188
194
#[ cfg( not( test) ) ]
189
195
let topology_state = TopologyState {
190
196
description,
191
197
servers : Default :: default ( ) ,
192
198
http_client,
199
+ options : options. clone ( ) ,
200
+ id,
193
201
} ;
194
202
195
203
let state = Arc :: new ( RwLock :: new ( topology_state) ) ;
@@ -232,12 +240,6 @@ impl Topology {
232
240
233
241
pub ( crate ) fn close ( & self ) {
234
242
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
- }
241
243
}
242
244
243
245
/// Gets the addresses of the servers in the cluster.
@@ -534,6 +536,26 @@ impl Topology {
534
536
}
535
537
}
536
538
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
+
537
559
impl WeakTopology {
538
560
/// Attempts to convert the WeakTopology to a strong reference.
539
561
pub ( crate ) fn upgrade ( & self ) -> Option < Topology > {
0 commit comments