feat: Upload collab limit (#160)

* chore: add logs

* feat: update upload collab limit
This commit is contained in:
Nathan.fooo 2023-11-12 16:03:48 +08:00 committed by GitHub
parent c7e90eb24b
commit 04989e9485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 22 deletions

View File

@ -6,7 +6,7 @@ use std::sync::{Arc, Weak};
use std::time::Duration;
use crate::collab_sync::pending_msg::{MessageState, PendingMsgQueue};
use crate::collab_sync::{SyncError, DEFAULT_SYNC_TIMEOUT};
use crate::collab_sync::{SyncError, SyncObject, DEFAULT_SYNC_TIMEOUT};
use futures_util::SinkExt;
use realtime_entity::collab_msg::{CollabSinkMessage, MsgId};
@ -41,7 +41,7 @@ pub struct CollabSink<Sink, Msg> {
/// The [PendingMsgQueue] is used to queue the messages that are waiting to be sent to the
/// remote. It will merge the messages if possible.
pending_msg_queue: Arc<parking_lot::Mutex<PendingMsgQueue<Msg>>>,
msg_id_counter: Arc<dyn MsgIdCounter>,
msg_id_counter: Arc<DefaultMsgIdCounter>,
/// The [watch::Sender] is used to notify the [CollabSinkRunner] to process the pending messages.
/// Sending `false` will stop the [CollabSinkRunner].
@ -49,7 +49,6 @@ pub struct CollabSink<Sink, Msg> {
config: SinkConfig,
/// Stop the [IntervalRunner] if the sink strategy is [SinkStrategy::FixInterval].
#[allow(dead_code)]
interval_runner_stop_tx: Option<mpsc::Sender<()>>,
/// Used to calculate the time interval between two messages. Only used when the sink strategy
@ -57,10 +56,17 @@ pub struct CollabSink<Sink, Msg> {
instant: Mutex<Instant>,
state_notifier: Arc<watch::Sender<SinkState>>,
pause: AtomicBool,
object: SyncObject,
}
impl<Sink, Msg> Drop for CollabSink<Sink, Msg> {
fn drop(&mut self) {
trace!("Drop CollabSink {}", self.object.object_id);
if let Some(stop_tx) = self.interval_runner_stop_tx.take() {
spawn(async move {
let _ = stop_tx.send(()).await;
});
}
let _ = self.notifier.send(true);
}
}
@ -71,18 +77,16 @@ where
Sink: SinkExt<Msg, Error = E> + Send + Sync + Unpin + 'static,
Msg: CollabSinkMessage,
{
pub fn new<C>(
pub fn new(
uid: i64,
object: SyncObject,
sink: Sink,
notifier: watch::Sender<bool>,
sync_state_tx: watch::Sender<SinkState>,
msg_id_counter: C,
config: SinkConfig,
pause: bool,
) -> Self
where
C: MsgIdCounter,
{
) -> Self {
let msg_id_counter = DefaultMsgIdCounter::new();
let notifier = Arc::new(notifier);
let state_notifier = Arc::new(sync_state_tx);
let sender = Arc::new(Mutex::new(sink));
@ -109,6 +113,7 @@ where
instant,
interval_runner_stop_tx,
pause: AtomicBool::new(pause),
object,
}
}
@ -312,7 +317,7 @@ where
match self.sender.try_lock() {
Ok(mut sender) => {
debug!("ending {}", collab_msg);
debug!("Sending {}", collab_msg);
sender.send(collab_msg).await.ok()?;
},
Err(_) => {
@ -362,12 +367,6 @@ where
pub(crate) fn notify(&self) {
let _ = self.notifier.send(false);
}
/// Stop the sink.
#[allow(dead_code)]
fn stop(&self) {
let _ = self.notifier.send(true);
}
}
fn retry_later(weak_notifier: Weak<watch::Sender<bool>>) {
@ -491,9 +490,6 @@ impl DefaultMsgIdCounter {
pub fn new() -> Self {
Self::default()
}
}
impl MsgIdCounter for DefaultMsgIdCounter {
fn next(&self) -> MsgId {
self.0.fetch_add(1, Ordering::SeqCst)
}

View File

@ -1,5 +1,5 @@
use crate::collab_sync::{
CollabSink, CollabSinkRunner, DefaultMsgIdCounter, SinkConfig, SinkState, SyncError, SyncObject,
CollabSink, CollabSinkRunner, SinkConfig, SinkState, SyncError, SyncObject,
};
use bytes::Bytes;
use collab::core::collab::MutexCollab;
@ -67,10 +67,10 @@ where
let sink = Arc::new(CollabSink::new(
origin.client_user_id().unwrap_or(0),
object.clone(),
sink,
notifier,
sync_state_tx,
DefaultMsgIdCounter::new(),
config,
pause,
));

View File

@ -41,6 +41,9 @@ pub fn workspace_scope() -> Scope {
)
.service(
web::resource("{workspace_id}/collab/{object_id}")
.app_data(
PayloadConfig::new(5 * 1024 * 1024), // 10 MB
)
.route(web::post().to(create_collab_handler))
.route(web::get().to(get_collab_handler))
.route(web::put().to(update_collab_handler))
@ -68,7 +71,7 @@ pub fn collab_scope() -> Scope {
web::scope("/api/realtime").service(
web::resource("post")
.app_data(
PayloadConfig::new(10 * 1024 * 1024), // 10 MB
PayloadConfig::new(5 * 1024 * 1024), // 10 MB
)
.route(web::post().to(post_realtime_message_handler)),
)