JavaScript is disabled, refresh for a better experience. ambee/giterated-api

ambee/giterated-api

Git repository hosting, collaboration, and discovery for the Fediverse.

Error handling fixes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨5eaea71

⁨src/pool.rs⁩ - ⁨2069⁩ bytes
Raw
1 use std::net::SocketAddr;
2
3 use deadpool::managed::{Manager, Metrics, RecycleError, RecycleResult};
4 use futures_util::SinkExt;
5 use giterated_models::instance::Instance;
6 use tokio_tungstenite::{connect_async, tungstenite::Message};
7
8 use crate::{handshake::GiteratedConnectionHandshaker, Socket};
9
10 pub struct GiteratedConnectionPool {
11 pub target_instance: Instance,
12 pub socket_addr: Option<SocketAddr>,
13 }
14
15 #[async_trait::async_trait]
16 impl Manager for GiteratedConnectionPool {
17 type Type = Socket;
18 type Error = anyhow::Error;
19
20 async fn create(&self) -> Result<Socket, Self::Error> {
21 info!("Creating new Daemon connection");
22 let connection = connect_to(&self.target_instance, &self.socket_addr).await?;
23
24 // Handshake first!
25 let connection = GiteratedConnectionHandshaker::new(connection)
26 .handshake()
27 .await?;
28 Ok(connection)
29 }
30
31 async fn recycle(&self, socket: &mut Socket, _metrics: &Metrics) -> RecycleResult<Self::Error> {
32 match socket.send(Message::Ping(vec![])).await {
33 Ok(_) => Ok(()),
34 Err(err) => {
35 info!("Socket died!");
36 Err(RecycleError::Backend(err.into()))
37 }
38 }
39 }
40 }
41
42 async fn connect_to(
43 instance: &Instance,
44 socket_addr: &Option<SocketAddr>,
45 ) -> Result<Socket, anyhow::Error> {
46 if let Some(addr) = socket_addr {
47 info!(
48 "Connecting to {}",
49 format!("ws://{}/.giterated/daemon/", addr)
50 );
51 let (websocket, _response) =
52 connect_async(&format!("ws://{}/.giterated/daemon/", addr)).await?;
53 info!("Connection established with {}", addr);
54
55 Ok(websocket)
56 } else {
57 info!(
58 "Connecting to {}",
59 format!("wss://{}/.giterated/daemon/", instance.0)
60 );
61 let (websocket, _response) =
62 connect_async(&format!("wss://{}/.giterated/daemon/", instance.0)).await?;
63 info!("Connection established with {}", instance.0);
64
65 Ok(websocket)
66 }
67 }
68