Skip to content

WebSocket — алерт когда disconnect-rate скачет

Real-time-чат работает, но disconnects/sec в три раза выше обычного — где-то проксирующий nginx стал рвать соединения. Нужен endpoint с rate.

Рецепт

js
// Track 60-second sliding window of disconnects
const WebSocket = require('ws');
const buckets = new Array(60).fill(0);
let cursor = 0;

setInterval(() => {
  cursor = (cursor + 1) % 60;
  buckets[cursor] = 0;
}, 1000);

const wss = new WebSocket.Server({ port: 8081 });
wss.on('connection', (ws) => {
  ws.on('close', () => { buckets[cursor]++; });
});

require('http').createServer((_req, res) => {
  const total = buckets.reduce((a, b) => a + b, 0);
  const threshold = parseInt(process.env.WS_DISC_THRESHOLD || '30', 10);
  res.end(total >= threshold ? `high ${total}` : `ok ${total}`);
}).listen(8082);

То же самое в Enterno.io

HTTP-монитор Enterno на :8082 с keyword "ok" даёт алерт ровно когда rate переходит порог. Pro+ хранит history, что помогает увидеть pattern (например — каждый день в 14:00 при auto-restart).

Настроить HTTP monitor → ← Все рецепты

Похожие рецепты

Нужно ловить момент, когда реплика начала отставать от мастера больше чем на 10 секунд.