@@ -226,24 +226,29 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
226
226
if self . last_eviction_time + 60 < get_epoch_time_secs ( ) {
227
227
self . last_eviction_time = get_epoch_time_secs ( ) ;
228
228
if self . replicas . len ( ) > 0 {
229
- eviction_index = Some ( thread_rng ( ) . gen :: < usize > ( ) % self . replicas . len ( ) ) ;
229
+ eviction_index = Some ( thread_rng ( ) . gen_range ( 0 .. self . replicas . len ( ) ) ) ;
230
230
}
231
231
}
232
232
233
- let mut remove_naddr = None ;
234
- for ( i, naddr) in self . replicas . iter ( ) . enumerate ( ) {
235
- if let Some ( eviction_index) = eviction_index. as_ref ( ) {
236
- if * eviction_index == i {
237
- debug ! (
238
- "{:?}: {}: don't reuse connection for replica {:?}" ,
239
- network. get_local_peer( ) ,
240
- & self . smart_contract_id,
241
- & naddr,
242
- ) ;
243
- remove_naddr = Some ( naddr. clone ( ) ) ;
244
- continue ;
245
- }
233
+ let remove_naddr = eviction_index. and_then ( |idx| {
234
+ let removed = self . replicas . iter ( ) . nth ( idx) . cloned ( ) ;
235
+ if let Some ( naddr) = removed. as_ref ( ) {
236
+ debug ! (
237
+ "{:?}: {}: don't reuse connection for replica {:?}" ,
238
+ network. get_local_peer( ) ,
239
+ & self . smart_contract_id,
240
+ & naddr,
241
+ ) ;
246
242
}
243
+ removed
244
+ } ) ;
245
+
246
+ if let Some ( naddr) = remove_naddr {
247
+ self . replicas . remove ( & naddr) ;
248
+ }
249
+
250
+ // retain the remaining replica connections
251
+ for naddr in self . replicas . iter ( ) {
247
252
if let Some ( event_id) = network. get_event_id ( & naddr. to_neighbor_key ( network) ) {
248
253
self . comms . pin_connection ( event_id) ;
249
254
debug ! (
@@ -255,9 +260,6 @@ impl<NC: NeighborComms> StackerDBSync<NC> {
255
260
) ;
256
261
}
257
262
}
258
- if let Some ( naddr) = remove_naddr. take ( ) {
259
- self . replicas . remove ( & naddr) ;
260
- }
261
263
}
262
264
263
265
// reload from config
0 commit comments