diff --git a/src/tinyweb/handlers/rns.py b/src/tinyweb/handlers/rns.py
index 8941f99..53a5616 100644
--- a/src/tinyweb/handlers/rns.py
+++ b/src/tinyweb/handlers/rns.py
@@ -1,10 +1,54 @@
import json
+import time
+import threading
import traceback
from tinyweb.db import get_db, return_db
from tinyweb.rns_client import fetch_remote_page
from tinyweb.templates import esc
+class _PageCache:
+ def __init__(self, maxsize=50, ttl=300):
+ self._maxsize = maxsize
+ self._ttl = ttl
+ self._cache = {}
+ self._lock = threading.Lock()
+
+ def get(self, key):
+ with self._lock:
+ entry = self._cache.get(key)
+ if entry is None:
+ return None
+ if time.time() - entry["time"] > self._ttl:
+ del self._cache[key]
+ return None
+ self._cache.pop(key)
+ self._cache[key] = entry
+ return entry["value"]
+
+ def put(self, key, value):
+ with self._lock:
+ if key in self._cache:
+ self._cache.pop(key)
+ elif len(self._cache) >= self._maxsize:
+ oldest = next(iter(self._cache))
+ del self._cache[oldest]
+ self._cache[key] = {"value": value, "time": time.time()}
+
+
+_page_cache = _PageCache()
+
+
+def _inject_base_tag(html, dest_hash):
+ base = f'
{esc(json.dumps(data, indent=2))}"
except (json.JSONDecodeError, TypeError):
body = f"{esc(body[:2000])}"
+ else:
+ body = _inject_base_tag(body, dest_hash)
- body = _rewrite_links(body, dest_hash)
+ _page_cache.put(cache_key, body)
return {
"status": 200,
diff --git a/src/tinyweb/handlers/subscriptions.py b/src/tinyweb/handlers/subscriptions.py
index 0113d07..a7ba389 100644
--- a/src/tinyweb/handlers/subscriptions.py
+++ b/src/tinyweb/handlers/subscriptions.py
@@ -1,4 +1,5 @@
import threading
+import time
from datetime import datetime
from tinyweb.db import get_db, return_db, get_setting, set_setting, get_site_name, index_url, clean_url
@@ -10,6 +11,8 @@ from ._helpers import (
)
_sync_threads = {}
+_sync_starts = {}
+_SYNC_TIMEOUT = 120
MAX_API_SITES = 5000
MAX_BROWSE = 5000
@@ -142,6 +145,13 @@ def handle_subscriptions(msg=""):
subs = db.execute("SELECT * FROM subscriptions ORDER BY id DESC").fetchall()
finally:
return_db(db)
+ now_t = time.time()
+ for sub_id, start_t in list(_sync_starts.items()):
+ if now_t - start_t > _SYNC_TIMEOUT:
+ set_setting(f"sync_status_{sub_id}", "error:Timed out")
+ _sync_threads.pop(sub_id, None)
+ _sync_starts.pop(sub_id, None)
+
cards = ""
for s in subs:
sub_id = s["id"]
@@ -152,6 +162,9 @@ def handle_subscriptions(msg=""):
if is_syncing:
status_html = '