orchestra_toolkit/session/synchronous.rs
1/* Copyright 2024-2025 LEDR Technologies Inc.
2* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
3*
4* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
5*
6* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
7*
8* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
9*
10* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
11*/
12
13use std::sync::Arc;
14
15use futures::Future;
16use tokio::runtime::Runtime;
17
18use crate::{hgtp::HGTPPool, SessionAsync, SessionConfig};
19
20use super::SessionTrait;
21
22///
23/// Synchronous AvesTerra client.
24/// Exposes the same API as [`SessionAsync`], but all methods are blocking.
25///
26/// This Session is backed by a Tokio runtime and internally uses a
27/// SessionAsync.
28///
29/// This makes it possible to switch to using the asynchronous client at any
30/// time.
31/// See the example [`examples/hybrid_sync_async.rs`] for an example of how to
32/// do that.
33///
34/// WARNING: CALLING `.call()` WILL PANIC IF USED FROM AN ASYNCHRONOUS CONTEXT.
35/// IF YOU WANT TO USE YOUR OWN ASYNCHRONOUS CONTEXT, USE [`SessionAsync`].
36/// [`Session`] WILL CREATE ITS OWN CONTEXT, AND THERE SHOULD ONLY BE ONE.
37/// (it's technically possible to have multiple, but you have to be very careful,
38/// and it's probably not what you want anyways).
39///
40/// See [`SessionAsync`] for more details.
41///
42/// You need to create the [`Session`] with [`Session::initialize`]
43///
44#[derive(Clone)]
45pub struct Session {
46 pub inner: Arc<Inner>,
47}
48
49pub struct Inner {
50 pub session: SessionAsync,
51 pub rt: Runtime,
52}
53
54impl Session {
55 pub fn initialize(config: SessionConfig) -> anyhow::Result<Self> {
56 let rt = tokio::runtime::Builder::new_multi_thread()
57 .enable_all()
58 .build()?;
59 let session = rt.block_on(SessionAsync::initialize(config))?;
60
61 Ok(Self {
62 inner: Arc::new(Inner { session, rt }),
63 })
64 }
65
66 /// Cleanly closes all connections.
67 /// This method will wait for all currently used connections to be returned
68 /// to the pool. This might take a while depending on how long is the queue
69 /// of calls waiting for a connection.
70 /// In an ideal world this would have been implemented better and the queue
71 /// of pending calls would have been cancelled, but it's not the case, sorry.
72 pub fn finalize(self) {
73 self.run_async(|s| s.finalize())
74 }
75
76 pub fn run_async<'a, Fn, Fut>(&'a self, func: Fn) -> <Fut as Future>::Output
77 where
78 Fn: FnOnce(&'a SessionAsync) -> Fut + std::marker::Send,
79 Fut: Future + Send + 'a,
80 <Fut as Future>::Output: Send,
81 {
82 let mut res = None;
83
84 self.inner.rt.block_on(async {
85 res = Some(func(&self.inner.session).await);
86 });
87 res.unwrap()
88 }
89}
90
91impl SessionTrait for Session {
92 fn get_async_session(&self) -> &SessionAsync {
93 &self.inner.session
94 }
95 fn get_socket_pool(&self) -> &HGTPPool {
96 &self.inner.session.socket_pool
97 }
98}