iota_graphql_rpc/server/
system_package_task.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use iota_types::SYSTEM_PACKAGE_ADDRESSES;
6use tokio::sync::watch;
7use tokio_util::sync::CancellationToken;
8use tracing::info;
9
10use crate::data::package_resolver::PackageResolver;
11
12/// Background task responsible for evicting system packages from the package
13/// resolver's cache on epoch boundaries.
14pub(crate) struct SystemPackageTask {
15    resolver: PackageResolver,
16    epoch_rx: watch::Receiver<u64>,
17    cancel: CancellationToken,
18}
19
20impl SystemPackageTask {
21    pub(crate) fn new(
22        resolver: PackageResolver,
23        epoch_rx: watch::Receiver<u64>,
24        cancel: CancellationToken,
25    ) -> Self {
26        Self {
27            resolver,
28            epoch_rx,
29            cancel,
30        }
31    }
32
33    pub(crate) async fn run(&mut self) {
34        loop {
35            tokio::select! {
36                _ = self.cancel.cancelled() => {
37                    info!("Shutdown signal received, terminating system package eviction task");
38                    return;
39                }
40
41                _ = self.epoch_rx.changed() => {
42                    info!("Detected epoch boundary, evicting system packages from cache");
43                    self.resolver
44                        .package_store()
45                        .evict(SYSTEM_PACKAGE_ADDRESSES.iter().copied());
46                }
47            }
48        }
49    }
50}