orchestra_toolkit/session/
asynchronous.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 futures::StreamExt;
14
15use crate::{hgtp::*, SessionConfig};
16
17use super::SessionTrait;
18
19///
20/// Asynchronous AvesTerra client
21///
22/// This is not the recommended client to use. Unless you want to use you own
23/// asynchronous context, you should use [`crate::Session`] instead.
24/// The [`crate::Session`] client is easier to use, and also provide the ability
25/// to execute asynchronous code.
26///
27/// You need to create the [`SessionAsync`] with [`SessionAsync::initialize`]
28///
29#[derive(Clone)]
30pub struct SessionAsync {
31    pub socket_pool: HGTPPool,
32}
33
34impl SessionAsync {
35    pub async fn initialize(config: SessionConfig) -> anyhow::Result<Self> {
36        Ok(Self {
37            socket_pool: HGTPPool::new(config).await?,
38        })
39    }
40
41    /// Cleanly closes all connections.
42    /// This method will wait for all currently used connections to be returned
43    /// to the pool. This might take a while depending on how long is the queue
44    /// of calls waiting for a connection.
45    /// In an ideal world this would have been implemented better and the queue
46    /// of pending calls would have been cancelled, but it's not the case, sorry.
47    pub async fn finalize(&self) {
48        let futures = (0..self.socket_pool.state().connections).map(|_| async {
49            if let Ok(mut s) = self.socket_pool.get().await {
50                s.send_bye().await;
51                s.close().await;
52            }
53        });
54
55        let stream = futures::stream::iter(futures);
56        let _: Vec<_> = stream.collect().await;
57    }
58}
59
60impl SessionTrait for SessionAsync {
61    fn get_async_session(&self) -> &SessionAsync {
62        self
63    }
64    fn get_socket_pool(&self) -> &HGTPPool {
65        &self.socket_pool
66    }
67}