@@ -12,13 +12,18 @@ use serde::{Deserialize, Serialize};
12
12
use std:: fmt:: { Display , Formatter } ;
13
13
use std:: ops:: { Deref , DerefMut } ;
14
14
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.
16
18
///
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
+ }
22
27
23
28
#[ derive( Debug , Default , Clone , Serialize , Deserialize ) ]
24
29
pub struct MessageByObjectId ( pub HashMap < String , Vec < ClientCollabMessage > > ) ;
@@ -105,7 +110,7 @@ impl RealtimeMessage {
105
110
let data = DefaultOptions :: new ( )
106
111
. with_fixint_encoding ( )
107
112
. allow_trailing_bytes ( )
108
- . with_limit ( MAXIMUM_REALTIME_MESSAGE_SIZE )
113
+ . with_limit ( max_sync_message_size ( ) as u64 )
109
114
. serialize ( self )
110
115
. map_err ( |e| {
111
116
anyhow ! (
@@ -121,7 +126,7 @@ impl RealtimeMessage {
121
126
let message = DefaultOptions :: new ( )
122
127
. with_fixint_encoding ( )
123
128
. allow_trailing_bytes ( )
124
- . with_limit ( MAXIMUM_REALTIME_MESSAGE_SIZE )
129
+ . with_limit ( max_sync_message_size ( ) as u64 )
125
130
. deserialize ( data) ?;
126
131
Ok ( message)
127
132
}
0 commit comments