-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rt-sync): add pull and merge (#556)
- Loading branch information
Showing
12 changed files
with
842 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use anyhow::{anyhow, Result}; | ||
|
||
use async_trait::async_trait; | ||
use log::debug; | ||
use tokio::sync::Mutex; | ||
|
||
use super::model::sync::{ | ||
syncer_client::SyncerClient as ProtoSyncerClient, ListChangesReply, ListChangesRequest, | ||
SetRecordReply, SetRecordRequest, | ||
}; | ||
|
||
#[async_trait] | ||
pub(crate) trait SyncerClient: Send + Sync { | ||
async fn connect(&self, connect_url: String) -> Result<()>; | ||
async fn push(&self, req: SetRecordRequest) -> Result<SetRecordReply>; | ||
async fn pull(&self, req: ListChangesRequest) -> Result<ListChangesReply>; | ||
async fn disconnect(&self) -> Result<()>; | ||
} | ||
|
||
pub(crate) struct BreezSyncerClient { | ||
inner: Mutex<Option<ProtoSyncerClient<tonic::transport::Channel>>>, | ||
} | ||
|
||
impl BreezSyncerClient { | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
inner: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SyncerClient for BreezSyncerClient { | ||
async fn connect(&self, connect_url: String) -> Result<()> { | ||
let mut client = self.inner.lock().await; | ||
*client = Some(ProtoSyncerClient::connect(connect_url.clone()).await?); | ||
debug!("Successfully connected to {connect_url}"); | ||
Ok(()) | ||
} | ||
|
||
async fn push(&self, req: SetRecordRequest) -> Result<SetRecordReply> { | ||
let Some(mut client) = self.inner.lock().await.clone() else { | ||
return Err(anyhow!("Cannot run `set_record`: client not connected")); | ||
}; | ||
Ok(client.set_record(req).await?.into_inner()) | ||
} | ||
async fn pull(&self, req: ListChangesRequest) -> Result<ListChangesReply> { | ||
let Some(mut client) = self.inner.lock().await.clone() else { | ||
return Err(anyhow!("Cannot run `list_changes`: client not connected")); | ||
}; | ||
Ok(client.list_changes(req).await?.into_inner()) | ||
} | ||
|
||
async fn disconnect(&self) -> Result<()> { | ||
let mut client = self.inner.lock().await; | ||
*client = None; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.