✨ feat(ssr): einen Renderer pro URL über context.lock
deploy to production / deploy (push) Successful in 43s

Der SSR-Hook koordiniert Renders jetzt über den Server-Lock statt sich auf
den Unique-Index zu verlassen: acquire("ssr:render:"+url, {waitMs:15000}),
danach Cache-Doppelprüfung, Schreiben nur durch den Lock-Inhaber und
Freigabe erst nach dem Commit des Cache-Eintrags.

Wer den Lock nicht bekommt, rendert als Rückfallebene ohne Cache-Schreiben;
ein älterer Server ohne context.lock fällt ohne Fehler auf das bisherige
Verhalten zurück.

tibi-types: LockPackage (4e13d4a).
This commit is contained in:
apairon committed 2026-11-09 16:44:52 +00:00
1 parent cf44cd75f4
commit c358392386
2 files changed
+72 -5

No files matched your search

+71 -4
View File
@@ -5,6 +5,11 @@ const { release } = require("../config-client")
const { ssrValidatePath } = require("../config") const { ssrValidatePath } = require("../config")
const { ssrRequest } = require("../lib/ssr-server") const { ssrRequest } = require("../lib/ssr-server")
const APP_SERVER_MODULE_PATH = "../lib/app.server" const APP_SERVER_MODULE_PATH = "../lib/app.server"
// Upper bound for waiting on another renderer of the same URL. Long enough to
// cover a normal render, short enough that a visitor does not sit in the
// request for the whole hook timeout.
const SSR_LOCK_WAIT_MS = 15000
;(function () { ;(function () {
var request = context.request() var request = context.request()
@@ -113,12 +118,48 @@ const APP_SERVER_MODULE_PATH = "../lib/app.server"
comment += ", path: " + url comment += ", path: " + url
var cacheIt = false var cacheIt = false
// Cross-request coordination for this URL: one renderer at a time.
// lockKey stays empty when no lock is taken (noCache/old server).
var lockKey = ""
var lockHeld = false
var skipCacheWrite = false
if (pNorender) { if (pNorender) {
html = "<!-- NO SSR RENDERING -->" html = "<!-- NO SSR RENDERING -->"
} else if (pNotfound) { } else if (pNotfound) {
status = 404 status = 404
html = "404 NOT FOUND" html = "404 NOT FOUND"
} else { } else {
if (!noCache) {
// Coordinate renders across requests. Without this, a cache
// invalidation makes every concurrent request render and write
// the same page; the DB unique index then turns the losers into
// duplicate-key errors and wasted CPU.
lockKey = "ssr:render:" + url
try {
const lock = context.lock.acquire(lockKey, { waitMs: SSR_LOCK_WAIT_MS })
if (lock.acquired) {
lockHeld = true
// The previous owner may have populated the cache while
// we waited — never render a page that already exists.
useCache(url)
comment += ", lock"
} else {
// Timed out: somebody is still working on this URL.
skipCacheWrite = true
useCache(url)
comment += ", lockTimeout"
}
} catch (/** @type {any} */ e) {
// useCache throws its hook response: never swallow that.
if (lockHeld || skipCacheWrite) throw e
// context.lock is not available (older server) or acquire
// failed. Render as before, without coordination.
utils.log("ssr lock: " + (e && e.message ? e.message : e))
lockKey = ""
comment += ", lockUnavailable"
}
}
// @ts-ignore // @ts-ignore
context.ssrCache = {} context.ssrCache = {}
// @ts-ignore – tracks dependencies as { "col:id": true, "col:*": true } // @ts-ignore – tracks dependencies as { "col:id": true, "col:*": true }
@@ -211,10 +252,13 @@ const APP_SERVER_MODULE_PATH = "../lib/app.server"
// Besucher dann für immer leer, weil hydrate() nichts zum // Besucher dann für immer leer, weil hydrate() nichts zum
// Einhängen findet. // Einhängen findet.
// //
// Was hier nicht passiert: dass zweimal gerendert wird. Dafür bräuchte // Seit context.lock rendert pro URL nur noch ein Arbeiter. Wer den
// es Koordination über Anfragen hinweg, und ein Hook hat keinen // Lock nicht bekommt (Timeout), rendert zwar als Rückfallebene,
// geteilten Zustand. Das bleibt erstmal so — ein Ansturm kostet // schreibt aber nicht: ein zweiter Schreiber würde nur wieder am
// Rechenzeit, keine Seite. // Unique-Index scheitern.
if (skipCacheWrite) {
comment += ", cacheWriteSkipped"
} else {
try { try {
context.db.create("ssr", { context.db.create("ssr", {
path: url, path: url,
@@ -226,7 +270,30 @@ const APP_SERVER_MODULE_PATH = "../lib/app.server"
}) })
} catch (e) { } catch (e) {
utils.log("ssr cache: Eintrag für " + url + " wurde parallel geschrieben, Anfrage ohne eigenen Cache-Eintrag") utils.log("ssr cache: Eintrag für " + url + " wurde parallel geschrieben, Anfrage ohne eigenen Cache-Eintrag")
} finally {
// Hand over only after the write is committed, so a waiter
// that acquires next actually sees the entry.
if (lockHeld) {
try {
context.lock.release(lockKey)
} catch (/** @type {any} */ e) {
utils.log("ssr lock release: " + (e && e.message ? e.message : e))
} }
lockHeld = false
}
}
}
}
// The page was not cached (404, render error, noCache or lock timeout):
// free the claim before the response is thrown.
if (lockHeld) {
try {
context.lock.release(lockKey)
} catch (/** @type {any} */ e) {
utils.log("ssr lock release: " + (e && e.message ? e.message : e))
}
lockHeld = false
} }
throw { throw {