⚡ Use json serialization for announcements cache (#4339)
* Use json for annoucements cache Parsing this XML file is very slow, so there's no reason to do it more than we have to. See 4480-2021-12-11T13:23:48-05:00.json, MainThread -> parse On my system, this reduces the time for /plugin/announcements/channels from 250ms CPU time to 50ms. The rough improvment in this specific function call is from 8% to 0.32% of total runtime. * ♻️ Refactor json serializer * Move back to octoprint.util.json * Leave old encoding helper as is but add additional one focused on serialization (with full roundtrips). Use each as needed (primarily encoding one). octoprint.util.encoding -> old one octoprint.util.serializing -> new one Each serves different purpose, we don't want to end up with `__jsonclass__` stuff in our API output at some point, so separate the concerns here and allow different handling. * Ensure wrappers are in place for backwards compatibility (e.g. dump vs dumps). * 🐛 Fix handling of invalid cache We get different exceptions now and in any case just throw *anything* away that we can't parse. * 🎨 Use fallback typing annotation for Py3.7.{0,1} Better solution than getting stuck to Dict only, thanks @flaviut for the suggestion! * 🐛 Fix bytes encoding bytes objects are explicitly not always UTF-8, so encode them in a way that doesn't lead to errors and encoding/decoding problems when they are actually binary data. Co-authored-by: Gina Häußge <gina@octoprint.org>
This commit is contained in:
1 parent
a7f7e63489
commit
1758efb82d
9 files changed
+405
-61
No files matched your search
@@ -23,7 +23,7 @@ from octoprint.server.util.flask import (
|
||||
no_firstrun_access,
|
||||
with_revalidation_checking,
|
||||
)
|
||||
from octoprint.util import count, utmify
|
||||
from octoprint.util import count, deserialize, serialize, utmify
|
||||
from octoprint.util.text import sanitize
|
||||
|
||||
|
||||
@@ -428,12 +428,22 @@ class AnnouncementPlugin(
|
||||
|
||||
ttl *= 60
|
||||
now = time.time()
|
||||
if os.stat(channel_path).st_mtime + ttl > now:
|
||||
d = feedparser.parse(channel_path)
|
||||
if now > os.stat(channel_path).st_mtime + ttl:
|
||||
return None
|
||||
|
||||
try:
|
||||
d = deserialize(channel_path)
|
||||
self._logger.debug(f"Loaded channel {key} from cache at {channel_path}")
|
||||
return d
|
||||
|
||||
return None
|
||||
except Exception:
|
||||
self._logger.exception(
|
||||
f"Could not read channel {key} from cache {channel_path}"
|
||||
)
|
||||
try:
|
||||
os.remove(channel_path)
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
def _get_channel_data_from_network(self, key, config):
|
||||
"""Fetch channel feed from network."""
|
||||
@@ -456,11 +466,12 @@ class AnnouncementPlugin(
|
||||
)
|
||||
return None
|
||||
|
||||
response = r.text
|
||||
channel_path = self._get_channel_cache_path(key)
|
||||
with open(channel_path, mode="wt", encoding="utf-8") as f:
|
||||
f.write(response)
|
||||
return feedparser.parse(response)
|
||||
response = feedparser.parse(r.text)
|
||||
try:
|
||||
serialize(self._get_channel_cache_path(key), response)
|
||||
except Exception:
|
||||
self._logger.exception(f"Failed to cache data for channel {key}")
|
||||
return response
|
||||
|
||||
def _to_internal_feed(self, feed, read_until=None):
|
||||
"""Convert feed to internal data structure."""
|
||||
|
||||
Reference in new issue
Block a user