Skip to content

Commit

Permalink
chore(runnable_task): remove async_trait usage (#2660)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
<!-- List of related issues/PRs -->
- none

## Description
<!-- List of detailed changes -->

should improve performance. breaking changes for anyone using fuel-core
as a dependency, more specifically the `RunnableTask` trait. maybe cc:
@FuelLabs/data-systems ?

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog
- [x] New behavior is reflected in tests
- [x] [The specification](https://github.com/FuelLabs/fuel-specs/)
matches the implemented behavior (link update PR if changes are needed)

### Before requesting review
- [x] I have reviewed the code myself
- [x] I have created follow-up issues caused by this PR and linked them
here

### After merging, notify other teams

[Add or remove entries as needed]

- [ ] [Rust SDK](https://github.com/FuelLabs/fuels-rs/)
- [ ] [Sway compiler](https://github.com/FuelLabs/sway/)
- [ ] [Platform
documentation](https://github.com/FuelLabs/devrel-requests/issues/new?assignees=&labels=new+request&projects=&template=NEW-REQUEST.yml&title=%5BRequest%5D%3A+)
(for out-of-organization contributors, the person merging the PR will do
this)
- [ ] Someone else?
  • Loading branch information
rymnc authored Feb 4, 2025
1 parent ea46e9f commit 6665bc4
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 32 deletions.
1 change: 0 additions & 1 deletion crates/fuel-core/src/graphql_api/api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ impl RunnableService for GraphqlService {
}
}

#[async_trait::async_trait]
impl RunnableTask for Task {
async fn run(&mut self, _: &mut StateWatcher) -> TaskNextAction {
match self.server.as_mut().await {
Expand Down
1 change: 0 additions & 1 deletion crates/fuel-core/src/graphql_api/worker_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ where
Ok(())
}

#[async_trait::async_trait]
impl<TxPool, D> RunnableTask for Task<TxPool, D>
where
TxPool: ports::worker::TxPool,
Expand Down
1 change: 0 additions & 1 deletion crates/fuel-core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ impl RunnableService for Task {
}
}

#[async_trait::async_trait]
impl RunnableTask for Task {
#[tracing::instrument(skip_all)]
async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl SharedState {
}
}

#[async_trait::async_trait]
impl RunnableTask for Task {
async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction {
tokio::select! {
Expand Down
1 change: 0 additions & 1 deletion crates/services/consensus_module/poa/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ where
}
}

#[async_trait::async_trait]
impl<T, B, I, S, PB, C> RunnableTask for MainTask<T, B, I, S, PB, C>
where
T: TransactionPool,
Expand Down
3 changes: 1 addition & 2 deletions crates/services/consensus_module/poa/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl RunnableService for SyncTask {
}
}

#[async_trait::async_trait]
impl RunnableTask for SyncTask {
async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction {
let tick: BoxFuture<tokio::time::Instant> = if let Some(timer) = &mut self.timer {
Expand Down Expand Up @@ -329,7 +328,7 @@ mod tests {
}
}

/// Helper function that creates a `SyncTask` with a given configuration
/// Helper function that creates a `SyncTask` with a given configuration
fn configure_sync_task(
min_connected_reserved_peers: usize,
connections_stream: impl IntoIterator<Item = usize>,
Expand Down
2 changes: 0 additions & 2 deletions crates/services/gas_price_service/src/v0/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
v0::algorithm::SharedV0Algorithm,
};
use anyhow::anyhow;
use async_trait::async_trait;
use fuel_core_services::{
RunnableTask,
StateWatcher,
Expand Down Expand Up @@ -137,7 +136,6 @@ where
Ok(())
}
}
#[async_trait]
impl<L2, Metadata> RunnableTask for GasPriceServiceV0<L2, Metadata>
where
L2: L2BlockSource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ where
}
}

#[async_trait::async_trait]
impl<Source> RunnableTask for DaSourceService<Source>
where
Source: DaBlockCostsSource,
Expand Down
1 change: 0 additions & 1 deletion crates/services/gas_price_service/src/v1/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ where
}
}

#[async_trait]
impl<L2, DA, AtomicStorage> RunnableTask for GasPriceServiceV1<L2, DA, AtomicStorage>
where
L2: L2BlockSource,
Expand Down
11 changes: 8 additions & 3 deletions crates/services/p2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,6 @@ where
}

// TODO: Add tests https://github.com/FuelLabs/fuel-core/issues/1275
#[async_trait::async_trait]
impl<P, V, B, T> RunnableTask for Task<P, V, B, T>
where
P: TaskP2PService + 'static,
Expand Down Expand Up @@ -1019,8 +1018,14 @@ where
tracing::error!("Failed to perform peer heartbeat reputation checks: {:?}", e);
}
}
self.next_check_time += self.heartbeat_check_interval;
TaskNextAction::Continue

