detached 2d8d7a1

fix: add backoff and skip init for non-API routes

commit
2d8d7a10c22c00252c960e7fc3f17dac8ae67f00
author
bnema · · 2026-01-18 21:23 UTC
parent
fe76e67
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
1 file · +25 -5
M src/hooks.server.ts +25 -5
@@ -35,19 +35,34 @@ async function setAllUsersOffline() {
3535
3636 // Initialize server (deferred, non-blocking for Cloudflare Workers compatibility)
3737 let initPromise: Promise<void> | null = null;
38+let lastFailedAttempt = 0;
39+let failureCount = 0;
40+const BASE_COOLDOWN = 5000; // 5 seconds
41+const MAX_COOLDOWN = 60000; // 1 minute max
3842
3943 function initializeServer(): Promise<void> {
4044 if (initPromise) return initPromise;
4145
46+ // Exponential backoff after failures
47+ if (lastFailedAttempt > 0) {
48+ const cooldown = Math.min(BASE_COOLDOWN * Math.pow(2, failureCount - 1), MAX_COOLDOWN);
49+ if (Date.now() - lastFailedAttempt < cooldown) {
50+ return Promise.resolve();
51+ }
52+ }
53+
4254 initPromise = (async () => {
4355 try {
4456 log.info('Initializing server...');
4557 await ensureDefaultChatRoom();
4658 await setAllUsersOffline();
4759 log.info('Server initialized successfully');
60+ lastFailedAttempt = 0;
61+ failureCount = 0;
4862 } catch (error: unknown) {
49- log.error('Failed to initialize server:', { error });
50- // Reset so next request can retry
63+ log.error('Failed to initialize server:', { error, failureCount: failureCount + 1 });
64+ lastFailedAttempt = Date.now();
65+ failureCount++;
5166 initPromise = null;
5267 }
5368 })();
@@ -56,10 +71,15 @@ function initializeServer(): Promise<void> {
5671 }
5772
5873 export const handle: Handle = async ({ event, resolve }) => {
59- // Lazy initialization on first request (Cloudflare Workers compatible)
60- await initializeServer();
74+ const path = event.url.pathname;
75+
76+ // Skip initialization for static assets and non-API routes
77+ const needsInit = path.startsWith('/api/') && !path.startsWith('/api/health');
78+ if (needsInit) {
79+ await initializeServer();
80+ }
6181
62- log.debug('Handling request', { path: event.url.pathname });
82+ log.debug('Handling request', { path });
6383
6484 // CSRF protection: validate Origin header for state-changing requests
6585 if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {
@@ -35,19 +35,34 @@ async function setAllUsersOffline() {
35 35
36 // Initialize server (deferred, non-blocking for Cloudflare Workers compatibility)36 // Initialize server (deferred, non-blocking for Cloudflare Workers compatibility)
37 let initPromise: Promise<void> | null = null;37 let initPromise: Promise<void> | null = null;
38+let lastFailedAttempt = 0;
39+let failureCount = 0;
40+const BASE_COOLDOWN = 5000; // 5 seconds
41+const MAX_COOLDOWN = 60000; // 1 minute max
38 42
39 function initializeServer(): Promise<void> {43 function initializeServer(): Promise<void> {
40 if (initPromise) return initPromise;44 if (initPromise) return initPromise;
41 45
46+ // Exponential backoff after failures
47+ if (lastFailedAttempt > 0) {
48+ const cooldown = Math.min(BASE_COOLDOWN * Math.pow(2, failureCount - 1), MAX_COOLDOWN);
49+ if (Date.now() - lastFailedAttempt < cooldown) {
50+ return Promise.resolve();
51+ }
52+ }
53+
42 initPromise = (async () => {54 initPromise = (async () => {
43 try {55 try {
44 log.info('Initializing server...');56 log.info('Initializing server...');
45 await ensureDefaultChatRoom();57 await ensureDefaultChatRoom();
46 await setAllUsersOffline();58 await setAllUsersOffline();
47 log.info('Server initialized successfully');59 log.info('Server initialized successfully');
60+ lastFailedAttempt = 0;
61+ failureCount = 0;
48 } catch (error: unknown) {62 } catch (error: unknown) {
49- log.error('Failed to initialize server:', { error });63+ log.error('Failed to initialize server:', { error, failureCount: failureCount + 1 });
50- // Reset so next request can retry64+ lastFailedAttempt = Date.now();
65+ failureCount++;
51 initPromise = null;66 initPromise = null;
52 }67 }
53 })();68 })();
@@ -56,10 +71,15 @@ function initializeServer(): Promise<void> {
56 }71 }
57 72
58 export const handle: Handle = async ({ event, resolve }) => {73 export const handle: Handle = async ({ event, resolve }) => {
59- // Lazy initialization on first request (Cloudflare Workers compatible)74+ const path = event.url.pathname;
60- await initializeServer();75+
76+ // Skip initialization for static assets and non-API routes
77+ const needsInit = path.startsWith('/api/') && !path.startsWith('/api/health');
78+ if (needsInit) {
79+ await initializeServer();
80+ }
61 81
62- log.debug('Handling request', { path: event.url.pathname });82+ log.debug('Handling request', { path });
63 83
64 // CSRF protection: validate Origin header for state-changing requests84 // CSRF protection: validate Origin header for state-changing requests
65 if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {85 if (event.request.method !== 'GET' && event.request.method !== 'HEAD') {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 3

Tags 8

↑↓ moveenter openesc close