forked from loro-dev/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1469 lines (1400 loc) · 56.3 KB
/
lib.rs
File metadata and controls
1469 lines (1400 loc) · 56.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Loro WebSocket Server (simple skeleton)
//!
//! Minimal async WebSocket server that accepts connections and echoes binary
//! protocol frames back to clients. It also responds to text "ping" with
//! text "pong" as described in protocol.md keepalive section.
//!
//! This is intentionally simple and is meant as a starting point. Application
//! logic (authorization, room routing, broadcasting, etc.) should be layered
//! on top using the `loro_protocol` crate for message encoding/decoding.
//!
//! Example (not run here because it binds a socket):
//! ```no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! # let rt = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
//! # rt.block_on(async move {
//! loro_websocket_server::serve("127.0.0.1:9000").await?;
//! # Ok(())
//! # })
//! # }
//! ```
use futures_util::{SinkExt, StreamExt};
use std::{
collections::{HashMap, HashSet},
future::Future,
hash::{Hash, Hasher},
pin::Pin,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::Duration,
};
use tokio::{
net::{TcpListener, TcpStream},
sync::mpsc,
};
use tokio_tungstenite::accept_hdr_async;
use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
use tokio_tungstenite::tungstenite::protocol::frame::CloseFrame;
use tokio_tungstenite::tungstenite::{self, Message};
use loro::awareness::EphemeralStore;
use loro::{ExportMode, LoroDoc};
pub use loro_protocol as protocol;
use protocol::{
try_decode, CrdtType, JoinErrorCode, Permission, ProtocolMessage, UpdateStatusCode,
};
use tracing::{debug, error, info, warn};
// Limits to protect server memory from abusive fragment headers
const MAX_FRAGMENTS: u64 = 4096; // hard cap on number of fragments per batch
const MAX_BATCH_BYTES: u64 = 64 * 1024 * 1024; // 64 MiB per batch
#[derive(Clone, Debug, PartialEq, Eq)]
struct RoomKey {
crdt: CrdtType,
room: String,
}
impl Hash for RoomKey {
fn hash<H: Hasher>(&self, state: &mut H) {
// CrdtType is repr as enum with a few variants; map to u8 for hashing
let tag = match self.crdt {
CrdtType::Loro => 0u8,
CrdtType::LoroEphemeralStore => 1,
CrdtType::LoroEphemeralStorePersisted => 2,
CrdtType::Yjs => 3,
CrdtType::YjsAwareness => 4,
CrdtType::Elo => 5,
};
tag.hash(state);
self.room.hash(state);
}
}
type Sender = mpsc::UnboundedSender<Message>;
// Hook types
/// Snapshot payload returned by `on_load_document` alongside optional metadata
/// that will be passed through to `on_save_document`.
pub struct LoadedDoc<DocCtx> {
pub snapshot: Option<Vec<u8>>,
pub ctx: Option<DocCtx>,
}
/// Arguments provided to `on_load_document`.
pub struct LoadDocArgs {
pub workspace: String,
pub room: String,
pub crdt: CrdtType,
}
/// Arguments provided to `on_save_document`.
pub struct SaveDocArgs<DocCtx> {
pub workspace: String,
pub room: String,
pub crdt: CrdtType,
pub data: Vec<u8>,
pub ctx: Option<DocCtx>,
}
type LoadFuture<DocCtx> =
Pin<Box<dyn Future<Output = Result<LoadedDoc<DocCtx>, String>> + Send + 'static>>;
type SaveFuture = Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'static>>;
type LoadFn<DocCtx> = Arc<dyn Fn(LoadDocArgs) -> LoadFuture<DocCtx> + Send + Sync>;
type SaveFn<DocCtx> = Arc<dyn Fn(SaveDocArgs<DocCtx>) -> SaveFuture + Send + Sync>;
/// Arguments provided to `authenticate`.
pub struct AuthArgs {
pub room: String,
pub crdt: CrdtType,
pub auth: Vec<u8>,
pub conn_id: u64,
}
type AuthFuture =
Pin<Box<dyn Future<Output = Result<Option<Permission>, String>> + Send + 'static>>;
type AuthFn = Arc<dyn Fn(AuthArgs) -> AuthFuture + Send + Sync>;
/// Arguments provided to `handshake_auth`.
pub struct HandshakeAuthArgs<'a> {
pub workspace: &'a str,
pub token: Option<&'a str>,
pub request: &'a tungstenite::handshake::server::Request,
pub conn_id: u64,
}
type HandshakeAuthFn = dyn Fn(HandshakeAuthArgs) -> bool + Send + Sync;
/// Arguments provided to `on_close_connection`.
pub struct CloseConnectionArgs {
pub workspace: String,
pub conn_id: u64,
pub rooms: Vec<(CrdtType, String)>,
}
type CloseConnectionFuture =
Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'static>>;
type CloseConnectionFn =
Arc<dyn Fn(CloseConnectionArgs) -> CloseConnectionFuture + Send + Sync>;
#[derive(Clone)]
pub struct ServerConfig<DocCtx = ()> {
pub on_load_document: Option<LoadFn<DocCtx>>,
pub on_save_document: Option<SaveFn<DocCtx>>,
pub save_interval_ms: Option<u64>,
pub default_permission: Permission,
pub authenticate: Option<AuthFn>,
/// Optional handshake auth: called during WS HTTP upgrade.
///
/// Parameters:
/// - `workspace_id`: extracted from request path `/{workspace}` (empty if missing)
/// - `token`: `token` query parameter if present
/// - `request`: the full HTTP request (headers, uri, etc)
/// - `conn_id`: the connection id
///
/// Return true to accept, false to reject with 401.
pub handshake_auth: Option<Arc<HandshakeAuthFn>>,
/// Optional hook invoked after a connection fully closes.
/// Receives the workspace id, connection id, and rooms the client had joined.
pub on_close_connection: Option<CloseConnectionFn>,
}
// CRDT document abstraction to reduce match-based branching
trait CrdtDoc: Send {
fn get_version(&self) -> Vec<u8> {
Vec::new()
}
fn compute_backfill(&self, _client_version: &[u8]) -> Vec<Vec<u8>> {
Vec::new()
}
fn apply_updates(&mut self, _updates: &[Vec<u8>]) -> Result<(), String> {
Ok(())
}
fn should_persist(&self) -> bool {
false
}
fn export_snapshot(&self) -> Option<Vec<u8>> {
None
}
fn import_snapshot(&mut self, _data: &[u8]) {}
fn allow_backfill_when_no_other_clients(&self) -> bool {
false
}
fn remove_when_last_subscriber_leaves(&self) -> bool {
false
}
}
struct LoroRoomDoc {
doc: LoroDoc,
}
impl LoroRoomDoc {
fn new() -> Self {
Self {
doc: LoroDoc::new(),
}
}
}
impl CrdtDoc for LoroRoomDoc {
fn apply_updates(&mut self, updates: &[Vec<u8>]) -> Result<(), String> {
for u in updates {
let _ = self.doc.import(u);
}
Ok(())
}
fn should_persist(&self) -> bool {
true
}
fn export_snapshot(&self) -> Option<Vec<u8>> {
self.doc.export(ExportMode::Snapshot).ok()
}
fn import_snapshot(&mut self, data: &[u8]) {
let _ = self.doc.import(data);
}
}
struct EphemeralRoomDoc {
store: EphemeralStore,
}
impl EphemeralRoomDoc {
fn new(timeout_ms: i64) -> Self {
Self {
store: EphemeralStore::new(timeout_ms),
}
}
}
impl CrdtDoc for EphemeralRoomDoc {
fn compute_backfill(&self, _client_version: &[u8]) -> Vec<Vec<u8>> {
let data = self.store.encode_all();
if data.is_empty() {
Vec::new()
} else {
vec![data]
}
}
fn apply_updates(&mut self, updates: &[Vec<u8>]) -> Result<(), String> {
for u in updates {
if !u.is_empty() {
self.store.apply(u);
}
}
Ok(())
}
fn remove_when_last_subscriber_leaves(&self) -> bool {
true
}
}
struct PersistentEphemeralRoomDoc {
store: EphemeralStore,
timeout_ms: i64,
}
impl PersistentEphemeralRoomDoc {
fn new(timeout_ms: i64) -> Self {
Self {
store: EphemeralStore::new(timeout_ms),
timeout_ms,
}
}
}
impl CrdtDoc for PersistentEphemeralRoomDoc {
fn compute_backfill(&self, _client_version: &[u8]) -> Vec<Vec<u8>> {
let data = self.store.encode_all();
if data.is_empty() {
Vec::new()
} else {
vec![data]
}
}
fn apply_updates(&mut self, updates: &[Vec<u8>]) -> Result<(), String> {
for u in updates {
if !u.is_empty() {
self.store.apply(u);
}
}
Ok(())
}
fn should_persist(&self) -> bool {
true
}
fn export_snapshot(&self) -> Option<Vec<u8>> {
Some(self.store.encode_all())
}
fn import_snapshot(&mut self, data: &[u8]) {
self.store = EphemeralStore::new(self.timeout_ms);
if !data.is_empty() {
self.store.apply(data);
}
}
fn allow_backfill_when_no_other_clients(&self) -> bool {
true
}
}
// ELO header index entries
struct EloDeltaSpanIndexEntry {
start: u64,
end: u64,
key_id: String,
record: Vec<u8>,
}
struct EloRoomDoc {
spans_by_peer: std::collections::HashMap<String, Vec<EloDeltaSpanIndexEntry>>,
}
impl EloRoomDoc {
fn new() -> Self {
Self {
spans_by_peer: std::collections::HashMap::new(),
}
}
fn peer_key_from_bytes(bytes: &[u8]) -> String {
// Prefer UTF-8 if valid, else hex
match std::str::from_utf8(bytes) {
Ok(s) => s.to_string(),
Err(_) => {
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
use std::fmt::Write as _;
let _ = write!(&mut out, "{:02x}", b);
}
out
}
}
}
fn decode_version_vector(&self, buf: &[u8]) -> Option<std::collections::HashMap<String, u64>> {
use loro_protocol::bytes::BytesReader;
let mut r = BytesReader::new(buf);
let count = usize::try_from(r.read_uleb128().ok()?).ok()?;
let mut map: std::collections::HashMap<String, u64> =
std::collections::HashMap::with_capacity(count);
for _ in 0..count {
let peer_bytes = r.read_var_bytes().ok()?;
let ctr = r.read_uleb128().ok()?;
map.insert(Self::peer_key_from_bytes(peer_bytes), ctr);
}
Some(map)
}
fn encode_current_vv(&self) -> Vec<u8> {
use loro_protocol::bytes::BytesWriter;
let mut entries: Vec<(String, u64)> = Vec::new();
for (peer, spans) in self.spans_by_peer.iter() {
if !peer.as_bytes().iter().all(|b| b.is_ascii_digit()) {
continue;
}
let mut max_end = 0u64;
for s in spans.iter() {
if s.end > max_end {
max_end = s.end;
}
}
if max_end > 0 {
entries.push((peer.clone(), max_end));
}
}
let mut w = BytesWriter::new();
w.push_uleb128(entries.len() as u64);
for (peer, ctr) in entries.iter() {
w.push_var_bytes(peer.as_bytes());
w.push_uleb128(*ctr);
}
w.finalize()
}
}
impl CrdtDoc for EloRoomDoc {
fn get_version(&self) -> Vec<u8> {
// If we have no indexed entries yet, return an empty version to signal
// "unknown/empty" baseline so clients may choose to send a snapshot.
if self.spans_by_peer.is_empty() {
return Vec::new();
}
self.encode_current_vv()
}
fn compute_backfill(&self, client_version: &[u8]) -> Vec<Vec<u8>> {
let known = self
.decode_version_vector(client_version)
.unwrap_or_default();
let mut records: Vec<Vec<u8>> = Vec::new();
for (peer, spans) in self.spans_by_peer.iter() {
let k = known.get(peer).copied().unwrap_or(0);
for e in spans {
if e.end > k {
records.push(e.record.clone());
}
}
}
if records.is_empty() {
return Vec::new();
}
let mut w = loro_protocol::bytes::BytesWriter::new();
w.push_uleb128(records.len() as u64);
for rec in records.iter() {
w.push_var_bytes(rec);
}
vec![w.finalize()]
}
fn apply_updates(&mut self, updates: &[Vec<u8>]) -> Result<(), String> {
use loro_protocol::elo::{
decode_elo_container, parse_elo_record_header, EloHeader, EloRecordKind,
};
for u in updates {
let records = decode_elo_container(u.as_slice())?;
for rec in records {
let parsed = parse_elo_record_header(rec)?;
match parsed.kind {
EloRecordKind::DeltaSpan => {
if let EloHeader::Delta(h) = parsed.header {
if !(h.end > h.start) {
return Err("invalid ELO delta span: end must be > start".into());
}
if h.iv.len() != 12 {
return Err("invalid ELO delta span: IV must be 12 bytes".into());
}
let peer = Self::peer_key_from_bytes(&h.peer_id);
let list = self.spans_by_peer.entry(peer).or_default();
// Insert keeping order by start; remove fully covered entries [start, end]
let mut kept: Vec<EloDeltaSpanIndexEntry> =
Vec::with_capacity(list.len() + 1);
let mut inserted = false;
for e in list.iter() {
if !inserted && e.start >= h.start {
kept.push(EloDeltaSpanIndexEntry {
start: h.start,
end: h.end,
key_id: h.key_id.clone(),
record: rec.to_vec(),
});
inserted = true;
}
// keep entries not fully covered by [start, end]
let covered = e.start >= h.start && e.end <= h.end;
if !covered {
kept.push(EloDeltaSpanIndexEntry {
start: e.start,
end: e.end,
key_id: e.key_id.clone(),
record: e.record.clone(),
});
}
}
if !inserted {
kept.push(EloDeltaSpanIndexEntry {
start: h.start,
end: h.end,
key_id: h.key_id.clone(),
record: rec.to_vec(),
});
}
*list = kept;
}
}
EloRecordKind::Snapshot => {
// Snapshot header validation already done by parser; no indexing needed
}
}
}
}
Ok(())
}
fn allow_backfill_when_no_other_clients(&self) -> bool {
true
}
}
impl<DocCtx> Default for ServerConfig<DocCtx> {
fn default() -> Self {
Self {
on_load_document: None,
on_save_document: None,
save_interval_ms: None,
default_permission: Permission::Write,
authenticate: None,
handshake_auth: None,
on_close_connection: None,
}
}
}
struct RoomDocState<DocCtx> {
doc: Box<dyn CrdtDoc>,
dirty: bool,
ctx: Option<DocCtx>,
}
struct Hub<DocCtx> {
// room -> vec of (conn_id, sender)
subs: HashMap<RoomKey, Vec<(u64, Sender)>>,
// room -> document state (Loro persistent, Ephemeral in-memory, Elo index)
docs: HashMap<RoomKey, RoomDocState<DocCtx>>,
config: ServerConfig<DocCtx>,
// (conn_id, room) -> permission
perms: HashMap<(u64, RoomKey), Permission>,
workspace: String,
// Fragment reassembly state: per room + batch id
fragments: HashMap<(RoomKey, protocol::BatchId), FragmentBatch>,
}
impl<DocCtx> Hub<DocCtx>
where
DocCtx: Clone + Send + Sync + 'static,
{
fn new(config: ServerConfig<DocCtx>, workspace: String) -> Self {
Self {
subs: HashMap::new(),
docs: HashMap::new(),
config,
perms: HashMap::new(),
workspace,
fragments: HashMap::new(),
}
}
const EPHEMERAL_TIMEOUT_MS: i64 = 60_000;
fn join(&mut self, conn_id: u64, room: RoomKey, tx: &Sender) {
let entry = self.subs.entry(room).or_default();
if !entry.iter().any(|(id, _)| *id == conn_id) {
entry.push((conn_id, tx.clone()));
}
}
fn leave_all(&mut self, conn_id: u64) {
let mut emptied: Vec<RoomKey> = Vec::new();
for (k, vec) in self.subs.iter_mut() {
vec.retain(|(id, _)| *id != conn_id);
if vec.is_empty() {
emptied.push(k.clone());
}
}
// Drop empty rooms from subscription map
for k in &emptied {
let _ = self.subs.remove(k);
}
// Remove permissions for this connection
self.perms.retain(|(id, _), _| *id != conn_id);
// Clean up ephemeral state for rooms that no longer have subscribers
for k in emptied.clone() {
if let Some(state) = self.docs.get(&k) {
if state.doc.remove_when_last_subscriber_leaves() {
self.docs.remove(&k);
debug!(room=?k.room, "cleaned up ephemeral doc after last subscriber left");
}
}
}
// Clean up in-flight fragment batches started by this connection, or for rooms now emptied
if !self.fragments.is_empty() {
use std::collections::HashSet;
let emptied_set: HashSet<RoomKey> = emptied.into_iter().collect();
self.fragments
.retain(|(rk, _), b| b.from_conn != conn_id && !emptied_set.contains(rk));
}
}
fn broadcast(&mut self, room: &RoomKey, from: u64, msg: Message) {
if let Some(list) = self.subs.get_mut(room) {
// drop dead senders
let mut dead: HashSet<u64> = HashSet::new();
for (id, tx) in list.iter() {
if *id == from {
continue;
}
if tx.send(msg.clone()).is_err() {
dead.insert(*id);
}
}
if !dead.is_empty() {
list.retain(|(id, _)| !dead.contains(id));
debug!(room=?room.room, removed=%dead.len(), "removed dead subscribers");
}
}
}
async fn ensure_room_loaded(&mut self, room: &RoomKey) {
if self.docs.contains_key(room) {
return;
}
match room.crdt {
CrdtType::Loro => {
let mut d = LoroRoomDoc::new();
let mut ctx = None;
if let Some(loader) = &self.config.on_load_document {
let args = LoadDocArgs {
workspace: self.workspace.clone(),
room: room.room.clone(),
crdt: room.crdt,
};
match (loader)(args).await {
Ok(loaded) => {
if let Some(bytes) = loaded.snapshot {
d.import_snapshot(&bytes);
}
ctx = loaded.ctx;
}
Err(e) => {
warn!(room=?room.room, %e, "load document failed");
}
}
}
self.docs.insert(
room.clone(),
RoomDocState {
doc: Box::new(d),
dirty: false,
ctx,
},
);
}
CrdtType::LoroEphemeralStore => {
let d = EphemeralRoomDoc::new(Self::EPHEMERAL_TIMEOUT_MS);
self.docs.insert(
room.clone(),
RoomDocState {
doc: Box::new(d),
dirty: false,
ctx: None,
},
);
}
CrdtType::LoroEphemeralStorePersisted => {
let mut d = PersistentEphemeralRoomDoc::new(Self::EPHEMERAL_TIMEOUT_MS);
let mut ctx = None;
if let Some(loader) = &self.config.on_load_document {
let args = LoadDocArgs {
workspace: self.workspace.clone(),
room: room.room.clone(),
crdt: room.crdt,
};
match (loader)(args).await {
Ok(loaded) => {
if let Some(bytes) = loaded.snapshot {
d.import_snapshot(&bytes);
}
ctx = loaded.ctx;
}
Err(e) => {
warn!(room=?room.room, %e, "load persisted ephemeral store failed");
}
}
}
self.docs.insert(
room.clone(),
RoomDocState {
doc: Box::new(d),
dirty: false,
ctx,
},
);
}
CrdtType::Elo => {
let d = EloRoomDoc::new();
self.docs.insert(
room.clone(),
RoomDocState {
doc: Box::new(d),
dirty: false,
ctx: None,
},
);
}
_ => {}
}
}
fn current_version_bytes(&self, room: &RoomKey) -> Vec<u8> {
match self.docs.get(room) {
Some(state) => state.doc.get_version(),
None => Vec::new(),
}
}
fn apply_updates(&mut self, room: &RoomKey, updates: &[Vec<u8>]) -> Result<(), String> {
match self.docs.get_mut(room) {
Some(state) => {
if let Err(e) = state.doc.apply_updates(updates) {
warn!(room=?room.room, %e, "apply_updates failed");
Err(e)
} else {
if state.doc.should_persist() {
state.dirty = true;
}
Ok(())
}
}
None => Err("room not found".into()),
}
}
fn snapshot_bytes(&self, room: &RoomKey) -> Option<Vec<u8>> {
let Some(data) = self.docs.get(room).and_then(|s| s.doc.export_snapshot()) else {
return None;
};
if data.is_empty() {
None
} else {
Some(data)
}
}
}
struct FragmentBatch {
from_conn: u64,
fragment_count: u64,
total_size: u64,
received: u64,
chunks: Vec<Option<Vec<u8>>>,
}
impl<DocCtx> Hub<DocCtx>
where
DocCtx: Clone + Send + Sync + 'static,
{
fn start_fragment_batch(
&mut self,
room: &RoomKey,
from_conn: u64,
batch_id: protocol::BatchId,
fragment_count: u64,
total_size: u64,
) {
let key = (room.clone(), batch_id);
let chunks_len = usize::try_from(fragment_count).unwrap_or(0);
let batch = FragmentBatch {
from_conn,
fragment_count,
total_size,
received: 0,
chunks: vec![None; chunks_len],
};
self.fragments.insert(key, batch);
}
/// Returns Some(reassembled) when complete; removes batch.
fn add_fragment_and_maybe_finish(
&mut self,
room: &RoomKey,
batch_id: protocol::BatchId,
index: u64,
fragment: Vec<u8>,
) -> Option<Vec<u8>> {
let key = (room.clone(), batch_id);
if let Some(b) = self.fragments.get_mut(&key) {
let idx = match usize::try_from(index) {
Ok(i) => i,
Err(_) => return None,
};
if idx >= b.chunks.len() {
return None;
}
if b.chunks[idx].is_none() {
b.chunks[idx] = Some(fragment);
b.received += 1;
}
if b.received == b.fragment_count {
let mut out = Vec::with_capacity(b.total_size as usize);
for ch in b.chunks.iter() {
if let Some(bytes) = ch.as_ref() {
out.extend_from_slice(bytes);
}
}
self.fragments.remove(&key);
return Some(out);
}
}
None
}
}
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
static NEXT_BATCH_ID: AtomicU64 = AtomicU64::new(1);
fn next_batch_id() -> protocol::BatchId {
protocol::BatchId(NEXT_BATCH_ID.fetch_add(1, Ordering::Relaxed).to_be_bytes())
}
fn send_ack(
tx: &Sender,
crdt: CrdtType,
room: &str,
ref_id: protocol::BatchId,
status: UpdateStatusCode,
) {
let ack = ProtocolMessage::Ack {
crdt,
room_id: room.to_string(),
ref_id,
status,
};
if let Ok(bytes) = loro_protocol::encode(&ack) {
let _ = tx.send(Message::Binary(bytes.into()));
}
}
struct HubRegistry<DocCtx> {
config: ServerConfig<DocCtx>,
hubs: tokio::sync::Mutex<HashMap<String, Arc<tokio::sync::Mutex<Hub<DocCtx>>>>>,
}
impl<DocCtx> HubRegistry<DocCtx>
where
DocCtx: Clone + Send + Sync + 'static,
{
fn new(config: ServerConfig<DocCtx>) -> Self {
Self {
config,
hubs: tokio::sync::Mutex::new(HashMap::new()),
}
}
async fn get_or_create(&self, workspace: &str) -> Arc<tokio::sync::Mutex<Hub<DocCtx>>> {
let mut map = self.hubs.lock().await;
if let Some(h) = map.get(workspace) {
return h.clone();
}
let hub = Arc::new(tokio::sync::Mutex::new(Hub::new(
self.config.clone(),
workspace.to_string(),
)));
// Spawn saver task for this hub if configured
if let (Some(ms), Some(saver)) = (
self.config.save_interval_ms,
self.config.on_save_document.clone(),
) {
let hub_clone = hub.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(ms));
loop {
interval.tick().await;
let mut guard = hub_clone.lock().await;
let ws = guard.workspace.clone();
let rooms: Vec<RoomKey> = guard.docs.keys().cloned().collect();
for room in rooms {
if let Some(state) = guard.docs.get_mut(&room) {
if state.dirty && state.doc.should_persist() {
let start = std::time::Instant::now();
if let Some(snapshot) = state.doc.export_snapshot() {
let room_str = room.room.clone();
let ctx = state.ctx.clone();
let args = SaveDocArgs {
workspace: ws.clone(),
room: room_str.clone(),
crdt: room.crdt,
data: snapshot,
ctx,
};
match (saver)(args).await {
Ok(()) => {
state.dirty = false;
let elapsed = start.elapsed();
debug!(workspace=%ws, room=%room_str, ms=%elapsed.as_millis(), "snapshot saved");
}
Err(e) => {
warn!(workspace=%ws, room=%room_str, %e, "snapshot save failed");
}
}
}
}
}
}
}
});
}
map.insert(workspace.to_string(), hub.clone());
hub
}
}
/// Start a simple broadcast server on the given socket address.
pub async fn serve(addr: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
info!(%addr, "binding TCP listener");
let listener = TcpListener::bind(addr).await?;
serve_incoming_with_config::<()>(listener, ServerConfig::default()).await
}
/// Serve a pre-bound listener. Useful for tests to bind on port 0.
pub async fn serve_incoming(
listener: TcpListener,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
serve_incoming_with_config::<()>(listener, ServerConfig::default()).await
}
pub async fn serve_incoming_with_config<DocCtx>(
listener: TcpListener,
config: ServerConfig<DocCtx>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
DocCtx: Clone + Send + Sync + 'static,
{
let registry = Arc::new(HubRegistry::new(config.clone()));
loop {
match listener.accept().await {
Ok((stream, peer)) => {
debug!(remote=%peer, "accepted TCP connection");
let registry = registry.clone();
tokio::spawn(async move {
if let Err(e) = handle_conn(stream, registry).await {
warn!(%e, "connection task ended with error");
}
});
}
Err(e) => {
error!(%e, "accept failed; continuing");
continue;
}
}
}
}
async fn handle_conn<DocCtx>(
stream: TcpStream,
registry: Arc<HubRegistry<DocCtx>>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>>
where
DocCtx: Clone + Send + Sync + 'static,
{
// Generate a connection id
let conn_id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
// Capture config outside of non-async closure
let handshake_auth = registry.config.handshake_auth.clone();
let close_connection = registry.config.on_close_connection.clone();
let workspace_holder: Arc<std::sync::Mutex<Option<String>>> =
Arc::new(std::sync::Mutex::new(None));
let workspace_holder_c = workspace_holder.clone();
let ws = accept_hdr_async(
stream,
move |req: &tungstenite::handshake::server::Request,
resp: tungstenite::handshake::server::Response| {
if let Some(check) = &handshake_auth {
// Parse path: expect "/{workspace}" (workspace may be empty)
let uri = req.uri();
let path = uri.path();
let mut workspace_id = "";
if let Some(rest) = path.strip_prefix('/') {
if !rest.is_empty() {
// take first segment as workspace id
workspace_id = rest.split('/').next().unwrap_or("");
}
}
// Save for later
{
if let Ok(mut guard) = workspace_holder_c.lock() {
*guard = Some(workspace_id.to_string());
}
}
// Parse query token parameter (no external deps)
let token = uri.query().and_then(|q| {
for pair in q.split('&') {
let mut it = pair.splitn(2, '=');
let k = it.next().unwrap_or("");
let v = it.next();
if k == "token" {
return Some(v.unwrap_or(""));
}
}
None
});
let allowed = (check)(HandshakeAuthArgs {
workspace: workspace_id,
token,
request: req,
conn_id,
});
if !allowed {
warn!(workspace=%workspace_id, token=?token, "handshake auth denied");
// Build a 401 Unauthorized response
let builder = tungstenite::http::Response::builder()
.status(tungstenite::http::StatusCode::UNAUTHORIZED);
// Provide a small body for clarity
let response = builder
.body(Some("Unauthorized".to_string()))
.unwrap_or_else(|e| {
warn!(?e, "failed to build unauthorized response");
let mut fallback =
tungstenite::http::Response::new(Some("Unauthorized".to_string()));
*fallback.status_mut() = tungstenite::http::StatusCode::UNAUTHORIZED;
fallback
});
return Err(response);
}
debug!(workspace=%workspace_id, token=?token, "handshake auth accepted");
}
Ok(resp)
},
)
.await?;
// Determine workspace id (default to empty string)
let workspace_id = workspace_holder