mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-27 15:57:07 +03:00
serialize push subscription cleanup and test malformed tokens
This commit is contained in:
+24
-22
@@ -181,6 +181,7 @@ class WebPushClient(Communicator):
|
|||||||
self.web_pushers: dict[str, list[WebPusher]] = {}
|
self.web_pushers: dict[str, list[WebPusher]] = {}
|
||||||
self.expired_subs: dict[str, list[str]] = {}
|
self.expired_subs: dict[str, list[str]] = {}
|
||||||
self.expired_subs_lock = threading.Lock()
|
self.expired_subs_lock = threading.Lock()
|
||||||
|
self.cleanup_lock = threading.Lock()
|
||||||
self.suspended_cameras: dict[str, int] = {
|
self.suspended_cameras: dict[str, int] = {
|
||||||
c.name: 0 # type: ignore[misc]
|
c.name: 0 # type: ignore[misc]
|
||||||
for c in self.config.cameras.values()
|
for c in self.config.cameras.values()
|
||||||
@@ -261,36 +262,37 @@ class WebPushClient(Communicator):
|
|||||||
self.claim_headers[endpoint] = self.vapid.sign(claim)
|
self.claim_headers[endpoint] = self.vapid.sign(claim)
|
||||||
|
|
||||||
def cleanup_registrations(self) -> None:
|
def cleanup_registrations(self) -> None:
|
||||||
# delete any expired subs
|
# concurrent cleanups would each restore what the other removed
|
||||||
with self.expired_subs_lock:
|
with self.cleanup_lock:
|
||||||
expired_subs, self.expired_subs = self.expired_subs, {}
|
with self.expired_subs_lock:
|
||||||
|
expired_subs, self.expired_subs = self.expired_subs, {}
|
||||||
|
|
||||||
for user, expired in expired_subs.items():
|
for user, expired in expired_subs.items():
|
||||||
user_subs = []
|
user_subs = []
|
||||||
|
|
||||||
# get all subscriptions, removing ones that are expired
|
# get all subscriptions, removing ones that are expired
|
||||||
stored_user: User | None = User.get_or_none(User.username == user)
|
stored_user: User | None = User.get_or_none(User.username == user)
|
||||||
|
|
||||||
# the user was deleted after the endpoint expired
|
# the user was deleted after the endpoint expired
|
||||||
if stored_user is None:
|
if stored_user is None:
|
||||||
continue
|
|
||||||
|
|
||||||
for token in stored_user.notification_tokens:
|
|
||||||
if isinstance(token, dict) and token.get("endpoint") in expired:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
user_subs.append(token)
|
for token in stored_user.notification_tokens:
|
||||||
|
if isinstance(token, dict) and token.get("endpoint") in expired:
|
||||||
|
continue
|
||||||
|
|
||||||
# overwrite the database and reset web pushers
|
user_subs.append(token)
|
||||||
User.update(notification_tokens=user_subs).where(
|
|
||||||
User.username == user
|
|
||||||
).execute()
|
|
||||||
|
|
||||||
self.web_pushers[user] = _build_web_pushers(user, user_subs)
|
# overwrite the database and reset web pushers
|
||||||
|
User.update(notification_tokens=user_subs).where(
|
||||||
|
User.username == user
|
||||||
|
).execute()
|
||||||
|
|
||||||
logger.info(
|
self.web_pushers[user] = _build_web_pushers(user, user_subs)
|
||||||
f"Cleaned up {len(expired)} notification subscriptions for {user}"
|
|
||||||
)
|
logger.info(
|
||||||
|
f"Cleaned up {len(expired)} notification subscriptions for {user}"
|
||||||
|
)
|
||||||
|
|
||||||
def suspend_notifications(self, camera: str, minutes: int) -> None:
|
def suspend_notifications(self, camera: str, minutes: int) -> None:
|
||||||
"""Suspend notifications for a specific camera."""
|
"""Suspend notifications for a specific camera."""
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ DEAD = {"endpoint": "https://push.example.com/dead", "keys": {}}
|
|||||||
class TestCleanupRegistrations(unittest.TestCase):
|
class TestCleanupRegistrations(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.db = SqliteDatabase(":memory:")
|
self.db = SqliteDatabase(":memory:")
|
||||||
self.db.bind([User])
|
self.enterContext(self.db.bind_ctx([User]))
|
||||||
self.db.create_tables([User])
|
self.db.create_tables([User])
|
||||||
|
|
||||||
User.create(
|
User.create(
|
||||||
@@ -27,6 +27,7 @@ class TestCleanupRegistrations(unittest.TestCase):
|
|||||||
self.client.web_pushers = {}
|
self.client.web_pushers = {}
|
||||||
self.client.expired_subs = {}
|
self.client.expired_subs = {}
|
||||||
self.client.expired_subs_lock = threading.Lock()
|
self.client.expired_subs_lock = threading.Lock()
|
||||||
|
self.client.cleanup_lock = threading.Lock()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.db.close()
|
self.db.close()
|
||||||
@@ -43,3 +44,15 @@ class TestCleanupRegistrations(unittest.TestCase):
|
|||||||
self.assertEqual(User.get_by_id("alice").notification_tokens, [LIVE])
|
self.assertEqual(User.get_by_id("alice").notification_tokens, [LIVE])
|
||||||
self.assertEqual(self.client.expired_subs, {})
|
self.assertEqual(self.client.expired_subs, {})
|
||||||
_build.assert_called_once_with("alice", [LIVE])
|
_build.assert_called_once_with("alice", [LIVE])
|
||||||
|
|
||||||
|
@patch("frigate.comms.webpush._build_web_pushers", return_value=[])
|
||||||
|
def test_keeps_malformed_tokens_and_prunes_expired(self, _build):
|
||||||
|
malformed = [None, {"keys": {}}]
|
||||||
|
User.update(notification_tokens=[LIVE, *malformed, DEAD]).execute()
|
||||||
|
self.client.expired_subs = {"alice": [DEAD["endpoint"]]}
|
||||||
|
|
||||||
|
self.client.cleanup_registrations()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
User.get_by_id("alice").notification_tokens, [LIVE, *malformed]
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user