Skip to content

Commit fedf60f

Browse files
authored
feat: gracefully shutdown optimistic v3 server (#814)
## 📝 Summary Gracefully shutdown optimistic v3 server upon receiving global cancellation. Delay the server shutdown ## ✅ I have completed the following steps: * [x] Run `make lint` * [x] Run `make test` * [ ] Added tests (if applicable)
1 parent e331bcf commit fedf60f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

crates/rbuilder/src/live_builder/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ impl L1Config {
360360
chain_spec: Arc<ChainSpec>,
361361
relay_sets: Vec<RelaySet>,
362362
bid_observer: Box<dyn BidObserver + Send + Sync>,
363+
cancellation_token: CancellationToken,
363364
) -> eyre::Result<(
364365
RelaySubmitSinkFactory,
365366
Vec<MevBoostRelaySlotInfoProvider>,
@@ -395,6 +396,7 @@ impl L1Config {
395396
signing_domain,
396397
self.optimistic_v3_relay_pubkeys.clone(),
397398
BroadcastStream::from(optimistic_v3_block_rx),
399+
cancellation_token,
398400
)?;
399401

400402
optimistic_v3_config = Some(OptimisticV3Config {
@@ -1068,6 +1070,7 @@ where
10681070
base_config.chain_spec()?,
10691071
relay_sets.clone(),
10701072
bid_observer,
1073+
cancellation_token.clone(),
10711074
)?;
10721075

10731076
if !l1_config.relay_bid_scrapers.is_empty() {

crates/rbuilder/src/mev_boost/optimistic_v3.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::{
2121
time::{Duration, Instant, SystemTime},
2222
};
2323
use tokio_stream::wrappers::{errors::BroadcastStreamRecvError, BroadcastStream};
24+
use tokio_util::sync::CancellationToken;
2425
use tracing::*;
2526
use warp::{
2627
http::{header::CONTENT_TYPE, HeaderValue, StatusCode},
@@ -65,6 +66,7 @@ pub fn spawn_server(
6566
domain: B256,
6667
relay_pubkeys: HashSet<BlsPublicKey>,
6768
bid_stream: BroadcastStream<Arc<AlloySubmitBlockRequest>>,
69+
cancellation: CancellationToken,
6870
) -> eyre::Result<()> {
6971
let blocks = Arc::new(Mutex::new(LruMap::new(ByLength::new(
7072
OPTIMISTIC_V3_CACHE_SIZE_DEFAULT,
@@ -92,7 +94,16 @@ pub fn spawn_server(
9294
))
9395
.and(warp::body::bytes())
9496
.map(Handler::get_payload_v3_metered);
95-
tokio::spawn(warp::serve(path).run(address));
97+
98+
let (_, server) = warp::serve(path).bind_with_graceful_shutdown(address, async move {
99+
cancellation.cancelled().await;
100+
let now = std::time::Instant::now();
101+
info!(target: "relay_server", "Received cancellation, initiating graceful shutdown");
102+
// Sleep for 12 seconds to avoid being demoted for the current slot.
103+
tokio::time::sleep(Duration::from_secs(12)).await;
104+
info!(target: "relay_server", elapsed = ?now.elapsed(), "Graceful shutdown complete");
105+
});
106+
tokio::spawn(server);
96107
info!(target: "relay_server", %address, "Relay server listening");
97108

98109
Ok(())

0 commit comments

Comments
 (0)