Limit Hub polling and rotate VPS logs
This commit is contained in:
1 parent
fcde23bfc4
commit
927181dd54
6 files changed
+601
-17
No files matched your search
@@ -0,0 +1,17 @@
|
||||
# Notification long-poll and VPS log limits
|
||||
|
||||
The dashboard keeps one notification long-poll for the current browser profile.
|
||||
Tabs share a short lease in `localStorage`; the server also admits only one
|
||||
`/api/notifications/wait` request for a session and client id. A stale cursor
|
||||
after clearing or pruning the notification file returns an empty response with
|
||||
the current serial instead of replaying the whole list in a tight loop.
|
||||
|
||||
Successful long-poll and HTTP-terminal reads are omitted from the Hub stderr
|
||||
request log. HTTP errors remain logged. The nginx policy excludes only 2xx
|
||||
responses for these endpoints from `access.log`.
|
||||
|
||||
`install-vps.sh` and `deploy-request-load-fix.ps1` install the same bounded
|
||||
logging policy: journald is capped at 256 MiB with a seven-day retention limit,
|
||||
and nginx logs rotate daily with seven compressed archives and a 50 MiB trigger.
|
||||
The deploy script keeps a backup at
|
||||
`/root/owrt-remote-backup-before-request-load-v119` and supports Rollback.
|
||||
@@ -0,0 +1,99 @@
|
||||
"""Regression tests for notification long-poll admission and stale cursors."""
|
||||
import importlib.util
|
||||
import io
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import contextlib
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def load_hub():
|
||||
state = tempfile.TemporaryDirectory(prefix="notification-load-state-")
|
||||
os.environ["OWRT_REMOTE_STATE_DIR"] = state.name
|
||||
os.environ["OWRT_REMOTE_DB"] = str(Path(state.name) / "hub.db")
|
||||
spec = importlib.util.spec_from_file_location("notification_load_hub", ROOT / "vps" / "owrt-remote-hub.py")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
module._notification_test_state = state
|
||||
return module
|
||||
|
||||
|
||||
class NotificationLoadTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.hub = load_hub()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
state = getattr(cls.hub, "_notification_test_state", None)
|
||||
if state:
|
||||
state.cleanup()
|
||||
os.environ.pop("OWRT_REMOTE_STATE_DIR", None)
|
||||
os.environ.pop("OWRT_REMOTE_DB", None)
|
||||
|
||||
def test_stale_cursor_does_not_return_old_notifications(self):
|
||||
rows = [{"id": "one", "serial": 4, "ts": 1, "title": "old"}]
|
||||
with mock.patch.object(self.hub, "load_notifications", return_value=rows):
|
||||
self.assertEqual(self.hub.list_notifications(after_serial=99), [])
|
||||
|
||||
def test_same_client_can_have_only_one_active_long_poll(self):
|
||||
key = self.hub.notification_wait_context("session", "client-1234")
|
||||
self.assertTrue(self.hub.claim_notification_wait(key))
|
||||
self.assertFalse(self.hub.claim_notification_wait(key))
|
||||
self.hub.release_notification_wait(key)
|
||||
self.assertTrue(self.hub.claim_notification_wait(key))
|
||||
self.hub.release_notification_wait(key)
|
||||
|
||||
def test_metrics_are_bounded_and_released(self):
|
||||
key = self.hub.notification_wait_context("session", "client-5678")
|
||||
before = self.hub.notification_wait_metrics()["active"]
|
||||
self.assertTrue(self.hub.claim_notification_wait(key))
|
||||
self.assertEqual(self.hub.notification_wait_metrics()["active"], before + 1)
|
||||
self.hub.release_notification_wait(key)
|
||||
self.assertEqual(self.hub.notification_wait_metrics()["active"], before)
|
||||
|
||||
def test_concurrent_claims_have_one_winner(self):
|
||||
key = self.hub.notification_wait_context("session", "client-concurrent")
|
||||
winners = []
|
||||
barrier = threading.Barrier(8)
|
||||
|
||||
def claim():
|
||||
barrier.wait()
|
||||
if self.hub.claim_notification_wait(key):
|
||||
winners.append(True)
|
||||
|
||||
threads = [threading.Thread(target=claim) for _ in range(8)]
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
for thread in threads:
|
||||
thread.join(timeout=2)
|
||||
self.assertEqual(len(winners), 1)
|
||||
self.hub.release_notification_wait(key)
|
||||
|
||||
def test_successful_poll_logs_are_quiet_but_errors_are_kept(self):
|
||||
class FakeHandler:
|
||||
path = "/api/notifications/wait?client_id=test"
|
||||
|
||||
def address_string(self):
|
||||
return "127.0.0.1"
|
||||
|
||||
def log_date_time_string(self):
|
||||
return "now"
|
||||
|
||||
output = io.StringIO()
|
||||
with contextlib.redirect_stderr(output):
|
||||
with mock.patch.object(self.hub.sys, "stderr", output):
|
||||
self.hub.Handler.log_message(FakeHandler(), '"%s" %s %s', "GET", "200", "12")
|
||||
self.hub.Handler.log_message(FakeHandler(), '"%s" %s %s', "GET", "500", "12")
|
||||
self.assertIn("500", output.getvalue())
|
||||
self.assertNotIn("200", output.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Static validation for the local VPS request-load deployment script."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import unittest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class RequestLoadDeployTests(unittest.TestCase):
|
||||
def test_embedded_remote_script_is_valid_posix_shell(self):
|
||||
source = (ROOT / "vps" / "deploy-request-load-fix.ps1").read_text(encoding="utf-8")
|
||||
script = source.split("$RemoteScript = @'\n", 1)[1].split("\n'@", 1)[0]
|
||||
script = script.replace("__UPLOAD_NAME__", "test-stage").replace("__ACTION__", "Install")
|
||||
shell = shutil.which("sh")
|
||||
if os.name == "nt":
|
||||
shell = str(Path(os.environ.get("ProgramFiles", "C:/Program Files")) / "Git/usr/bin/sh.exe")
|
||||
if not shell or not Path(shell).exists():
|
||||
self.skipTest("POSIX shell required")
|
||||
result = subprocess.run([shell, "-n"], input=script, text=True, capture_output=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
def test_policy_and_rollback_markers_are_present(self):
|
||||
source = (ROOT / "vps" / "deploy-request-load-fix.ps1").read_text(encoding="utf-8")
|
||||
for marker in (
|
||||
"owrt-remote-log-policy.conf",
|
||||
"owrt-remote-limits.conf",
|
||||
"MaxRetentionSec=7day",
|
||||
"rotate 7",
|
||||
"REQUEST_LOAD_FIX_V119_",
|
||||
"restore_optional",
|
||||
):
|
||||
self.assertIn(marker, source)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user