From fa535da0696b2c44d1775bc320bdd93e8784aba4 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Wed, 3 Jul 2019 15:36:49 +0200 Subject: [PATCH] Implement Executor for Service and SpawnHandle (#3007) * Implement Executor for Service and SpawnHandle * Update lib.rs * Fix the race condition --- substrate/core/service/src/lib.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/substrate/core/service/src/lib.rs b/substrate/core/service/src/lib.rs index cff77059fe..b5d40006d7 100644 --- a/substrate/core/service/src/lib.rs +++ b/substrate/core/service/src/lib.rs @@ -119,10 +119,17 @@ pub struct SpawnTaskHandle { sender: mpsc::UnboundedSender + Send>>, } -impl SpawnTaskHandle { - /// Spawn a task to run the given future. - pub fn spawn_task(&self, task: impl Future + Send + 'static) { - let _ = self.sender.unbounded_send(Box::new(task)); +impl Executor + Send>> for SpawnTaskHandle { + fn execute( + &self, + future: Box + Send> + ) -> Result<(), futures::future::ExecuteError + Send>>> { + if let Err(err) = self.sender.unbounded_send(future) { + let kind = futures::future::ExecuteErrorKind::Shutdown; + Err(futures::future::ExecuteError::new(kind, err.into_inner())) + } else { + Ok(()) + } } } @@ -614,6 +621,22 @@ impl Future for Service where Components: components::Co } } +impl Executor + Send>> + for Service where Components: components::Components +{ + fn execute( + &self, + future: Box + Send> + ) -> Result<(), futures::future::ExecuteError + Send>>> { + if let Err(err) = self.to_spawn_tx.unbounded_send(future) { + let kind = futures::future::ExecuteErrorKind::Shutdown; + Err(futures::future::ExecuteError::new(kind, err.into_inner())) + } else { + Ok(()) + } + } +} + /// Builds a never-ending future that continuously polls the network. /// /// The `status_sink` contain a list of senders to send a periodic network status to.