Skip to content

Commit

Permalink
disable remote sync for server "all"
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed May 19, 2024
1 parent 941a903 commit 3303c3f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
22 changes: 20 additions & 2 deletions crates/unavi-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,37 @@ pub enum Storage {
Memory,
}

#[derive(Clone)]
pub struct StartOptions {
pub enable_remote_sync: bool,
}

impl Default for StartOptions {
fn default() -> Self {
Self {
enable_remote_sync: true,
}
}
}

#[async_recursion::async_recursion]
pub async fn start(
args: Args,
opts: StartOptions,
dwn: Arc<DWN<impl DataStore + 'static, impl MessageStore + 'static>>,
) -> Result<()> {
debug!("Processing args: {:?}", args);

match args.command {
Command::All => {
let mut opts = opts.clone();
opts.enable_remote_sync = false;

tokio::select! {
res = start(Args::parse_from(["unavi-server", "social"]), dwn.clone()) => {
res = start(Args::parse_from(["unavi-server", "social"]), opts.clone(), dwn.clone()) => {
res?;
}
res = start(Args::parse_from(["unavi-server", "world"]), dwn) => {
res = start(Args::parse_from(["unavi-server", "world"]), opts, dwn) => {
res?;
}
}
Expand Down Expand Up @@ -117,6 +134,7 @@ pub async fn start(
dwn,
port,
remote_dwn,
remote_sync: opts.enable_remote_sync,
storage,
};

Expand Down
4 changes: 2 additions & 2 deletions crates/unavi-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use surrealdb::{
Surreal,
};
use tracing::{error, Level};
use unavi_server::Storage;
use unavi_server::{StartOptions, Storage};

#[tokio::main]
async fn main() {
Expand All @@ -42,7 +42,7 @@ async fn main() {
};
let dwn = Arc::new(DWN::from(store));

if let Err(e) = unavi_server::start(args, dwn).await {
if let Err(e) = unavi_server::start(args, StartOptions::default(), dwn).await {
error!("{}", e);
};
}
11 changes: 5 additions & 6 deletions crates/unavi-server/tests/world_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dwn::{
};
use surrealdb::{engine::local::Mem, Surreal};
use tracing_test::traced_test;
use unavi_server::{Args, Command, Storage};
use unavi_server::{Args, Command, StartOptions, Storage};
use wired_social::protocols::world_host::{world_host_protocol_url, WORLD_HOST_PROTOCOL_VERSION};
use wtransport::{ClientConfig, Endpoint, VarInt};

Expand Down Expand Up @@ -52,8 +52,10 @@ async fn test_world_host() {
let store = SurrealStore::new(db).await.unwrap();
let dwn = Arc::new(DWN::from(store));

let social_task = tokio::spawn(unavi_server::start(args_social, dwn.clone()));
let world_task = tokio::spawn(unavi_server::start(args_world, dwn));
let opts = StartOptions::default();

let social_task = tokio::spawn(unavi_server::start(args_social, opts.clone(), dwn.clone()));
let world_task = tokio::spawn(unavi_server::start(args_world, opts, dwn));
tokio::time::sleep(Duration::from_secs(5)).await;

// DID document is available.
Expand All @@ -72,9 +74,6 @@ async fn test_world_host() {
.unwrap();
assert_eq!(service_endpoint, remote_dwn);

// World host synced with social server.
assert!(logs_contain("Sync successful."));

// Can query protocols and records using world host DID.
let world_host_did = format!("did:web:localhost%3A{}", port_world);

Expand Down
14 changes: 11 additions & 3 deletions crates/unavi-world-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct ServerOptions<D: DataStore, M: MessageStore> {
pub dwn: Arc<DWN<D, M>>,
pub port: u16,
pub remote_dwn: String,
pub remote_sync: bool,
pub storage: Storage,
}

Expand All @@ -43,7 +44,10 @@ pub async fn start(opts: ServerOptions<impl DataStore, impl MessageStore>) -> st
dwn: opts.dwn,
storage: opts.storage.clone(),
});
actor.add_remote(opts.remote_dwn.clone());

if opts.remote_sync {
actor.add_remote(opts.remote_dwn.clone());
}

let document = did::document::create_document(&actor, opts.remote_dwn.clone());

Expand All @@ -59,14 +63,18 @@ pub async fn start(opts: ServerOptions<impl DataStore, impl MessageStore>) -> st
tokio::time::sleep(Duration::from_secs(1)).await;

// Sync first.
sync_retry(&mut actor).await;
if opts.remote_sync {
sync_retry(&mut actor).await;
}

// Interact with DWN.
let connect_url = format!("https://{}", opts.domain);
world_host::create_world_host(&actor, &connect_url).await;

// Sync after.
sync_retry(&mut actor).await;
if opts.remote_sync {
sync_retry(&mut actor).await;
}

server.await??;

Expand Down
2 changes: 1 addition & 1 deletion crates/unavi-world-server/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn handle_connection_impl<D: DataStore + 'static, M: MessageStore + 'stati
tokio::select! {
stream = session.accept_bi() => {
let stream = stream?;
info!("Accepted bi stream");
info!("Accepted bi stream.");
tokio::task::spawn_local(
bi_stream::handle_bi_stream(new_connection.id, dwn, stream).instrument(info_span!("bi"))
);
Expand Down

0 comments on commit 3303c3f

Please sign in to comment.