Skip to main content

Mountain/RPC/EchoAction/
EchoActionServer.rs

1#![allow(non_snake_case)]
2
3//! Singleton submission gate for every Cocoon→Mountain request. Wraps the
4//! Echo scheduler with a per-method priority lane.
5
6use std::sync::Arc;
7
8use Echo::{Scheduler::Scheduler::Scheduler, Task::Priority::Priority as EchoPriority};
9use tokio::sync::oneshot;
10
11use crate::RPC::EchoAction::{ExtensionHostRegistry, ResolveMethodPriority};
12
13#[derive(Clone)]
14pub struct Struct {
15	Registry:Arc<ExtensionHostRegistry::Struct>,
16}
17
18impl Default for Struct {
19	fn default() -> Self { Self::new() }
20}
21
22impl Struct {
23	pub fn new() -> Self { Self { Registry:Arc::new(ExtensionHostRegistry::Struct::new()) } }
24
25	/// Registry accessor so tonic handlers can pass it into per-extension
26	/// logic without threading it through the scheduler.
27	pub fn Registry(&self) -> Arc<ExtensionHostRegistry::Struct> { self.Registry.clone() }
28
29	/// Submit `Task` to the Echo scheduler on the lane chosen for `Method`,
30	/// wait for completion, and return the result.
31	pub async fn Dispatch<F, T>(&self, Scheduler:&Scheduler, Method:&str, Task:F) -> Result<T, String>
32	where
33		F: std::future::Future<Output = T> + Send + 'static,
34		T: Send + 'static, {
35		let Priority = ResolveMethodPriority::Fn(Method);
36		let (Sender, Receiver) = oneshot::channel::<T>();
37		Scheduler.Submit(
38			async move {
39				let Output = Task.await;
40				let _ = Sender.send(Output);
41			},
42			Priority,
43		);
44		Receiver
45			.await
46			.map_err(|_| "EchoAction task cancelled before completion".to_string())
47	}
48}
49
50// Allow `EchoPriority` import below to satisfy clippy unused warning when
51// the inner Scheduler import is feature-gated in future revisions.
52#[allow(dead_code)]
53fn _Priority(P:EchoPriority) -> EchoPriority { P }