if let Some(next_check_time) = self.next_check_time.checked_add(self.heartbeat_check_interval) {
self.next_check_time = next_check_time;
TaskNextAction::Continue
} else {
tracing::error!("Next check time overflowed");
TaskNextAction::Stop
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/services/relayer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ where
}
}

#[async_trait]
impl<P, D> RunnableTask for Task<P, D>
where
P: Middleware<Error = ProviderError> + 'static,
Expand Down
1 change: 0 additions & 1 deletion crates/services/shared-sequencer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ where
}
}

#[async_trait]
impl<S> RunnableTask for Task<S>
where
S: Signer + 'static,
Expand Down
22 changes: 9 additions & 13 deletions crates/services/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl From<Result<bool, anyhow::Error>> for TaskNextAction {

/// The trait is implemented by the service task and contains a single iteration of the infinity
/// loop.
#[async_trait::async_trait]
pub trait RunnableTask: Send {
/// This function should contain the main business logic of the service task. It will run until
/// the service either returns false, panics or a stop signal is received.
Expand All @@ -134,10 +133,13 @@ pub trait RunnableTask: Send {
/// `State::Started`. So first, the `run` method should return a value, and after, the service
/// will stop. If the service should react to the state change earlier, it should handle it in
/// the `run` loop on its own. See [`StateWatcher::while_started`].
async fn run(&mut self, watcher: &mut StateWatcher) -> TaskNextAction;
fn run(
&mut self,
watcher: &mut StateWatcher,
) -> impl core::future::Future<Output = TaskNextAction> + Send;

/// Gracefully shutdowns the task after the end of the execution cycle.
async fn shutdown(self) -> anyhow::Result<()>;
fn shutdown(self) -> impl core::future::Future<Output = anyhow::Result<()>> + Send;
}

/// The service runner manages the lifecycle, execution and error handling of a `RunnableService`.
Expand Down Expand Up @@ -458,7 +460,6 @@ fn panic_to_string(e: Box<dyn core::any::Any + Send>) -> String {
#[cfg(test)]
mod tests {
use super::*;
use futures::future::BoxFuture;

mockall::mock! {
Service {}
Expand All @@ -480,16 +481,11 @@ mod tests {
mockall::mock! {
Task {}

#[async_trait::async_trait]
impl RunnableTask for Task {
fn run<'_self, '_state, 'a>(
&'_self mut self,
state: &'_state mut StateWatcher
) -> BoxFuture<'a, TaskNextAction>
where
'_self: 'a,
'_state: 'a,
Self: Sync + 'a;
fn run(
&mut self,
state: &mut StateWatcher
) -> impl core::future::Future<Output = TaskNextAction> + Send;

async fn shutdown(self) -> anyhow::Result<()>;
}
Expand Down
2 changes: 0 additions & 2 deletions crates/services/sync/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ where
}
}

#[async_trait::async_trait]
impl<P, E, C> RunnableTask for SyncTask<P, E, C>
where
P: PeerToPeerPort + Send + Sync + 'static,
Expand Down Expand Up @@ -171,7 +170,6 @@ where
}
}

#[async_trait::async_trait]
impl<P, E, C> RunnableTask for ImportTask<P, E, C>
where
P: PeerToPeerPort + Send + Sync + 'static,
Expand Down
1 change: 0 additions & 1 deletion crates/services/txpool_v2/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ where
}
}

#[async_trait::async_trait]
impl<View> RunnableTask for Task<View>
where
View: TxPoolPersistentStorage,
Expand Down

0 comments on commit 6665bc4

Please sign in to comment.