2024-01-25 23:27:53 +08:00
|
|
|
import { useEffect, useRef } from 'react';
|
2024-04-01 20:36:28 +08:00
|
|
|
import { useMutation } from '@apollo/client';
|
|
|
|
import { UPDATE_CONNECTION_ALIVE_AT } from './mutations';
|
2023-12-05 22:18:45 +08:00
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const STATS_INTERVAL = window.meetingClientSettings.public.stats.interval;
|
2023-12-06 00:12:12 +08:00
|
|
|
|
2023-12-05 22:18:45 +08:00
|
|
|
const ConnectionStatus = () => {
|
2024-04-01 20:36:28 +08:00
|
|
|
const networkRttInMs = useRef(0); // Ref to store the last rtt
|
2024-01-25 23:27:53 +08:00
|
|
|
const timeoutRef = useRef(null);
|
|
|
|
|
2024-04-01 20:36:28 +08:00
|
|
|
const [updateConnectionAliveAtM] = useMutation(UPDATE_CONNECTION_ALIVE_AT);
|
2023-12-05 22:18:45 +08:00
|
|
|
|
2024-04-01 20:36:28 +08:00
|
|
|
const handleUpdateConnectionAliveAt = () => {
|
|
|
|
const startTime = performance.now();
|
|
|
|
updateConnectionAliveAtM({
|
2024-01-25 23:27:53 +08:00
|
|
|
variables: {
|
|
|
|
networkRttInMs: networkRttInMs.current,
|
|
|
|
},
|
2024-04-01 20:36:28 +08:00
|
|
|
}).then(() => {
|
2024-01-25 23:27:53 +08:00
|
|
|
const endTime = performance.now();
|
|
|
|
networkRttInMs.current = endTime - startTime;
|
|
|
|
}).finally(() => {
|
|
|
|
if (timeoutRef.current) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
}
|
2023-12-05 22:18:45 +08:00
|
|
|
|
2024-01-25 23:27:53 +08:00
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
handleUpdateConnectionAliveAt();
|
|
|
|
}, STATS_INTERVAL);
|
|
|
|
});
|
2023-12-05 22:18:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-01 20:36:28 +08:00
|
|
|
// Delay first connectionAlive to avoid high RTT misestimation
|
|
|
|
// due to initial subscription and mutation traffic at client render
|
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
handleUpdateConnectionAliveAt();
|
|
|
|
}, STATS_INTERVAL / 2);
|
2023-12-05 22:18:45 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ConnectionStatus;
|