From 26970c2d88adc1338f51974f00959b3c28de5490 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 27 Feb 2023 13:40:13 -0800 Subject: [PATCH] move wait on new connection --- web3_proxy/src/rpcs/many.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/web3_proxy/src/rpcs/many.rs b/web3_proxy/src/rpcs/many.rs index 44973a2a..aa7bea20 100644 --- a/web3_proxy/src/rpcs/many.rs +++ b/web3_proxy/src/rpcs/many.rs @@ -275,19 +275,20 @@ impl Web3Rpcs { while let Some(x) = spawn_handles.next().await { match x { - Ok(Ok((connection, _handle))) => { - // TODO: wait for connection to have a block by watching a channel instead of looping - while connection.head_block.read().is_none() { - sleep(Duration::from_millis(100)).await; - } - + Ok(Ok((rpc, _handle))) => { // web3 connection worked - let old_rpc = self - .by_name - .write() - .insert(connection.name.clone(), connection); + let old_rpc = self.by_name.write().insert(rpc.name.clone(), rpc.clone()); if let Some(old_rpc) = old_rpc { + if old_rpc.head_block.read().is_some() { + debug!("waiting for new {} to sync", rpc); + // TODO: wait for connection to have a block by watching a channel instead of looping + // TODO: maximum wait time or this could block things for too long + while rpc.head_block.read().is_none() { + sleep(Duration::from_millis(100)).await; + } + } + old_rpc.disconnect().await.context("disconnect old rpc")?; }