2024-01-03 10:24:52 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tracing;
|
|
|
|
|
2024-07-04 06:04:17 +00:00
|
|
|
pub mod auto_sync;
|
2024-01-03 10:24:52 +00:00
|
|
|
mod context;
|
|
|
|
mod controllers;
|
|
|
|
mod service;
|
|
|
|
pub mod test_util;
|
|
|
|
|
2024-08-06 06:01:57 +00:00
|
|
|
use auto_sync::{batcher_random::RandomBatcherState, batcher_serial::SerialBatcherState};
|
2024-01-03 10:24:52 +00:00
|
|
|
pub use controllers::FileSyncInfo;
|
|
|
|
use duration_str::deserialize_duration;
|
2024-08-06 06:01:57 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-01-03 10:24:52 +00:00
|
|
|
pub use service::{SyncMessage, SyncReceiver, SyncRequest, SyncResponse, SyncSender, SyncService};
|
2024-07-08 03:47:59 +00:00
|
|
|
use std::{
|
|
|
|
fmt::Debug,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2024-01-03 10:24:52 +00:00
|
|
|
|
2024-01-19 06:04:59 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Deserialize)]
|
2024-01-03 10:24:52 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub struct Config {
|
2024-08-08 08:21:50 +00:00
|
|
|
// sync service config
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub heartbeat_interval: Duration,
|
2024-01-19 06:04:59 +00:00
|
|
|
pub auto_sync_enabled: bool,
|
2024-01-03 10:24:52 +00:00
|
|
|
pub max_sync_files: usize,
|
2024-01-19 06:04:59 +00:00
|
|
|
pub sync_file_by_rpc_enabled: bool,
|
|
|
|
pub sync_file_on_announcement_enabled: bool,
|
2024-07-04 06:04:17 +00:00
|
|
|
|
2024-08-08 08:21:50 +00:00
|
|
|
// serial sync config
|
|
|
|
pub max_chunks_to_request: u64,
|
|
|
|
pub max_request_failures: usize,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_connect_timeout: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_disconnect_timeout: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_find_timeout: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_chunks_download_timeout: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_wait_outgoing_connection_timeout: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub peer_next_chunks_request_wait_timeout: Duration,
|
|
|
|
|
|
|
|
// auto sync config
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub auto_sync_idle_interval: Duration,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub auto_sync_error_interval: Duration,
|
2024-07-04 06:04:17 +00:00
|
|
|
pub max_sequential_workers: usize,
|
|
|
|
#[serde(deserialize_with = "deserialize_duration")]
|
|
|
|
pub find_peer_timeout: Duration,
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-08-08 08:21:50 +00:00
|
|
|
// sync service config
|
|
|
|
heartbeat_interval: Duration::from_secs(5),
|
2024-01-19 06:04:59 +00:00
|
|
|
auto_sync_enabled: false,
|
2024-07-08 03:47:59 +00:00
|
|
|
max_sync_files: 16,
|
2024-01-19 06:04:59 +00:00
|
|
|
sync_file_by_rpc_enabled: true,
|
|
|
|
sync_file_on_announcement_enabled: false,
|
2024-07-04 06:04:17 +00:00
|
|
|
|
2024-08-08 08:21:50 +00:00
|
|
|
// serial sync config
|
|
|
|
max_chunks_to_request: 2 * 1024,
|
|
|
|
max_request_failures: 5,
|
|
|
|
peer_connect_timeout: Duration::from_secs(5),
|
|
|
|
peer_disconnect_timeout: Duration::from_secs(5),
|
|
|
|
peer_find_timeout: Duration::from_secs(5),
|
|
|
|
peer_chunks_download_timeout: Duration::from_secs(5),
|
|
|
|
peer_wait_outgoing_connection_timeout: Duration::from_secs(10),
|
|
|
|
peer_next_chunks_request_wait_timeout: Duration::from_secs(3),
|
|
|
|
|
|
|
|
// auto sync config
|
|
|
|
auto_sync_idle_interval: Duration::from_secs(3),
|
|
|
|
auto_sync_error_interval: Duration::from_secs(10),
|
2024-07-04 06:04:17 +00:00
|
|
|
max_sequential_workers: 8,
|
|
|
|
find_peer_timeout: Duration::from_secs(10),
|
2024-01-03 10:24:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-08 03:47:59 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct InstantWrapper(Instant);
|
|
|
|
|
|
|
|
impl Debug for InstantWrapper {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "\"{} seconds ago\"", self.0.elapsed().as_secs())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Instant> for InstantWrapper {
|
|
|
|
fn from(value: Instant) -> Self {
|
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl InstantWrapper {
|
|
|
|
pub fn elapsed(&self) -> Duration {
|
|
|
|
self.0.elapsed()
|
|
|
|
}
|
|
|
|
}
|
2024-08-06 06:01:57 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SyncServiceState {
|
|
|
|
pub num_syncing: usize,
|
|
|
|
pub auto_sync_serial: Option<SerialBatcherState>,
|
|
|
|
pub auto_sync_random: Option<RandomBatcherState>,
|
|
|
|
}
|