Template
✨ feat(ssr): einen Renderer pro URL über context.lock
deploy to production / deploy (push) Successful in 43s
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:
1 parent
cf44cd75f4
commit
c358392386
2 files changed
+83
-16
No files matched your search
+82
-15
@@ -5,6 +5,11 @@ const { release } = require("../config-client")
|
||||
const { ssrValidatePath } = require("../config")
|
||||
const { ssrRequest } = require("../lib/ssr-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 () {
|
||||
var request = context.request()
|
||||
|
||||
@@ -113,12 +118,48 @@ const APP_SERVER_MODULE_PATH = "../lib/app.server"
|
||||
comment += ", path: " + url
|
||||
|
||||
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) {
|
||||
html = "<!-- NO SSR RENDERING -->"
|
||||
} else if (pNotfound) {
|
||||
status = 404
|
||||
html = "404 NOT FOUND"
|
||||
} 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
|
||||
context.ssrCache = {}
|
||||
// @ts-ignore – tracks dependencies as { "col:id": true, "col:*": true }
|
||||
@@ -211,24 +252,50 @@ const APP_SERVER_MODULE_PATH = "../lib/app.server"
|
||||
// Besucher dann für immer leer, weil hydrate() nichts zum
|
||||
// Einhängen findet.
|
||||
//
|
||||
// Was hier nicht passiert: dass zweimal gerendert wird. Dafür bräuchte
|
||||
// es Koordination über Anfragen hinweg, und ein Hook hat keinen
|
||||
// geteilten Zustand. Das bleibt erstmal so — ein Ansturm kostet
|
||||
// Rechenzeit, keine Seite.
|
||||
try {
|
||||
context.db.create("ssr", {
|
||||
path: url,
|
||||
content: tpl,
|
||||
// @ts-ignore
|
||||
validUntil: context.ssrCacheValidUntil,
|
||||
// dependency strings: "col:id" for detail, "col:*" for list (deduplicated)
|
||||
dependencies: depsKeys,
|
||||
})
|
||||
} catch (e) {
|
||||
utils.log("ssr cache: Eintrag für " + url + " wurde parallel geschrieben, Anfrage ohne eigenen Cache-Eintrag")
|
||||
// Seit context.lock rendert pro URL nur noch ein Arbeiter. Wer den
|
||||
// Lock nicht bekommt (Timeout), rendert zwar als Rückfallebene,
|
||||
// schreibt aber nicht: ein zweiter Schreiber würde nur wieder am
|
||||
// Unique-Index scheitern.
|
||||
if (skipCacheWrite) {
|
||||
comment += ", cacheWriteSkipped"
|
||||
} else {
|
||||
try {
|
||||
context.db.create("ssr", {
|
||||
path: url,
|
||||
content: tpl,
|
||||
// @ts-ignore
|
||||
validUntil: context.ssrCacheValidUntil,
|
||||
// dependency strings: "col:id" for detail, "col:*" for list (deduplicated)
|
||||
dependencies: depsKeys,
|
||||
})
|
||||
} catch (e) {
|
||||
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 {
|
||||
status: status,
|
||||
log: false,
|
||||
|
||||
+1
-1
Submodule tibi-types updated: 21472bdc72...4e13d4ab9a.
Reference in new issue
Block a user