Skip to content

Commit 7e8773c

Browse files
authored
chore: support custom message size (#1445)
1 parent 9c2bf07 commit 7e8773c

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

libs/collab-rt-entity/src/message.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ use serde::{Deserialize, Serialize};
1212
use std::fmt::{Display, Formatter};
1313
use std::ops::{Deref, DerefMut};
1414

15-
/// Maximum allowable size for a realtime message.
15+
pub const MAXIMUM_REALTIME_MESSAGE_SIZE: usize = 10 * 1024 * 1024; // 10 MB
16+
17+
/// Get the maximum realtime message size from environment variable or use default.
1618
///
17-
/// This sets the largest size a message can be for server processing in real-time communications.
18-
/// If a message goes over this size, it won't be processed and will trigger a parser error.
19-
/// This limit helps prevent server issues like overloads and denial-of-service attacks by rejecting
20-
/// overly large messages.
21-
pub const MAXIMUM_REALTIME_MESSAGE_SIZE: u64 = 1024 * 1024; // 1 MB
19+
/// Reads from the `APPFLOWY_REALTIME_MESSAGE_SIZE` environment variable.
20+
/// If not set or invalid, falls back to `MAXIMUM_REALTIME_MESSAGE_SIZE`.
21+
pub fn max_sync_message_size() -> usize {
22+
std::env::var("APPFLOWY_REALTIME_MESSAGE_SIZE")
23+
.ok()
24+
.and_then(|s| s.parse().ok())
25+
.unwrap_or(MAXIMUM_REALTIME_MESSAGE_SIZE)
26+
}
2227

2328
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
2429
pub struct MessageByObjectId(pub HashMap<String, Vec<ClientCollabMessage>>);
@@ -105,7 +110,7 @@ impl RealtimeMessage {
105110
let data = DefaultOptions::new()
106111
.with_fixint_encoding()
107112
.allow_trailing_bytes()
108-
.with_limit(MAXIMUM_REALTIME_MESSAGE_SIZE)
113+
.with_limit(max_sync_message_size() as u64)
109114
.serialize(self)
110115
.map_err(|e| {
111116
anyhow!(
@@ -121,7 +126,7 @@ impl RealtimeMessage {
121126
let message = DefaultOptions::new()
122127
.with_fixint_encoding()
123128
.allow_trailing_bytes()
124-
.with_limit(MAXIMUM_REALTIME_MESSAGE_SIZE)
129+
.with_limit(max_sync_message_size() as u64)
125130
.deserialize(data)?;
126131
Ok(message)
127132
}

src/api/ws.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use appflowy_collaborate::collab::storage::CollabAccessControlStorage;
1616
use appflowy_collaborate::ws2::{SessionInfo, WsSession};
1717
use appflowy_proto::{ServerMessage, WorkspaceNotification};
1818
use collab_rt_entity::user::{AFUserChange, RealtimeUser, UserMessage};
19-
use collab_rt_entity::RealtimeMessage;
19+
use collab_rt_entity::{max_sync_message_size, RealtimeMessage};
2020
use collab_stream::model::MessageId;
2121
use secrecy::Secret;
2222
use semver::Version;
@@ -156,7 +156,7 @@ pub async fn establish_ws_connection_v2(
156156
&request,
157157
payload,
158158
)
159-
.frame_size(10 * 1024 * 1024)
159+
.frame_size(max_sync_message_size())
160160
.start()
161161
}
162162

tests/collab/single_device_edit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async fn write_big_chunk_data_init_sync_test() {
124124
let mut test_client = TestClient::new_user().await;
125125
let workspace_id = test_client.workspace_id().await;
126126
let object_id = Uuid::new_v4();
127-
let big_text = generate_random_string((MAXIMUM_REALTIME_MESSAGE_SIZE / 2) as usize);
127+
let big_text = generate_random_string(MAXIMUM_REALTIME_MESSAGE_SIZE / 2);
128128
let collab_type = CollabType::Unknown;
129129
let doc_state = make_collab_with_key_value(&object_id, "text", big_text.clone());
130130

0 commit comments

Comments
 (0)