feat(EasySwitchManager, FilterManager): implement location-based ping requests to refresh data on route changes
Publish Fast Tabler Docker image / build-and-push-fast (push) Failing after 2m56s
Publish Fast Tabler Docker image / build-and-push-fast (push) Failing after 2m56s
This commit is contained in:
@@ -4,16 +4,20 @@ import api from '../lib/api.js';
|
||||
/**
|
||||
* Единая точка входа для пингов до шлюзов (MikroTik).
|
||||
* Кеш: ключ `${routerId}:${gatewayIp}` → number | null (мс или ошибка).
|
||||
* TTL: после истечения записи перезапрашиваются при переходе на раздел.
|
||||
* Используется и на Easy Switch, и на странице фильтров (карточки gateway).
|
||||
*/
|
||||
const PingContext = createContext(null);
|
||||
|
||||
const PING_COUNT = 5;
|
||||
/** Время жизни кеша пинга (мс). После истечения при открытии раздела данные перезапрашиваются. */
|
||||
const PING_CACHE_TTL_MS = 2 * 60 * 1000; // 2 минуты
|
||||
|
||||
export function PingProvider({ children }) {
|
||||
const [pingMap, setPingMap] = useState({});
|
||||
const inFlightRef = useRef(new Set());
|
||||
const pingMapRef = useRef(pingMap);
|
||||
const timestampsRef = useRef({}); // key → Date.now() когда запись установлена
|
||||
pingMapRef.current = pingMap;
|
||||
|
||||
const getKey = (routerId, gatewayIp) => {
|
||||
@@ -21,6 +25,14 @@ export function PingProvider({ children }) {
|
||||
return `${routerId}:${gatewayIp}`;
|
||||
};
|
||||
|
||||
const isCacheValid = useCallback((key) => {
|
||||
const cache = pingMapRef.current;
|
||||
if (!Object.prototype.hasOwnProperty.call(cache, key)) return false;
|
||||
const ts = timestampsRef.current[key];
|
||||
if (ts == null) return false;
|
||||
return (Date.now() - ts) < PING_CACHE_TTL_MS;
|
||||
}, []);
|
||||
|
||||
const getPing = useCallback((routerId, gatewayIp) => {
|
||||
const key = getKey(routerId, gatewayIp);
|
||||
if (!key) return undefined;
|
||||
@@ -29,12 +41,11 @@ export function PingProvider({ children }) {
|
||||
|
||||
/**
|
||||
* Запросить пинги для списка пар (routerId, gatewayIp).
|
||||
* Уже закешированные или запрашиваемые ключи пропускаются. Один вызов API на ключ.
|
||||
* Пропускаются ключи с валидным кешем (не истёк TTL) и уже запрашиваемые.
|
||||
* @param tasks Array<{ routerId, gatewayIp }>
|
||||
*/
|
||||
const requestPings = useCallback((tasks) => {
|
||||
if (!Array.isArray(tasks) || tasks.length === 0) return;
|
||||
const cache = pingMapRef.current;
|
||||
|
||||
const toRequest = [];
|
||||
tasks.forEach(({ routerId, gatewayIp }) => {
|
||||
@@ -42,7 +53,7 @@ export function PingProvider({ children }) {
|
||||
if (!routerId || !ip) return;
|
||||
const key = getKey(routerId, ip);
|
||||
if (!key) return;
|
||||
if (Object.prototype.hasOwnProperty.call(cache, key)) return;
|
||||
if (isCacheValid(key)) return;
|
||||
if (inFlightRef.current.has(key)) return;
|
||||
inFlightRef.current.add(key);
|
||||
toRequest.push({ routerId, ip, key });
|
||||
@@ -57,17 +68,19 @@ export function PingProvider({ children }) {
|
||||
})
|
||||
.then(({ data }) => {
|
||||
const value = typeof data?.avgMs === 'number' ? Math.round(data.avgMs) : null;
|
||||
timestampsRef.current[key] = Date.now();
|
||||
setPingMap((prev) => ({ ...prev, [key]: value }));
|
||||
})
|
||||
.catch((e) => {
|
||||
console.warn('PingContext: failed to fetch ping for', routerId, ip, e?.message || e);
|
||||
timestampsRef.current[key] = Date.now();
|
||||
setPingMap((prev) => ({ ...prev, [key]: null }));
|
||||
})
|
||||
.finally(() => {
|
||||
inFlightRef.current.delete(key);
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
}, [isCacheValid]);
|
||||
|
||||
const value = {
|
||||
pingMap,
|
||||
|
||||
Reference in New Issue
Block a user