2022-02-25 14:27:44 +00:00
|
|
|
use crate::ConflictRevisionSink;
|
2022-01-11 14:23:19 +00:00
|
|
|
use async_stream::stream;
|
2021-12-20 07:37:37 +00:00
|
|
|
use bytes::Bytes;
|
2022-03-19 08:52:28 +00:00
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-11-08 13:13:28 +00:00
|
|
|
use flowy_http_model::{
|
2022-01-20 15:51:11 +00:00
|
|
|
revision::{RevId, Revision, RevisionRange},
|
2022-01-15 03:20:28 +00:00
|
|
|
ws_data::{ClientRevisionWSData, NewDocumentUser, ServerRevisionWSData, ServerRevisionWSDataType},
|
2021-12-20 07:37:37 +00:00
|
|
|
};
|
2022-01-24 08:27:40 +00:00
|
|
|
use futures_util::{future::BoxFuture, stream::StreamExt};
|
2022-01-20 15:51:11 +00:00
|
|
|
use lib_infra::future::{BoxResultFuture, FutureResult};
|
2021-12-20 07:37:37 +00:00
|
|
|
use lib_ws::WSConnectState;
|
2022-01-23 14:33:47 +00:00
|
|
|
use std::{collections::VecDeque, convert::TryFrom, fmt::Formatter, sync::Arc};
|
2022-01-11 14:23:19 +00:00
|
|
|
use tokio::{
|
|
|
|
sync::{
|
2022-01-24 09:35:58 +00:00
|
|
|
broadcast, mpsc,
|
2022-01-12 04:40:41 +00:00
|
|
|
mpsc::{Receiver, Sender},
|
2022-01-20 15:51:11 +00:00
|
|
|
RwLock,
|
2022-01-11 14:23:19 +00:00
|
|
|
},
|
|
|
|
time::{interval, Duration},
|
|
|
|
};
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-12 04:40:41 +00:00
|
|
|
// The consumer consumes the messages pushed by the web socket.
|
2022-02-25 14:27:44 +00:00
|
|
|
pub trait RevisionWSDataStream: Send + Sync {
|
2022-01-21 13:41:24 +00:00
|
|
|
fn receive_push_revision(&self, bytes: Bytes) -> BoxResultFuture<(), FlowyError>;
|
|
|
|
fn receive_ack(&self, id: String, ty: ServerRevisionWSDataType) -> BoxResultFuture<(), FlowyError>;
|
|
|
|
fn receive_new_user_connect(&self, new_user: NewDocumentUser) -> BoxResultFuture<(), FlowyError>;
|
|
|
|
fn pull_revisions_in_range(&self, range: RevisionRange) -> BoxResultFuture<(), FlowyError>;
|
2022-01-12 04:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The sink provides the data that will be sent through the web socket to the
|
2022-11-02 02:21:10 +00:00
|
|
|
// server.
|
2022-03-04 10:11:12 +00:00
|
|
|
pub trait RevisionWebSocketSink: Send + Sync {
|
2022-01-14 07:23:21 +00:00
|
|
|
fn next(&self) -> FutureResult<Option<ClientRevisionWSData>, FlowyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type WSStateReceiver = tokio::sync::broadcast::Receiver<WSConnectState>;
|
2022-01-20 15:51:11 +00:00
|
|
|
pub trait RevisionWebSocket: Send + Sync + 'static {
|
2022-01-24 08:27:40 +00:00
|
|
|
fn send(&self, data: ClientRevisionWSData) -> BoxResultFuture<(), FlowyError>;
|
|
|
|
fn subscribe_state_changed(&self) -> BoxFuture<WSStateReceiver>;
|
2022-01-12 04:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub struct RevisionWebSocketManager {
|
2022-01-23 14:33:47 +00:00
|
|
|
pub object_name: String,
|
2022-01-14 07:23:21 +00:00
|
|
|
pub object_id: String,
|
2022-03-04 10:11:12 +00:00
|
|
|
ws_data_sink: Arc<dyn RevisionWebSocketSink>,
|
2022-02-25 14:27:44 +00:00
|
|
|
ws_data_stream: Arc<dyn RevisionWSDataStream>,
|
2022-02-18 15:04:55 +00:00
|
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
2022-01-14 07:23:21 +00:00
|
|
|
pub ws_passthrough_tx: Sender<ServerRevisionWSData>,
|
|
|
|
ws_passthrough_rx: Option<Receiver<ServerRevisionWSData>>,
|
|
|
|
pub state_passthrough_tx: broadcast::Sender<WSConnectState>,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_sync_tx: SinkStopTx,
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 14:33:47 +00:00
|
|
|
impl std::fmt::Display for RevisionWebSocketManager {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_fmt(format_args!("{}RevisionWebSocketManager", self.object_name))
|
|
|
|
}
|
|
|
|
}
|
2022-01-14 07:23:21 +00:00
|
|
|
impl RevisionWebSocketManager {
|
|
|
|
pub fn new(
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name: &str,
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: &str,
|
2022-02-18 15:04:55 +00:00
|
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
2022-03-04 10:11:12 +00:00
|
|
|
ws_data_sink: Arc<dyn RevisionWebSocketSink>,
|
2022-02-25 14:27:44 +00:00
|
|
|
ws_data_stream: Arc<dyn RevisionWSDataStream>,
|
2022-01-14 07:23:21 +00:00
|
|
|
ping_duration: Duration,
|
2022-01-11 14:23:19 +00:00
|
|
|
) -> Self {
|
2022-01-12 04:40:41 +00:00
|
|
|
let (ws_passthrough_tx, ws_passthrough_rx) = mpsc::channel(1000);
|
2022-01-11 14:23:19 +00:00
|
|
|
let (stop_sync_tx, _) = tokio::sync::broadcast::channel(2);
|
2022-01-14 07:23:21 +00:00
|
|
|
let object_id = object_id.to_string();
|
2022-01-23 14:33:47 +00:00
|
|
|
let object_name = object_name.to_string();
|
2022-01-12 04:40:41 +00:00
|
|
|
let (state_passthrough_tx, _) = broadcast::channel(2);
|
2022-01-14 07:23:21 +00:00
|
|
|
let mut manager = RevisionWebSocketManager {
|
|
|
|
object_id,
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name,
|
2022-02-25 14:27:44 +00:00
|
|
|
ws_data_sink,
|
|
|
|
ws_data_stream,
|
2022-02-18 15:04:55 +00:00
|
|
|
rev_web_socket,
|
2022-01-12 04:40:41 +00:00
|
|
|
ws_passthrough_tx,
|
|
|
|
ws_passthrough_rx: Some(ws_passthrough_rx),
|
|
|
|
state_passthrough_tx,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_sync_tx,
|
|
|
|
};
|
2022-01-14 07:23:21 +00:00
|
|
|
manager.run(ping_duration);
|
2022-01-11 14:23:19 +00:00
|
|
|
manager
|
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
fn run(&mut self, ping_duration: Duration) {
|
2022-02-25 14:27:44 +00:00
|
|
|
let ws_passthrough_rx = self.ws_passthrough_rx.take().expect("Only take once");
|
2022-01-14 07:23:21 +00:00
|
|
|
let sink = RevisionWSSink::new(
|
|
|
|
&self.object_id,
|
2022-01-23 14:33:47 +00:00
|
|
|
&self.object_name,
|
2022-02-25 14:27:44 +00:00
|
|
|
self.ws_data_sink.clone(),
|
2022-02-18 15:04:55 +00:00
|
|
|
self.rev_web_socket.clone(),
|
2022-01-11 14:23:19 +00:00
|
|
|
self.stop_sync_tx.subscribe(),
|
2022-01-14 07:23:21 +00:00
|
|
|
ping_duration,
|
2022-01-11 14:23:19 +00:00
|
|
|
);
|
2022-01-14 07:23:21 +00:00
|
|
|
let stream = RevisionWSStream::new(
|
2022-01-23 14:33:47 +00:00
|
|
|
&self.object_name,
|
2022-01-14 07:23:21 +00:00
|
|
|
&self.object_id,
|
2022-02-25 14:27:44 +00:00
|
|
|
self.ws_data_stream.clone(),
|
|
|
|
ws_passthrough_rx,
|
2022-01-11 14:23:19 +00:00
|
|
|
self.stop_sync_tx.subscribe(),
|
|
|
|
);
|
|
|
|
tokio::spawn(sink.run());
|
|
|
|
tokio::spawn(stream.run());
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
pub fn scribe_state(&self) -> broadcast::Receiver<WSConnectState> {
|
|
|
|
self.state_passthrough_tx.subscribe()
|
|
|
|
}
|
2022-01-11 14:23:19 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub fn stop(&self) {
|
2022-01-11 14:23:19 +00:00
|
|
|
if self.stop_sync_tx.send(()).is_ok() {
|
2022-01-14 07:23:21 +00:00
|
|
|
tracing::trace!("{} stop sync", self.object_id)
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
2022-02-25 14:27:44 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(self, data), err)]
|
|
|
|
pub async fn receive_ws_data(&self, data: ServerRevisionWSData) -> Result<(), FlowyError> {
|
|
|
|
let _ = self.ws_passthrough_tx.send(data).await.map_err(|e| {
|
|
|
|
let err_msg = format!("{} passthrough error: {}", self.object_id, e);
|
|
|
|
FlowyError::internal().context(err_msg)
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect_state_changed(&self, state: WSConnectState) {
|
|
|
|
match self.state_passthrough_tx.send(state) {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => tracing::error!("{}", e),
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl std::ops::Drop for RevisionWebSocketManager {
|
2022-01-24 09:35:58 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
tracing::trace!("{} was dropped", self)
|
|
|
|
}
|
2022-01-02 02:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
pub struct RevisionWSStream {
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name: String,
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: String,
|
2022-02-25 14:27:44 +00:00
|
|
|
consumer: Arc<dyn RevisionWSDataStream>,
|
2022-01-14 07:23:21 +00:00
|
|
|
ws_msg_rx: Option<mpsc::Receiver<ServerRevisionWSData>>,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_rx: Option<SinkStopRx>,
|
|
|
|
}
|
2022-01-02 02:34:42 +00:00
|
|
|
|
2022-01-23 14:33:47 +00:00
|
|
|
impl std::fmt::Display for RevisionWSStream {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_fmt(format_args!("{}RevisionWSStream", self.object_name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl std::ops::Drop for RevisionWSStream {
|
2022-01-24 09:35:58 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
tracing::trace!("{} was dropped", self)
|
|
|
|
}
|
2022-01-12 09:08:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl RevisionWSStream {
|
2022-01-11 14:23:19 +00:00
|
|
|
pub fn new(
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name: &str,
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: &str,
|
2022-02-25 14:27:44 +00:00
|
|
|
consumer: Arc<dyn RevisionWSDataStream>,
|
2022-01-14 07:23:21 +00:00
|
|
|
ws_msg_rx: mpsc::Receiver<ServerRevisionWSData>,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_rx: SinkStopRx,
|
|
|
|
) -> Self {
|
2022-01-14 07:23:21 +00:00
|
|
|
RevisionWSStream {
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name: object_name.to_string(),
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: object_id.to_owned(),
|
2022-01-11 14:23:19 +00:00
|
|
|
consumer,
|
|
|
|
ws_msg_rx: Some(ws_msg_rx),
|
|
|
|
stop_rx: Some(stop_rx),
|
2021-12-25 13:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:19 +00:00
|
|
|
pub async fn run(mut self) {
|
|
|
|
let mut receiver = self.ws_msg_rx.take().expect("Only take once");
|
|
|
|
let mut stop_rx = self.stop_rx.take().expect("Only take once");
|
2022-01-14 07:23:21 +00:00
|
|
|
let object_id = self.object_id.clone();
|
2022-01-23 14:33:47 +00:00
|
|
|
let name = format!("{}", &self);
|
2022-01-11 14:23:19 +00:00
|
|
|
let stream = stream! {
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
result = receiver.recv() => {
|
|
|
|
match result {
|
|
|
|
Some(msg) => {
|
|
|
|
yield msg
|
|
|
|
},
|
|
|
|
None => {
|
2022-01-23 14:33:47 +00:00
|
|
|
tracing::debug!("[{}]:{} loop exit", name, object_id);
|
2022-01-11 14:23:19 +00:00
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ = stop_rx.recv() => {
|
2022-01-23 14:33:47 +00:00
|
|
|
tracing::debug!("[{}]:{} loop exit", name, object_id);
|
2022-01-11 14:23:19 +00:00
|
|
|
break
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2022-01-06 04:39:32 +00:00
|
|
|
|
2022-01-11 14:23:19 +00:00
|
|
|
stream
|
|
|
|
.for_each(|msg| async {
|
|
|
|
match self.handle_message(msg).await {
|
2022-01-24 09:35:58 +00:00
|
|
|
Ok(_) => {}
|
2022-01-23 14:33:47 +00:00
|
|
|
Err(e) => tracing::error!("[{}]:{} error: {}", &self, self.object_id, e),
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
2021-12-25 13:44:45 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
async fn handle_message(&self, msg: ServerRevisionWSData) -> FlowyResult<()> {
|
2022-01-21 13:41:24 +00:00
|
|
|
let ServerRevisionWSData { object_id, ty, data } = msg;
|
2022-01-20 15:51:11 +00:00
|
|
|
let bytes = Bytes::from(data);
|
2022-01-11 14:23:19 +00:00
|
|
|
match ty {
|
2022-01-14 07:23:21 +00:00
|
|
|
ServerRevisionWSDataType::ServerPushRev => {
|
2022-01-24 13:17:31 +00:00
|
|
|
tracing::trace!("[{}]: new push revision: {}:{:?}", self, object_id, ty);
|
2022-01-11 14:23:19 +00:00
|
|
|
let _ = self.consumer.receive_push_revision(bytes).await?;
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-14 07:23:21 +00:00
|
|
|
ServerRevisionWSDataType::ServerPullRev => {
|
2022-01-11 14:23:19 +00:00
|
|
|
let range = RevisionRange::try_from(bytes)?;
|
2022-01-24 13:17:31 +00:00
|
|
|
tracing::trace!("[{}]: new pull: {}:{}-{:?}", self, object_id, range, ty);
|
2022-01-11 14:23:19 +00:00
|
|
|
let _ = self.consumer.pull_revisions_in_range(range).await?;
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-14 07:23:21 +00:00
|
|
|
ServerRevisionWSDataType::ServerAck => {
|
2022-01-11 14:23:19 +00:00
|
|
|
let rev_id = RevId::try_from(bytes).unwrap().value;
|
2022-01-24 13:17:31 +00:00
|
|
|
tracing::trace!("[{}]: new ack: {}:{}-{:?}", self, object_id, rev_id, ty);
|
2022-01-11 14:23:19 +00:00
|
|
|
let _ = self.consumer.receive_ack(rev_id.to_string(), ty).await;
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-14 07:23:21 +00:00
|
|
|
ServerRevisionWSDataType::UserConnect => {
|
2022-01-11 14:23:19 +00:00
|
|
|
let new_user = NewDocumentUser::try_from(bytes)?;
|
|
|
|
let _ = self.consumer.receive_new_user_connect(new_user).await;
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
2022-01-11 14:23:19 +00:00
|
|
|
Ok(())
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-12 04:40:41 +00:00
|
|
|
type SinkStopRx = broadcast::Receiver<()>;
|
|
|
|
type SinkStopTx = broadcast::Sender<()>;
|
2022-01-14 07:23:21 +00:00
|
|
|
pub struct RevisionWSSink {
|
2022-01-23 14:33:47 +00:00
|
|
|
object_id: String,
|
|
|
|
object_name: String,
|
2022-03-04 10:11:12 +00:00
|
|
|
provider: Arc<dyn RevisionWebSocketSink>,
|
2022-02-25 14:27:44 +00:00
|
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_rx: Option<SinkStopRx>,
|
2022-01-14 07:23:21 +00:00
|
|
|
ping_duration: Duration,
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl RevisionWSSink {
|
2022-01-11 14:23:19 +00:00
|
|
|
pub fn new(
|
2022-01-14 07:23:21 +00:00
|
|
|
object_id: &str,
|
2022-01-23 14:33:47 +00:00
|
|
|
object_name: &str,
|
2022-03-04 10:11:12 +00:00
|
|
|
provider: Arc<dyn RevisionWebSocketSink>,
|
2022-02-25 14:27:44 +00:00
|
|
|
rev_web_socket: Arc<dyn RevisionWebSocket>,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_rx: SinkStopRx,
|
2022-01-14 07:23:21 +00:00
|
|
|
ping_duration: Duration,
|
2022-01-11 14:23:19 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2022-01-23 14:33:47 +00:00
|
|
|
object_id: object_id.to_owned(),
|
|
|
|
object_name: object_name.to_owned(),
|
2022-01-11 14:23:19 +00:00
|
|
|
provider,
|
2022-02-25 14:27:44 +00:00
|
|
|
rev_web_socket,
|
2022-01-11 14:23:19 +00:00
|
|
|
stop_rx: Some(stop_rx),
|
2022-01-14 07:23:21 +00:00
|
|
|
ping_duration,
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 14:23:19 +00:00
|
|
|
pub async fn run(mut self) {
|
2022-01-12 04:40:41 +00:00
|
|
|
let (tx, mut rx) = mpsc::channel(1);
|
2022-01-11 14:23:19 +00:00
|
|
|
let mut stop_rx = self.stop_rx.take().expect("Only take once");
|
2022-01-14 07:23:21 +00:00
|
|
|
let object_id = self.object_id.clone();
|
|
|
|
tokio::spawn(tick(tx, self.ping_duration));
|
2022-01-23 14:33:47 +00:00
|
|
|
let name = format!("{}", self);
|
2022-01-11 14:23:19 +00:00
|
|
|
let stream = stream! {
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
result = rx.recv() => {
|
|
|
|
match result {
|
|
|
|
Some(msg) => yield msg,
|
|
|
|
None => break,
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
|
|
|
},
|
2022-01-11 14:23:19 +00:00
|
|
|
_ = stop_rx.recv() => {
|
2022-01-23 14:33:47 +00:00
|
|
|
tracing::trace!("[{}]:{} loop exit", name, object_id);
|
2022-01-11 14:23:19 +00:00
|
|
|
break
|
|
|
|
},
|
2021-12-20 07:37:37 +00:00
|
|
|
};
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
stream
|
|
|
|
.for_each(|_| async {
|
|
|
|
match self.send_next_revision().await {
|
2022-01-24 09:35:58 +00:00
|
|
|
Ok(_) => {}
|
2022-01-23 14:33:47 +00:00
|
|
|
Err(e) => tracing::error!("[{}] send failed, {:?}", self, e),
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
2022-01-11 14:23:19 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn send_next_revision(&self) -> FlowyResult<()> {
|
2022-01-26 15:29:18 +00:00
|
|
|
match self.provider.next().await? {
|
|
|
|
None => {
|
|
|
|
tracing::trace!("[{}]: Finish synchronizing revisions", self);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
Some(data) => {
|
|
|
|
tracing::trace!("[{}]: send {}:{}-{:?}", self, data.object_id, data.id(), data.ty);
|
2022-02-25 14:27:44 +00:00
|
|
|
self.rev_web_socket.send(data).await
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
}
|
2022-01-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-20 07:37:37 +00:00
|
|
|
|
2022-01-26 15:29:18 +00:00
|
|
|
async fn tick(sender: mpsc::Sender<()>, duration: Duration) {
|
|
|
|
let mut interval = interval(duration);
|
|
|
|
while sender.send(()).await.is_ok() {
|
|
|
|
interval.tick().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:33:47 +00:00
|
|
|
impl std::fmt::Display for RevisionWSSink {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_fmt(format_args!("{}RevisionWSSink", self.object_name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 07:23:21 +00:00
|
|
|
impl std::ops::Drop for RevisionWSSink {
|
2022-01-24 09:35:58 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
tracing::trace!("{} was dropped", self)
|
|
|
|
}
|
2022-01-12 09:08:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 15:51:11 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
enum Source {
|
|
|
|
Custom,
|
|
|
|
Revision,
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:27:44 +00:00
|
|
|
pub trait WSDataProviderDataSource: Send + Sync {
|
|
|
|
fn next_revision(&self) -> FutureResult<Option<Revision>, FlowyError>;
|
|
|
|
fn ack_revision(&self, rev_id: i64) -> FutureResult<(), FlowyError>;
|
|
|
|
fn current_rev_id(&self) -> i64;
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:51:11 +00:00
|
|
|
#[derive(Clone)]
|
2022-02-25 14:27:44 +00:00
|
|
|
pub struct WSDataProvider {
|
2022-01-20 15:51:11 +00:00
|
|
|
object_id: String,
|
2022-02-25 14:27:44 +00:00
|
|
|
rev_ws_data_list: Arc<RwLock<VecDeque<ClientRevisionWSData>>>,
|
|
|
|
data_source: Arc<dyn WSDataProviderDataSource>,
|
|
|
|
current_source: Arc<RwLock<Source>>,
|
2022-01-20 15:51:11 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 14:27:44 +00:00
|
|
|
impl WSDataProvider {
|
|
|
|
pub fn new(object_id: &str, data_source: Arc<dyn WSDataProviderDataSource>) -> Self {
|
|
|
|
WSDataProvider {
|
2022-01-20 15:51:11 +00:00
|
|
|
object_id: object_id.to_owned(),
|
2022-02-25 14:27:44 +00:00
|
|
|
rev_ws_data_list: Arc::new(RwLock::new(VecDeque::new())),
|
|
|
|
data_source,
|
|
|
|
current_source: Arc::new(RwLock::new(Source::Custom)),
|
2022-01-20 15:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
pub async fn push_data(&self, data: ClientRevisionWSData) {
|
2022-02-25 14:27:44 +00:00
|
|
|
self.rev_ws_data_list.write().await.push_back(data);
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
|
|
|
|
pub async fn next(&self) -> FlowyResult<Option<ClientRevisionWSData>> {
|
2022-02-25 14:27:44 +00:00
|
|
|
let source = self.current_source.read().await.clone();
|
2022-01-20 15:51:11 +00:00
|
|
|
let data = match source {
|
2022-02-25 14:27:44 +00:00
|
|
|
Source::Custom => match self.rev_ws_data_list.read().await.front() {
|
2022-01-20 15:51:11 +00:00
|
|
|
None => {
|
2022-02-25 14:27:44 +00:00
|
|
|
*self.current_source.write().await = Source::Revision;
|
2022-01-20 15:51:11 +00:00
|
|
|
Ok(None)
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
Some(data) => Ok(Some(data.clone())),
|
|
|
|
},
|
|
|
|
Source::Revision => {
|
2022-02-25 14:27:44 +00:00
|
|
|
if !self.rev_ws_data_list.read().await.is_empty() {
|
|
|
|
*self.current_source.write().await = Source::Custom;
|
2022-01-20 15:51:11 +00:00
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:27:44 +00:00
|
|
|
match self.data_source.next_revision().await? {
|
2022-01-20 15:51:11 +00:00
|
|
|
Some(rev) => Ok(Some(ClientRevisionWSData::from_revisions(&self.object_id, vec![rev]))),
|
|
|
|
None => Ok(Some(ClientRevisionWSData::ping(
|
|
|
|
&self.object_id,
|
2022-02-25 14:27:44 +00:00
|
|
|
self.data_source.current_rev_id(),
|
2022-01-20 15:51:11 +00:00
|
|
|
))),
|
|
|
|
}
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
};
|
|
|
|
data
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn ack_data(&self, id: String, _ty: ServerRevisionWSDataType) -> FlowyResult<()> {
|
2022-02-25 14:27:44 +00:00
|
|
|
let source = self.current_source.read().await.clone();
|
2022-01-20 15:51:11 +00:00
|
|
|
match source {
|
|
|
|
Source::Custom => {
|
2022-02-25 14:27:44 +00:00
|
|
|
let should_pop = match self.rev_ws_data_list.read().await.front() {
|
2022-01-20 15:51:11 +00:00
|
|
|
None => false,
|
|
|
|
Some(val) => {
|
|
|
|
let expected_id = val.id();
|
|
|
|
if expected_id == id {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
tracing::error!("The front element's {} is not equal to the {}", expected_id, id);
|
|
|
|
false
|
|
|
|
}
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
};
|
|
|
|
if should_pop {
|
2022-02-25 14:27:44 +00:00
|
|
|
let _ = self.rev_ws_data_list.write().await.pop_front();
|
2022-01-20 15:51:11 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
Source::Revision => {
|
|
|
|
let rev_id = id.parse::<i64>().map_err(|e| {
|
|
|
|
FlowyError::internal().context(format!("Parse {} rev_id from {} failed. {}", self.object_id, id, e))
|
|
|
|
})?;
|
2022-02-25 14:27:44 +00:00
|
|
|
let _ = self.data_source.ack_revision(rev_id).await?;
|
2022-01-20 15:51:11 +00:00
|
|
|
Ok::<(), FlowyError>(())
|
2022-01-24 09:35:58 +00:00
|
|
|
}
|
2022-01-20 15:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 14:27:44 +00:00
|
|
|
impl ConflictRevisionSink for Arc<WSDataProvider> {
|
2022-01-20 15:51:11 +00:00
|
|
|
fn send(&self, revisions: Vec<Revision>) -> BoxResultFuture<(), FlowyError> {
|
|
|
|
let sink = self.clone();
|
|
|
|
Box::pin(async move {
|
|
|
|
sink.push_data(ClientRevisionWSData::from_revisions(&sink.object_id, revisions))
|
|
|
|
.await;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ack(&self, rev_id: String, ty: ServerRevisionWSDataType) -> BoxResultFuture<(), FlowyError> {
|
|
|
|
let sink = self.clone();
|
|
|
|
Box::pin(async move { sink.ack_data(rev_id, ty).await })
|
|
|
|
}
|
|
|
|
}
|