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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
use ahash::AHashMap as HashMap;
use std::any::Any;
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use crate::adapter::connection_manager::{ConnectionManager, HorizontalAdapterInterface};
use crate::adapter::horizontal_adapter::{
BroadcastMessage, DeadNodeEvent, HorizontalAdapter, OrphanedMember, PendingRequest,
RequestBody, RequestType, ResponseBody, current_timestamp, generate_request_id,
};
use crate::adapter::horizontal_transport::{
HorizontalTransport, TransportConfig, TransportHandlers,
};
use crate::adapter::local_adapter::LocalAdapter;
use crate::app::manager::AppManager;
use crate::channel::PresenceMemberInfo;
use crate::error::{Error, Result};
use crate::metrics::MetricsInterface;
use crate::namespace::Namespace;
use crate::options::ClusterHealthConfig;
use crate::protocol::messages::PusherMessage;
use crate::websocket::{SocketId, WebSocketRef};
use async_trait::async_trait;
use sockudo_ws::axum_integration::WebSocketWriter;
use tokio::sync::{Mutex, Notify, RwLock};
use tracing::{debug, error, info, warn};
use uuid::Uuid;
/// Generic base adapter that handles all common horizontal scaling logic
pub struct HorizontalAdapterBase<T: HorizontalTransport> {
pub horizontal: Arc<RwLock<HorizontalAdapter>>,
pub local_adapter: Arc<LocalAdapter>,
pub transport: T,
pub config: T::Config,
pub event_bus: Arc<OnceLock<tokio::sync::mpsc::UnboundedSender<DeadNodeEvent>>>,
pub node_id: String,
pub cluster_health_enabled: bool,
pub heartbeat_interval_ms: u64,
pub node_timeout_ms: u64,
pub cleanup_interval_ms: u64,
pub enable_socket_counting: bool,
// Delta compression manager for bandwidth optimization
delta_compression: Option<Arc<crate::delta_compression::DeltaCompressionManager>>,
// App manager for getting channel-specific delta settings
app_manager: Option<Arc<dyn AppManager + Send + Sync>>,
}
/// Check if we should skip horizontal communication (single node optimization)
/// This is determined by checking if cluster health is enabled and
/// if the effective node count is 1 or less.
async fn should_skip_horizontal_communication_impl(
cluster_health_enabled: bool,
horizontal: &Arc<RwLock<HorizontalAdapter>>,
) -> bool {
// Don't skip sending if cluster health is disabled, we have no way of knowing other nodes
if !cluster_health_enabled {
return false;
}
let horizontal_guard = horizontal.read().await;
let effective_node_count = horizontal_guard.get_effective_node_count().await;
effective_node_count <= 1
}
impl<T: HorizontalTransport> HorizontalAdapterBase<T> {
/// Check if we should skip horizontal communication (single node optimization)
/// This is determined by checking if cluster health is enabled and
/// if the effective node count is 1 or less.
pub async fn should_skip_horizontal_communication(&self) -> bool {
should_skip_horizontal_communication_impl(self.cluster_health_enabled, &self.horizontal)
.await
}
}
impl<T: HorizontalTransport + 'static> HorizontalAdapterBase<T>
where
T::Config: TransportConfig,
{
pub async fn new(config: T::Config) -> Result<Self> {
let mut horizontal = HorizontalAdapter::new();
horizontal.requests_timeout = config.request_timeout_ms();
let node_id = horizontal.node_id.clone();
// Get the Arc reference to the same LocalAdapter that HorizontalAdapter uses
let local_adapter = horizontal.local_adapter.clone();
let transport = T::new(config.clone()).await?;
let cluster_health_defaults = ClusterHealthConfig::default();
Ok(Self {
horizontal: Arc::new(RwLock::new(horizontal)),
local_adapter,
transport,
config,
event_bus: Arc::new(OnceLock::new()),
node_id,
cluster_health_enabled: cluster_health_defaults.enabled,
heartbeat_interval_ms: cluster_health_defaults.heartbeat_interval_ms,
node_timeout_ms: cluster_health_defaults.node_timeout_ms,
cleanup_interval_ms: cluster_health_defaults.cleanup_interval_ms,
enable_socket_counting: true, // Default to enabled
delta_compression: None,
app_manager: None,
})
}
/// Set delta compression manager and app manager for delta compression support
pub async fn set_delta_compression(
&mut self,
delta_compression: Arc<crate::delta_compression::DeltaCompressionManager>,
app_manager: Arc<dyn AppManager + Send + Sync>,
) {
// Set on the horizontal adapter base
self.delta_compression = Some(delta_compression.clone());
self.app_manager = Some(app_manager.clone());
// Also set on the internal LocalAdapter
let horizontal = self.horizontal.read().await;
horizontal
.local_adapter
.set_delta_compression(delta_compression, app_manager)
.await;
}
pub async fn set_metrics(
&self,
metrics: Arc<Mutex<dyn MetricsInterface + Send + Sync>>,
) -> Result<()> {
let mut horizontal = self.horizontal.write().await;
horizontal.metrics = Some(metrics);
Ok(())
}
pub fn set_event_bus(&self, event_sender: tokio::sync::mpsc::UnboundedSender<DeadNodeEvent>) {
// OnceLock::set returns Err if already set, which we ignore (first-write-wins)
let _ = self.event_bus.set(event_sender);
}
/// Configure this adapter with discovered nodes for testing
/// This simulates that node discovery has already happened and sets up multi-node behavior
pub async fn with_discovered_nodes(self, node_ids: Vec<&str>) -> Result<Self> {
let horizontal = self.horizontal.read().await;
for node_id in node_ids {
let node_id_string = node_id.to_string();
if node_id_string != self.node_id {
horizontal
.add_discovered_node_for_test(node_id_string)
.await;
}
}
drop(horizontal); // Release the lock
Ok(self)
}
pub async fn set_cluster_health(&mut self, cluster_health: &ClusterHealthConfig) -> Result<()> {
// Validate cluster health configuration first
if let Err(validation_error) = cluster_health.validate() {
warn!(
"Cluster health configuration validation failed: {}",
validation_error
);
warn!("Keeping current cluster health settings");
return Ok(());
}
// Set cluster health configuration directly on the base adapter
self.heartbeat_interval_ms = cluster_health.heartbeat_interval_ms;
self.node_timeout_ms = cluster_health.node_timeout_ms;
self.cleanup_interval_ms = cluster_health.cleanup_interval_ms;
self.cluster_health_enabled = cluster_health.enabled;
// Log warning for high-frequency heartbeats
if cluster_health.heartbeat_interval_ms < 1000 {
warn!(
"High-frequency heartbeat interval ({} ms) may cause unnecessary network load. Consider using >= 1000 ms unless high availability is critical.",
cluster_health.heartbeat_interval_ms
);
}
Ok(())
}
/// Set socket counting configuration
pub fn set_socket_counting(&mut self, enable: bool) {
self.enable_socket_counting = enable;
}
/// Enhanced send_request that properly integrates with HorizontalAdapter
pub async fn send_request(
&self,
app_id: &str,
request_type: RequestType,
channel: Option<&str>,
socket_id: Option<&str>,
user_id: Option<&str>,
) -> Result<ResponseBody> {
let node_count = self.transport.get_node_count().await?;
// Create the request
let request_id = Uuid::new_v4().to_string();
let node_id = {
let horizontal = self.horizontal.read().await;
horizontal.node_id.clone()
};
let request = RequestBody {
request_id: request_id.clone(),
node_id,
app_id: app_id.to_string(),
request_type: request_type.clone(),
channel: channel.map(String::from),
socket_id: socket_id.map(String::from),
user_id: user_id.map(String::from),
// Cluster presence fields (not used for regular requests)
user_info: None,
timestamp: None,
dead_node_id: None,
target_node_id: None,
};
// Add to pending requests
{
let horizontal = self.horizontal.read().await;
horizontal.pending_requests.insert(
request_id.clone(),
PendingRequest {
start_time: Instant::now(),
app_id: app_id.to_string(),
responses: Vec::with_capacity(node_count.saturating_sub(1)),
notify: Arc::new(Notify::new()),
},
);
if let Some(metrics_ref) = &horizontal.metrics {
let metrics = metrics_ref.lock().await;
metrics.mark_horizontal_adapter_request_sent(app_id);
}
}
// Broadcast the request via transport (skip if single node)
if !self.should_skip_horizontal_communication().await {
self.transport.publish_request(&request).await?;
}
// Wait for responses with adaptive timeout based on request type and node count
let base_timeout = self.config.request_timeout_ms();
// Adaptive timeout: For simple queries (socket checks, counts), use shorter timeout
// For presence queries that need member data, use full timeout
let timeout_duration = match request_type {
RequestType::SocketExistsInChannel
| RequestType::ChannelSocketsCount
| RequestType::SocketsCount
| RequestType::Channels
| RequestType::Sockets => {
// Fast queries: 50ms per node, min 200ms (or base_timeout if lower), max base_timeout
let min_timeout = std::cmp::min(200, base_timeout);
let adaptive = (50 * node_count as u64).clamp(min_timeout, base_timeout);
Duration::from_millis(adaptive)
}
RequestType::ChannelMembers | RequestType::ChannelMembersCount => {
// Presence queries need more time for data aggregation
Duration::from_millis(base_timeout)
}
_ => {
// All other queries use base timeout
Duration::from_millis(base_timeout)
}
};
let max_expected_responses = node_count.saturating_sub(1);
if max_expected_responses == 0 {
self.horizontal
.read()
.await
.pending_requests
.remove(&request_id);
return Ok(ResponseBody {
request_id,
node_id: request.node_id,
app_id: app_id.to_string(),
members: HashMap::new(),
socket_ids: Vec::new(),
sockets_count: 0,
channels_with_sockets_count: HashMap::new(),
exists: false,
channels: HashSet::new(),
members_count: 0,
});
}
// Wait for responses using event-driven approach
let start = Instant::now();
let deadline = start + timeout_duration;
let notify = {
let horizontal = self.horizontal.read().await;
horizontal
.pending_requests
.get(&request_id)
.map(|req| req.notify.clone())
.ok_or_else(|| {
Error::Other(format!(
"Request {request_id} not found in pending requests"
))
})?
};
let responses = loop {
// Wait for notification or timeout
let result = tokio::select! {
_ = notify.notified() => {
// Check if we have enough responses OR can return early
let horizontal = self.horizontal.read().await;
if let Some(pending_request) = horizontal.pending_requests.get(&request_id) {
let response_count = pending_request.responses.len();
// Early return optimization: Return immediately when we find a positive result
// This dramatically improves latency for existence checks in large clusters
// Note: ChannelSockets must wait for all responses to get complete socket list
let can_early_return = match request_type {
RequestType::SocketExistsInChannel if response_count > 0 => {
// Check if any response is positive
pending_request.responses.iter().any(|r| r.exists)
}
_ => false,
};
if response_count >= max_expected_responses || can_early_return {
if can_early_return {
debug!(
"Request {} early return with {}/{} responses in {}ms (found positive result)",
request_id,
response_count,
max_expected_responses,
start.elapsed().as_millis()
);
} else {
debug!(
"Request {} completed with {}/{} responses in {}ms",
request_id,
response_count,
max_expected_responses,
start.elapsed().as_millis()
);
}
// Extract responses without removing the entry yet to avoid race condition
let responses = pending_request.responses.clone();
Some(responses)
} else {
None // Continue waiting
}
} else {
return Err(Error::Other(format!(
"Request {request_id} was removed unexpectedly"
)));
}
}
_ = tokio::time::sleep_until(deadline.into()) => {
// Timeout occurred
warn!(
"Request {} timed out after {}ms",
request_id,
start.elapsed().as_millis()
);
let horizontal = self.horizontal.read().await;
let responses = if let Some(pending_request) = horizontal.pending_requests.get(&request_id) {
pending_request.responses.clone()
} else {
Vec::new()
};
Some(responses)
}
};
if let Some(responses) = result {
break responses;
}
// If result is None, continue waiting (notification came but not enough responses yet)
};
// Aggregate responses first, then clean up to prevent race condition
let combined_response = {
let horizontal = self.horizontal.read().await;
horizontal.aggregate_responses(
request_id.clone(),
request.node_id,
app_id.to_string(),
&request_type,
responses,
)
}; // horizontal lock released here
// Clean up the pending request after aggregation is complete
{
let horizontal = self.horizontal.read().await;
horizontal.pending_requests.remove(&request_id);
}
// Track metrics
{
let horizontal = self.horizontal.read().await;
if let Some(metrics_ref) = &horizontal.metrics {
let metrics = metrics_ref.lock().await;
let duration_ms = start.elapsed().as_micros() as f64 / 1000.0; // Convert to milliseconds with 3 decimal places
metrics.track_horizontal_adapter_resolve_time(app_id, duration_ms);
let resolved = combined_response.sockets_count > 0
|| !combined_response.members.is_empty()
|| combined_response.exists
|| !combined_response.channels.is_empty()
|| combined_response.members_count > 0
|| !combined_response.channels_with_sockets_count.is_empty();
metrics.track_horizontal_adapter_resolved_promises(app_id, resolved);
}
} // horizontal and metrics locks released here
Ok(combined_response)
}
pub async fn start_listeners(&self) -> Result<()> {
{
let horizontal = self.horizontal.read().await;
horizontal.start_request_cleanup();
}
// Start cluster health system only if enabled
if self.cluster_health_enabled {
self.start_cluster_health_system().await;
}
// Set up transport handlers
let horizontal_arc = self.horizontal.clone();
let broadcast_horizontal = horizontal_arc.clone();
let request_horizontal = horizontal_arc.clone();
let response_horizontal = horizontal_arc.clone();
let transport_for_request = self.transport.clone();
let handlers = TransportHandlers {
on_broadcast: Arc::new(move |broadcast| {
let horizontal_clone = broadcast_horizontal.clone();
Box::pin(async move {
let node_id = {
let horizontal = horizontal_clone.read().await;
horizontal.node_id.clone()
};
if broadcast.node_id == node_id {
return;
}
if let Ok(message) = sonic_rs::from_str::<PusherMessage>(&broadcast.message) {
// Debug log for tag filtering diagnostics
tracing::debug!(
"Received broadcast from node {}: channel={}, event={:?}, tags={:?}",
broadcast.node_id,
broadcast.channel,
message.event,
message.tags
);
let except_id = broadcast
.except_socket_id
.as_ref()
.and_then(|id| SocketId::from_string(id).ok());
let horizontal_lock = horizontal_clone.read().await;
// Log tag filtering status
let tag_filtering_enabled =
horizontal_lock.local_adapter.is_tag_filtering_enabled();
tracing::debug!(
"Tag filtering enabled on this node: {}",
tag_filtering_enabled
);
// Count local recipients for this node (adjusts for excluded socket)
let local_recipient_count = horizontal_lock
.get_local_recipient_count(
&broadcast.app_id,
&broadcast.channel,
except_id.as_ref(),
)
.await;
// Check if compression metadata is present and delta compression is enabled
let (send_result, compression_used) = if let Some(ref compression_meta) =
broadcast.compression_metadata
&& compression_meta.enabled
{
// Try to get delta compression manager and app manager (lock-free via OnceLock)
let delta_compression_opt =
horizontal_lock.local_adapter.get_delta_compression();
let app_manager_opt = horizontal_lock.local_adapter.get_app_manager();
// Only proceed with compression if both are available
if let (Some(delta_compression), Some(app_manager)) =
(delta_compression_opt.cloned(), app_manager_opt.cloned())
{
// Try to get channel-specific settings
let channel_settings = if let Ok(Some(app)) =
app_manager.find_by_id(&broadcast.app_id).await
{
app.channel_delta_compression
.as_ref()
.and_then(|map| map.get(&broadcast.channel))
.and_then(|config| {
use crate::delta_compression::ChannelDeltaConfig;
match config {
ChannelDeltaConfig::Full(settings) => {
// Use the conflation key from compression metadata
let mut settings = settings.clone();
if compression_meta.conflation_key.is_some() {
settings.conflation_key =
compression_meta.conflation_key.clone();
}
Some(settings)
}
_ => None,
}
})
} else {
None
};
// Use compression-aware sending
let result = horizontal_lock
.local_adapter
.send_with_compression(
&broadcast.channel,
message,
except_id.as_ref(),
&broadcast.app_id,
broadcast.timestamp_ms,
crate::adapter::connection_manager::CompressionParams {
delta_compression,
channel_settings: channel_settings.as_ref(),
},
)
.await;
(result, true)
} else {
// Delta compression not available, use regular send
let result = horizontal_lock
.local_adapter
.send(
&broadcast.channel,
message,
except_id.as_ref(),
&broadcast.app_id,
broadcast.timestamp_ms,
)
.await;
(result, false)
}
} else {
// No compression metadata or compression not enabled, use regular send
let result = horizontal_lock
.local_adapter
.send(
&broadcast.channel,
message,
except_id.as_ref(),
&broadcast.app_id,
broadcast.timestamp_ms,
)
.await;
(result, false)
};
// Track delta compression metrics if compression was used
if compression_used && let Some(ref metrics) = horizontal_lock.metrics {
let metrics_lock = metrics.lock().await;
metrics_lock.track_horizontal_delta_compression(
&broadcast.app_id,
&broadcast.channel,
true,
);
}
// Track broadcast latency metrics using helper function
let metrics_ref = horizontal_lock.metrics.clone();
drop(horizontal_lock); // Release horizontal lock to avoid deadlock
HorizontalAdapter::track_broadcast_latency_if_successful(
&send_result,
broadcast.timestamp_ms,
Some(local_recipient_count), // Use local count, not sender's count
&broadcast.app_id,
&broadcast.channel,
metrics_ref,
)
.await;
}
})
}),
on_request: Arc::new(move |request| {
let horizontal_clone = request_horizontal.clone();
let transport_clone = transport_for_request.clone();
Box::pin(async move {
let node_id = {
let horizontal = horizontal_clone.read().await;
horizontal.node_id.clone()
};
if request.node_id == node_id {
return Err(Error::OwnRequestIgnored);
}
// Check if this is a targeted request for another node
if let Some(ref target_node) = request.target_node_id
&& target_node != &node_id
{
// Not for us, ignore silently
return Err(Error::RequestNotForThisNode);
}
let horizontal_lock = horizontal_clone.read().await;
let response = horizontal_lock.process_request(request.clone()).await?;
// Check if this was a heartbeat that detected a new node
if request.request_type == RequestType::Heartbeat && response.exists {
// New node detected, send our presence state to it
// Spawn task to avoid blocking request processing
let new_node_id = request.node_id.clone();
let horizontal_clone_for_task = horizontal_clone.clone();
let transport_for_task = transport_clone.clone();
tokio::spawn(async move {
// Small delay to let the new node finish initialization
tokio::time::sleep(Duration::from_millis(100)).await;
if let Err(e) = send_presence_state_to_node(
&horizontal_clone_for_task,
&transport_for_task,
&new_node_id,
)
.await
{
error!(
"Failed to send presence state to new node {}: {}",
new_node_id, e
);
}
});
}
Ok(response)
})
}),
on_response: Arc::new(move |response| {
let horizontal_clone = response_horizontal.clone();
Box::pin(async move {
let node_id = {
let horizontal = horizontal_clone.read().await;
horizontal.node_id.clone()
};
if response.node_id == node_id {
return;
}
let horizontal_lock = horizontal_clone.read().await;
let _ = horizontal_lock.process_response(response).await;
})
}),
};
self.transport.start_listeners(handlers).await?;
Ok(())
}
/// Start cluster health monitoring system
pub async fn start_cluster_health_system(&self) {
let heartbeat_interval_ms = self.heartbeat_interval_ms;
let node_timeout_ms = self.node_timeout_ms;
info!(
"Starting cluster health system with heartbeat interval: {}ms, timeout: {}ms",
heartbeat_interval_ms, node_timeout_ms
);
// Start heartbeat broadcasting
self.start_heartbeat_loop().await;
// Start dead node detection
self.start_dead_node_detection().await;
}
/// Start heartbeat broadcasting loop
async fn start_heartbeat_loop(&self) {
let transport = self.transport.clone();
let node_id = self.node_id.clone();
let heartbeat_interval_ms = self.heartbeat_interval_ms;
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(heartbeat_interval_ms));
loop {
interval.tick().await;
let heartbeat_request = RequestBody {
request_id: generate_request_id(),
node_id: node_id.clone(),
app_id: "cluster".to_string(),
request_type: RequestType::Heartbeat,
channel: None,
socket_id: None,
user_id: None,
user_info: None,
timestamp: Some(current_timestamp()),
dead_node_id: None,
target_node_id: None,
};
if let Err(e) = transport.publish_request(&heartbeat_request).await {
error!("Failed to send heartbeat: {}", e);
}
}
});
}
/// Start dead node detection loop
async fn start_dead_node_detection(&self) {
let transport = self.transport.clone();
let horizontal = self.horizontal.clone();
let event_bus = self.event_bus.clone();
let node_id = self.node_id.clone();
let cleanup_interval_ms = self.cleanup_interval_ms;
let node_timeout_ms = self.node_timeout_ms;
let cluster_health_enabled = self.cluster_health_enabled;
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(cleanup_interval_ms));
loop {
interval.tick().await;
let dead_nodes = {
let horizontal_guard = horizontal.read().await;
horizontal_guard.get_dead_nodes(node_timeout_ms).await
};
if !dead_nodes.is_empty() {
// Single leader election for entire cleanup round
let is_leader = {
let horizontal_guard = horizontal.read().await;
horizontal_guard.is_cleanup_leader(&dead_nodes).await
};
if is_leader {
info!(
"Elected as cleanup leader for {} dead nodes: {:?}",
dead_nodes.len(),
dead_nodes
);
// Process ALL dead nodes as the elected leader
for dead_node_id in dead_nodes {
debug!("Processing dead node: {}", dead_node_id);
// 1. Remove from local heartbeat tracking
{
let horizontal_guard = horizontal.read().await;
horizontal_guard.remove_dead_node(&dead_node_id).await;
}
// 2. Get orphaned members and clean up local registry
let cleanup_tasks = {
let horizontal_guard = horizontal.read().await;
match horizontal_guard
.handle_dead_node_cleanup(&dead_node_id)
.await
{
Ok(tasks) => tasks,
Err(e) => {
error!(
"Failed to cleanup dead node {}: {}",
dead_node_id, e
);
continue;
}
}
};
if !cleanup_tasks.is_empty() {
// 3. Convert to orphaned members and emit event
let orphaned_members: Vec<OrphanedMember> = cleanup_tasks
.into_iter()
.map(|(app_id, channel, user_id, user_info)| OrphanedMember {
app_id,
channel,
user_id,
user_info, // Preserve user_info for proper presence management
})
.collect();
let event = DeadNodeEvent {
dead_node_id: dead_node_id.clone(),
orphaned_members: orphaned_members.clone(),
};
// Emit event for processing by ConnectionHandler (lock-free read via OnceLock)
if let Some(event_sender) = event_bus.get() {
if let Err(e) = event_sender.send(event) {
error!("Failed to send dead node event: {}", e);
}
} else {
warn!("No event bus configured for dead node cleanup");
}
info!(
"Emitted cleanup event for {} orphaned presence members from dead node {}",
orphaned_members.len(),
dead_node_id
);
}
// 4. Notify followers to clean their registries (skip if single node)
let should_skip = should_skip_horizontal_communication_impl(
cluster_health_enabled,
&horizontal,
)
.await;
if !should_skip {
let dead_node_request = RequestBody {
request_id: generate_request_id(),
node_id: node_id.clone(),
app_id: "cluster".to_string(),
request_type: RequestType::NodeDead,
channel: None,
socket_id: None,
user_id: None,
user_info: None,
timestamp: Some(current_timestamp()),
dead_node_id: Some(dead_node_id.clone()),
target_node_id: None,
};
if let Err(e) = transport.publish_request(&dead_node_request).await
{
error!("Failed to send dead node notification: {}", e);
}
}
}
} else {
debug!(
"Not cleanup leader for {} dead nodes, waiting for leader's broadcasts",
dead_nodes.len()
);
}
}
}
});
}
/// Get a snapshot of the cluster presence registry
pub async fn get_cluster_presence_registry(
&self,
) -> crate::adapter::horizontal_adapter::ClusterPresenceRegistry {
let horizontal = self.horizontal.read().await;
let registry = horizontal.cluster_presence_registry.read().await;
registry.clone()
}
/// Get a snapshot of node heartbeat tracking
pub async fn get_node_heartbeats(&self) -> HashMap<String, Instant> {
let horizontal = self.horizontal.read().await;
let heartbeats = horizontal.node_heartbeats.read().await;
heartbeats.clone()
}
}
#[async_trait]
impl<T: HorizontalTransport + 'static> ConnectionManager for HorizontalAdapterBase<T>
where
T::Config: TransportConfig,
{
async fn init(&self) {
self.local_adapter.init().await;
if let Err(e) = self.start_listeners().await {
error!("Failed to start transport listeners: {}", e);
}
}
async fn get_namespace(&self, app_id: &str) -> Option<Arc<Namespace>> {
self.local_adapter.get_namespace(app_id).await
}
async fn add_socket(
&self,
socket_id: SocketId,
socket: WebSocketWriter,
app_id: &str,
app_manager: Arc<dyn AppManager + Send + Sync>,
buffer_config: crate::websocket::WebSocketBufferConfig,
) -> Result<()> {
self.local_adapter
.add_socket(socket_id, socket, app_id, app_manager, buffer_config)
.await
}
async fn get_connection(&self, socket_id: &SocketId, app_id: &str) -> Option<WebSocketRef> {
self.local_adapter.get_connection(socket_id, app_id).await
}
async fn remove_connection(&self, socket_id: &SocketId, app_id: &str) -> Result<()> {
self.local_adapter
.remove_connection(socket_id, app_id)
.await
}
async fn send_message(
&self,
app_id: &str,
socket_id: &SocketId,
message: PusherMessage,
) -> Result<()> {
self.local_adapter
.send_message(app_id, socket_id, message)
.await
}
async fn send(
&self,
channel: &str,
message: PusherMessage,
except: Option<&SocketId>,
app_id: &str,
start_time_ms: Option<f64>,
) -> Result<()> {
// Check if delta compression is available and configured for this channel
if let (Some(delta_compression), Some(app_manager)) =
(&self.delta_compression, &self.app_manager)
{
// Get app config to check for channel-specific delta settings
if let Ok(Some(app)) = app_manager.find_by_id(app_id).await {
// Get channel-specific delta compression settings
let channel_settings = app
.channel_delta_compression
.as_ref()
.and_then(|map| map.get(channel))
.and_then(|config| {
use crate::delta_compression::ChannelDeltaConfig;
match config {
ChannelDeltaConfig::Full(settings) => Some(settings.clone()),
_ => None,
}
});
// Use compression-aware sending if we have settings with conflation key
if channel_settings
.as_ref()
.and_then(|s| s.conflation_key.as_ref())
.is_some()
{
return self
.send_with_compression(
channel,
message,
except,
app_id,
start_time_ms,
crate::adapter::connection_manager::CompressionParams {
delta_compression: Arc::clone(delta_compression),
channel_settings: channel_settings.as_ref(),
},
)
.await;
}
}
}
// Fall back to regular sending without delta compression
// Send locally first (tracked in connection manager for metrics)
let node_id = {
let horizontal_lock = self.horizontal.read().await;
horizontal_lock.node_id.clone()
};
let local_result = self
.local_adapter
.send(channel, message.clone(), except, app_id, start_time_ms)
.await;
if let Err(e) = local_result {
warn!("Local send failed for channel {}: {}", channel, e);
}
// Broadcast to other nodes
let message_json = sonic_rs::to_string(&message)?;
let broadcast = BroadcastMessage {
node_id,
app_id: app_id.to_string(),
channel: channel.to_string(),
message: message_json,
except_socket_id: except.map(|id| id.to_string()),
timestamp_ms: start_time_ms.or_else(|| {
Some(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos() as f64
/ 1_000_000.0, // Convert to milliseconds with microsecond precision
)
}),
compression_metadata: None,
};
// Skip broadcasting to other nodes if we're in single-node mode
if !self.should_skip_horizontal_communication().await {
self.transport.publish_broadcast(&broadcast).await?;
}
Ok(())
}
async fn send_with_compression(
&self,
channel: &str,
message: PusherMessage,
except: Option<&SocketId>,
app_id: &str,
start_time_ms: Option<f64>,
compression: crate::adapter::connection_manager::CompressionParams<'_>,
) -> Result<()> {
// Send locally first with delta compression support
let (node_id, local_result) = {
let horizontal_lock = self.horizontal.read().await;
let result = horizontal_lock
.local_adapter
.send_with_compression(
channel,
message.clone(),
except,
app_id,
start_time_ms,
crate::adapter::connection_manager::CompressionParams {
delta_compression: compression.delta_compression.clone(),
channel_settings: compression.channel_settings,
},
)
.await;
(horizontal_lock.node_id.clone(), result)
};
if let Err(e) = local_result {
warn!(
"Local send with compression failed for channel {}: {}",
channel, e
);
}
// Broadcast to other nodes with compression metadata
// Other nodes will apply their own delta compression using this metadata
let message_json = sonic_rs::to_string(&message)?;
// Extract conflation key from channel settings
let conflation_key = compression
.channel_settings
.and_then(|s| s.conflation_key.clone());
// Extract event name from message for tracking
let event_name = message.event.as_deref().map(|s| s.to_string());
// Check cluster coordination for synchronized full message intervals
let (cluster_should_send_full, cluster_delta_count) = if compression
.delta_compression
.has_cluster_coordination()
{
if let Some(ck) = conflation_key.as_ref() {
// Use cluster coordination to determine if we should send full message
match compression
.delta_compression
.check_cluster_interval(app_id, channel, ck)
.await
{
Ok((should_send_full, count)) => {
debug!(
"Cluster coordination: should_send_full={}, count={} for app={}, channel={}, key={}",
should_send_full, count, app_id, channel, ck
);
(Some(should_send_full), Some(count))
}
Err(e) => {
warn!(
"Cluster coordination failed: {}, falling back to node-local",
e
);
(None, None)
}
}
} else {
(None, None)
}
} else {
(None, None)
};
// For horizontal broadcasts, we send full messages and let each node
// decide whether to apply delta compression based on its local state.
// If cluster coordination is enabled, we use the cluster-wide decision.
let is_full_message = cluster_should_send_full.unwrap_or(true);
let broadcast = BroadcastMessage {
node_id,
app_id: app_id.to_string(),
channel: channel.to_string(),
message: message_json,
except_socket_id: except.map(|id| id.to_string()),
timestamp_ms: start_time_ms.or_else(|| {
Some(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos() as f64
/ 1_000_000.0,
)
}),
compression_metadata: Some(crate::adapter::horizontal_adapter::CompressionMetadata {
conflation_key,
enabled: true,
sequence: cluster_delta_count, // Cluster-wide sequence if coordination enabled
is_full_message, // Determined by cluster coordination or defaults to true
event_name,
}),
};
// Skip broadcasting to other nodes if we're in single-node mode
if !self.should_skip_horizontal_communication().await {
self.transport.publish_broadcast(&broadcast).await?;
}
Ok(())
}
async fn get_channel_members(
&self,
app_id: &str,
channel: &str,
) -> Result<HashMap<String, PresenceMemberInfo>> {
// Get local members
let mut members = self
.local_adapter
.get_channel_members(app_id, channel)
.await?;
// Get distributed members
let response = self
.send_request(
app_id,
RequestType::ChannelMembers,
Some(channel),
None,
None,
)
.await?;
members.extend(response.members);
Ok(members)
}
async fn get_channel_sockets(&self, app_id: &str, channel: &str) -> Result<Vec<SocketId>> {
// Get local sockets
let mut all_socket_ids = self
.local_adapter
.get_channel_sockets(app_id, channel)
.await?;
// Get remote sockets
let response = self
.send_request(
app_id,
RequestType::ChannelSockets,
Some(channel),
None,
None,
)
.await?;
for socket_id in response.socket_ids {
if let Ok(sid) = SocketId::from_string(&socket_id) {
all_socket_ids.push(sid);
}
}
Ok(all_socket_ids)
}
async fn remove_channel(&self, app_id: &str, channel: &str) {
self.local_adapter.remove_channel(app_id, channel).await;
}
async fn is_in_channel(
&self,
app_id: &str,
channel: &str,
socket_id: &SocketId,
) -> Result<bool> {
// Check locally first
let local_result = self
.local_adapter
.is_in_channel(app_id, channel, socket_id)
.await?;
if local_result {
return Ok(true);
}
// Check other nodes
let response = self
.send_request(
app_id,
RequestType::SocketExistsInChannel,
Some(channel),
Some(&socket_id.to_string()),
None,
)
.await?;
Ok(response.exists)
}
async fn get_user_sockets(&self, user_id: &str, app_id: &str) -> Result<Vec<WebSocketRef>> {
self.local_adapter.get_user_sockets(user_id, app_id).await
}
async fn cleanup_connection(&self, app_id: &str, ws: WebSocketRef) {
self.local_adapter.cleanup_connection(app_id, ws).await;
}
async fn terminate_connection(&self, app_id: &str, user_id: &str) -> Result<()> {
// Terminate locally
self.local_adapter
.terminate_user_connections(app_id, user_id)
.await?;
// Broadcast termination to other nodes
let _response = self
.send_request(
app_id,
RequestType::TerminateUserConnections,
None,
None,
Some(user_id),
)
.await?;
Ok(())
}
async fn add_channel_to_sockets(&self, app_id: &str, channel: &str, socket_id: &SocketId) {
self.local_adapter
.add_channel_to_sockets(app_id, channel, socket_id)
.await;
}
async fn get_channel_socket_count(&self, app_id: &str, channel: &str) -> usize {
// Get local count
let local_count = self
.local_adapter
.get_channel_socket_count(app_id, channel)
.await;
// Get distributed count
match self
.send_request(
app_id,
RequestType::ChannelSocketsCount,
Some(channel),
None,
None,
)
.await
{
Ok(response) => local_count + response.sockets_count,
Err(e) => {
error!("Failed to get remote channel socket count: {}", e);
local_count
}
}
}
async fn add_to_channel(
&self,
app_id: &str,
channel: &str,
socket_id: &SocketId,
) -> Result<bool> {
// Fast path: direct local adapter access without locking horizontal
self.local_adapter
.add_to_channel(app_id, channel, socket_id)
.await
}
async fn remove_from_channel(
&self,
app_id: &str,
channel: &str,
socket_id: &SocketId,
) -> Result<bool> {
// Fast path: direct local adapter access without locking horizontal
self.local_adapter
.remove_from_channel(app_id, channel, socket_id)
.await
}
async fn get_presence_member(
&self,
app_id: &str,
channel: &str,
socket_id: &SocketId,
) -> Option<PresenceMemberInfo> {
self.local_adapter
.get_presence_member(app_id, channel, socket_id)
.await
}
async fn terminate_user_connections(&self, app_id: &str, user_id: &str) -> Result<()> {
self.terminate_connection(app_id, user_id).await
}
async fn add_user(&self, ws_ref: WebSocketRef) -> Result<()> {
self.local_adapter.add_user(ws_ref).await
}
async fn remove_user(&self, ws_ref: WebSocketRef) -> Result<()> {
self.local_adapter.remove_user(ws_ref).await
}
async fn remove_user_socket(
&self,
user_id: &str,
socket_id: &SocketId,
app_id: &str,
) -> Result<()> {
self.local_adapter
.remove_user_socket(user_id, socket_id, app_id)
.await
}
async fn count_user_connections_in_channel(
&self,
user_id: &str,
app_id: &str,
channel: &str,
excluding_socket: Option<&SocketId>,
) -> Result<usize> {
// Get local count (with excluding_socket filter)
let local_count = self
.local_adapter
.count_user_connections_in_channel(user_id, app_id, channel, excluding_socket)
.await?;
// Get remote count (no excluding_socket since it's local-only)
match self
.send_request(
app_id,
RequestType::CountUserConnectionsInChannel,
Some(channel),
None,
Some(user_id),
)
.await
{
Ok(response) => Ok(local_count + response.sockets_count),
Err(e) => {
error!("Failed to get remote user connections count: {}", e);
Ok(local_count)
}
}
}
async fn get_channels_with_socket_count(&self, app_id: &str) -> Result<HashMap<String, usize>> {
// Get local channels
let mut channels = self
.local_adapter
.get_channels_with_socket_count(app_id)
.await?;
// Get distributed channels
match self
.send_request(
app_id,
RequestType::ChannelsWithSocketsCount,
None,
None,
None,
)
.await
{
Ok(response) => {
for (channel, count) in response.channels_with_sockets_count {
*channels.entry(channel).or_insert(0) += count;
}
}
Err(e) => {
error!("Failed to get remote channels with socket count: {}", e);
}
}
Ok(channels)
}
async fn get_sockets_count(&self, app_id: &str) -> Result<usize> {
// Check if socket counting is enabled
if !self.enable_socket_counting {
return Ok(0);
}
// Get local count
let local_count = self.local_adapter.get_sockets_count(app_id).await?;
// Get distributed count
match self
.send_request(app_id, RequestType::SocketsCount, None, None, None)
.await
{
Ok(response) => Ok(local_count + response.sockets_count),
Err(e) => {
error!("Failed to get remote socket count: {}", e);
Ok(local_count)
}
}
}
async fn get_namespaces(&self) -> Result<Vec<(String, Arc<Namespace>)>> {
self.local_adapter.get_namespaces().await
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
async fn check_health(&self) -> Result<()> {
self.transport.check_health().await
}
fn get_node_id(&self) -> String {
self.node_id.clone()
}
fn as_horizontal_adapter(&self) -> Option<&dyn HorizontalAdapterInterface> {
Some(self)
}
fn configure_dead_node_events(
&self,
) -> Option<tokio::sync::mpsc::UnboundedReceiver<DeadNodeEvent>> {
let (event_sender, event_receiver) = tokio::sync::mpsc::unbounded_channel();
self.set_event_bus(event_sender);
Some(event_receiver)
}
}
#[async_trait]
impl<T: HorizontalTransport> HorizontalAdapterInterface for HorizontalAdapterBase<T> {
/// Broadcast presence member joined to all nodes
async fn broadcast_presence_join(
&self,
app_id: &str,
channel: &str,
user_id: &str,
socket_id: &str,
user_info: Option<sonic_rs::Value>,
) -> Result<()> {
// Store in our own registry first with a single lock acquisition
{
let horizontal = self.horizontal.read().await;
horizontal
.add_presence_entry(
&self.node_id,
channel,
socket_id,
user_id,
app_id,
user_info.clone(),
)
.await;
}
// Skip cluster broadcast if cluster health is disabled
if !self.cluster_health_enabled {
return Ok(());
}
let request = RequestBody {
request_id: crate::adapter::horizontal_adapter::generate_request_id(),
node_id: self.node_id.clone(),
app_id: app_id.to_string(),
request_type: RequestType::PresenceMemberJoined,
channel: Some(channel.to_string()),
socket_id: Some(socket_id.to_string()),
user_id: Some(user_id.to_string()),
// Cluster presence fields
user_info,
timestamp: None,
dead_node_id: None,
target_node_id: None,
};
// Send without waiting for response (broadcast) - skip if single node
if !self.should_skip_horizontal_communication().await {
self.transport.publish_request(&request).await
} else {
Ok(())
}
}
/// Broadcast presence member left to all nodes
async fn broadcast_presence_leave(
&self,
app_id: &str,
channel: &str,
user_id: &str,
socket_id: &str,
) -> Result<()> {
// Remove from our own registry first with a single lock acquisition
{
let horizontal = self.horizontal.read().await;
horizontal
.remove_presence_entry(&self.node_id, channel, socket_id)
.await;
}
// Skip cluster broadcast if cluster health is disabled
if !self.cluster_health_enabled {
return Ok(());
}
let request = RequestBody {
request_id: crate::adapter::horizontal_adapter::generate_request_id(),
node_id: self.node_id.clone(),
app_id: app_id.to_string(),
request_type: RequestType::PresenceMemberLeft,
channel: Some(channel.to_string()),
socket_id: Some(socket_id.to_string()),
user_id: Some(user_id.to_string()),
// Cluster presence fields
user_info: None,
timestamp: None,
dead_node_id: None,
target_node_id: None,
};
// Send without waiting for response (broadcast) - skip if single node
if !self.should_skip_horizontal_communication().await {
self.transport.publish_request(&request).await
} else {
Ok(())
}
}
}
/// Helper function to send presence state to a new node
async fn send_presence_state_to_node<T: HorizontalTransport>(
horizontal: &Arc<tokio::sync::RwLock<HorizontalAdapter>>,
transport: &T,
target_node_id: &str,
) -> Result<()> {
// Get our presence data
let (our_node_id, data_to_send) = {
let horizontal_lock = horizontal.read().await;
let registry = horizontal_lock.cluster_presence_registry.read().await;
// Get only our node's data
if let Some(our_presence_data) = registry.get(&horizontal_lock.node_id) {
// Clone the data to avoid holding the lock
(
horizontal_lock.node_id.clone(),
Some(our_presence_data.clone()),
)
} else {
(horizontal_lock.node_id.clone(), None)
}
};
if let Some(data_to_send) = data_to_send {
// Serialize the presence data
let serialized_data = sonic_rs::to_value(&data_to_send)?;
let sync_request = RequestBody {
request_id: generate_request_id(),
node_id: our_node_id,
app_id: "cluster".to_string(),
request_type: RequestType::PresenceStateSync,
target_node_id: Some(target_node_id.to_string()),
user_info: Some(serialized_data), // Reuse this field for bulk data
channel: None,
socket_id: None,
user_id: None,
timestamp: None,
dead_node_id: None,
};
// This broadcasts but only target_node will process it
transport.publish_request(&sync_request).await?;
info!(
"Sent presence state to new node: {} ({} channels)",
target_node_id,
data_to_send.len()
);
} else {
debug!("No presence data to send to new node: {}", target_node_id);
}
Ok(())
}