-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmod.rs
More file actions
103 lines (83 loc) · 2.91 KB
/
mod.rs
File metadata and controls
103 lines (83 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::convert::Infallible;
use anyhow::Context;
use sable_network::prelude::*;
use sable_server::ServerType;
use serde::Deserialize;
use tokio::sync::{mpsc::UnboundedReceiver, Mutex};
use std::sync::Arc;
use diesel_async::{AsyncConnection, AsyncPgConnection};
mod update_handler;
#[derive(Debug, Clone, Deserialize)]
pub struct HistoryServerConfig {
pub database: String,
}
pub struct HistoryServer {
node: Arc<NetworkNode>,
history_receiver: Mutex<UnboundedReceiver<sable_network::rpc::NetworkHistoryUpdate>>,
database_connection: Mutex<AsyncPgConnection>,
}
impl ServerType for HistoryServer {
type Config = HistoryServerConfig;
type ProcessedConfig = HistoryServerConfig;
type ConfigError = Infallible;
type Saved = ();
fn validate_config(config: &Self::Config) -> Result<Self::ProcessedConfig, Self::ConfigError> {
Ok(config.clone())
}
async fn new(
config: Self::ProcessedConfig,
_tls_data: &sable_network::config::TlsData,
node: std::sync::Arc<sable_network::prelude::NetworkNode>,
history_receiver: tokio::sync::mpsc::UnboundedReceiver<
sable_network::rpc::NetworkHistoryUpdate,
>,
) -> anyhow::Result<Self> {
Ok(Self {
node,
history_receiver: Mutex::new(history_receiver),
database_connection: Mutex::new(
AsyncPgConnection::establish(&config.database)
.await
.context("Couldn't connect to database")?,
),
})
}
async fn run(
self: std::sync::Arc<Self>,
mut shutdown_channel: tokio::sync::broadcast::Receiver<sable_network::rpc::ShutdownAction>,
) {
let mut history_receiver = self.history_receiver.lock().await;
loop {
tokio::select! {
_ = shutdown_channel.recv() => { break; }
update = history_receiver.recv() =>
{
let Some(update) = update else { break; };
if let Err(error) = self.handle_history_update(update).await {
tracing::error!(?error, "Error return handling history update");
}
}
}
}
}
async fn shutdown(self) {}
async fn save(self) -> Result<Self::Saved, sable_server::ServerSaveError> {
Ok(())
}
fn restore(
_state: Self::Saved,
_node: std::sync::Arc<sable_network::prelude::NetworkNode>,
_history_receiver: tokio::sync::mpsc::UnboundedReceiver<
sable_network::rpc::NetworkHistoryUpdate,
>,
_config: &Self::ProcessedConfig,
) -> std::io::Result<Self> {
unimplemented!("history servers can't hot-upgrade");
}
fn handle_remote_command(
&self,
_request: sable_network::rpc::RemoteServerRequestType,
) -> sable_network::rpc::RemoteServerResponse {
todo!()
}
}