fix: SSE libera worker en 28s y detecta desconexión cada 200ms
- TTL de 28s por conexión; EventSource reconecta automáticamente (retry:3000) - Reemplaza sleep(2) por loop usleep(200ms) para detectar pagehide rápido - PHP-FPM workers aumentados de 5 → 20 (directo en servidor) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
2082323586
commit
7b8a4c2056
@@ -202,12 +202,11 @@ HTML;
|
||||
header('X-Accel-Buffering: no'); // disable nginx buffering
|
||||
header('Connection: keep-alive');
|
||||
|
||||
set_time_limit(0);
|
||||
set_time_limit(35);
|
||||
ignore_user_abort(false);
|
||||
|
||||
$lastId = max(0, (int)($_GET['after'] ?? 0));
|
||||
|
||||
// Anchor to latest ID if no after param
|
||||
if ($lastId === 0) {
|
||||
try {
|
||||
$row = db()->query('SELECT id FROM webhook_logs ORDER BY id DESC LIMIT 1')->fetch();
|
||||
@@ -215,7 +214,13 @@ HTML;
|
||||
} catch (\PDOException $e) {}
|
||||
}
|
||||
|
||||
while (!connection_aborted()) {
|
||||
// Tell the client to reconnect after 3s if connection drops
|
||||
echo "retry: 3000\n\n";
|
||||
ob_flush(); flush();
|
||||
|
||||
$deadline = time() + 28; // release worker after 28s, client auto-reconnects
|
||||
|
||||
while (!connection_aborted() && time() < $deadline) {
|
||||
try {
|
||||
$stmt = db()->prepare(
|
||||
'SELECT id, event_field, from_number, contact_name,
|
||||
@@ -229,7 +234,7 @@ HTML;
|
||||
$lastId = (int)end($rows)['id'];
|
||||
echo 'data: ' . json_encode($rows, JSON_UNESCAPED_UNICODE) . "\n\n";
|
||||
} else {
|
||||
echo ": heartbeat\n\n"; // keep connection alive
|
||||
echo ": heartbeat\n\n";
|
||||
}
|
||||
} catch (\PDOException $e) {
|
||||
echo ": db-error\n\n";
|
||||
@@ -237,7 +242,11 @@ HTML;
|
||||
|
||||
ob_flush();
|
||||
flush();
|
||||
sleep(2);
|
||||
|
||||
// Sleep in 200ms chunks so connection_aborted() is checked frequently
|
||||
for ($i = 0; $i < 10 && !connection_aborted(); $i++) {
|
||||
usleep(200000);
|
||||
}
|
||||
}
|
||||
exit;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user