refactor(indexer): auto-cancel token on Drop

This commit is contained in:
erhant 2026-07-10 17:48:44 +03:00
parent 15512a4c97
commit 0f15c60cd0
3 changed files with 8 additions and 1 deletions

View File

@ -88,7 +88,7 @@ pub async fn run_server(
#[cfg(not(feature = "mock-responses"))]
let handle = {
let service = service::IndexerService::new(config, storage_dir, shutdown)
let service = service::IndexerService::new(config, storage_dir, shutdown.child_token())
.await
.context("Failed to initialize indexer service")?;
server.start(service.into_rpc())

View File

@ -175,6 +175,10 @@ impl indexer_service_rpc::RpcServer for IndexerService {
struct SubscriptionService {
parts: ArcSwap<SubscriptionLoopParts>,
indexer: IndexerCore,
/// Cancellation token that is used to signal the subscription service to shut down.
///
/// NOTE: This will auto-cancel on `Drop`, so if your token is shared with other parts
/// use [`CancellationToken::child_token()`] instead.
shutdown: CancellationToken,
}
@ -292,6 +296,7 @@ impl SubscriptionService {
impl Drop for SubscriptionService {
fn drop(&mut self) {
self.shutdown.cancel();
self.parts.load().handle.abort();
}
}

View File

@ -24,6 +24,8 @@ async fn main() -> Result<()> {
let Args { config_path, port } = Args::parse();
// TODO: handle this cancellation token more gracefully within Sequencer service
// similar to how we do in Indexer
let cancellation_token = listen_for_shutdown_signal();
let config = sequencer_service::SequencerConfig::from_path(&config_path)?;