-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
360 lines (320 loc) · 13.4 KB
/
Copy pathserver.js
File metadata and controls
360 lines (320 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use strict';
/*
* flakey-script — a deliberately unreliable webhook receiver.
*
* It is the destination half of a Webhook Relay "durable retries" demo:
*
* sender ──▶ Webhook Relay (durable retries) ──▶ tunnel ──▶ flakey-script
*
* Behaviour, per incoming webhook `id`:
* - The first time an id is seen we record `firstSeenAt`.
* - While an id is younger than RECOVERY_AFTER_MS it fails FAILURE_RATE of
* the time (HTTP 500) and succeeds the rest (HTTP 200). Default 80% fail.
* - Once an id is at least RECOVERY_AFTER_MS old (default 1 hour) it ALWAYS
* succeeds — this is the "endpoint has recovered" moment that lets Webhook
* Relay's retries finally land.
* - Once an id has succeeded once it stays delivered: any later retry for the
* same id is an idempotent 200 (we never double-process).
*
* State lives on the filesystem (DATA_DIR/state.json) so it survives restarts
* and you can watch every id converge on "delivered" over time, both here and
* in the Webhook Relay dashboard.
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = parseInt(process.env.PORT || '3000', 10);
const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, 'data');
const STATE_FILE = path.join(DATA_DIR, 'state.json');
const FAILURE_RATE = clampFloat(process.env.FAILURE_RATE, 0.8); // 80% of early attempts fail
const RECOVERY_AFTER_MS = parseInt(process.env.RECOVERY_AFTER_MS || String(60 * 60 * 1000), 10); // 1 hour
const SUCCESS_STATUS = parseInt(process.env.SUCCESS_STATUS || '200', 10);
const FAILURE_STATUS = parseInt(process.env.FAILURE_STATUS || '500', 10);
function clampFloat(v, dflt) {
const n = parseFloat(v);
if (Number.isNaN(n)) return dflt;
return Math.min(1, Math.max(0, n));
}
// ---------------------------------------------------------------------------
// State: a simple JSON document on disk, kept in memory and written atomically.
// ---------------------------------------------------------------------------
/** @type {Record<string, any>} */
let state = {};
function loadState() {
try {
state = JSON.parse(fs.readFileSync(STATE_FILE, 'utf8'));
if (state === null || typeof state !== 'object') state = {};
} catch (err) {
if (err.code !== 'ENOENT') console.error('failed to read state, starting fresh:', err.message);
state = {};
}
}
let writeQueued = false;
function persistState() {
// Debounce bursts of writes into a single atomic rename.
if (writeQueued) return;
writeQueued = true;
setImmediate(() => {
writeQueued = false;
const tmp = STATE_FILE + '.tmp';
try {
fs.writeFileSync(tmp, JSON.stringify(state, null, 2));
fs.renameSync(tmp, STATE_FILE);
} catch (err) {
console.error('failed to persist state:', err.message);
}
});
}
// ---------------------------------------------------------------------------
// Core decision: should this attempt for `id` succeed?
// ---------------------------------------------------------------------------
function handleDelivery(id, type, body, now) {
let rec = state[id];
if (!rec) {
rec = state[id] = {
id,
type: type || null,
firstSeenAt: now,
attempts: 0,
delivered: false,
deliveredAt: null,
deliveredOnAttempt: null,
lastAttemptAt: null,
lastStatus: null,
lastReason: null,
lastBody: null,
};
}
rec.attempts += 1;
rec.lastAttemptAt = now;
rec.type = type || rec.type;
rec.lastBody = truncate(body, 2000);
const ageMs = now - rec.firstSeenAt;
let status;
let reason;
if (rec.delivered) {
// Already done — be idempotent, never reprocess.
status = SUCCESS_STATUS;
reason = 'already-delivered';
} else if (ageMs >= RECOVERY_AFTER_MS) {
// The endpoint has "recovered": guaranteed success from here on.
status = SUCCESS_STATUS;
reason = 'recovered-after-grace';
rec.delivered = true;
rec.deliveredAt = now;
rec.deliveredOnAttempt = rec.attempts;
} else if (Math.random() >= FAILURE_RATE) {
// Lucky early success.
status = SUCCESS_STATUS;
reason = 'lucky-early-success';
rec.delivered = true;
rec.deliveredAt = now;
rec.deliveredOnAttempt = rec.attempts;
} else {
// Simulated flaky failure.
status = FAILURE_STATUS;
reason = 'simulated-failure';
}
rec.lastStatus = status;
rec.lastReason = reason;
persistState();
return { status, reason, rec, ageMs };
}
function truncate(s, n) {
if (typeof s !== 'string') return s;
return s.length > n ? s.slice(0, n) + '…' : s;
}
// ---------------------------------------------------------------------------
// HTTP handling
// ---------------------------------------------------------------------------
function readBody(req) {
return new Promise((resolve) => {
let data = '';
req.on('data', (chunk) => {
data += chunk;
if (data.length > 1_000_000) req.destroy(); // guard against giant bodies
});
req.on('end', () => resolve(data));
req.on('error', () => resolve(data));
});
}
function summary() {
const ids = Object.values(state);
const delivered = ids.filter((r) => r.delivered);
const totalAttempts = ids.reduce((a, r) => a + r.attempts, 0);
return {
totalIds: ids.length,
delivered: delivered.length,
pending: ids.length - delivered.length,
totalAttempts,
failedAttempts: totalAttempts - delivered.length, // each delivered id eventually had exactly one success
recoveryAfterMs: RECOVERY_AFTER_MS,
failureRate: FAILURE_RATE,
};
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
const route = url.pathname.replace(/\/+$/, '') || '/';
// Health check
if (route === '/healthz' || route === '/health') {
return json(res, 200, { ok: true });
}
// JSON state for tooling / the live dashboard
if (route === '/api/state' && req.method === 'GET') {
return json(res, 200, { summary: summary(), ids: Object.values(state) });
}
// Reset (handy between demo runs)
if (route === '/admin/reset' && (req.method === 'POST' || req.method === 'DELETE')) {
state = {};
persistState();
return json(res, 200, { ok: true, reset: true });
}
// Human-friendly live dashboard
if (route === '/' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
return res.end(renderDashboard());
}
// Everything else that is a POST/PUT is treated as an incoming webhook.
if (req.method === 'POST' || req.method === 'PUT') {
const raw = await readBody(req);
let payload = {};
try {
payload = raw ? JSON.parse(raw) : {};
} catch (_) {
payload = {};
}
const id = String(payload.id || payload.event_id || req.headers['x-webhook-id'] || 'no-id-' + Date.now());
const type = payload.type || payload.event || null;
const now = Date.now();
const { status, reason, rec, ageMs } = handleDelivery(id, type, raw, now);
const ok = status < 400;
const line =
`${new Date(now).toISOString()} id=${id} attempt=${rec.attempts} ` +
`age=${formatDuration(ageMs)} -> ${status} (${reason})`;
console.log((ok ? '✅ ' : '❌ ') + line);
return json(res, status, {
ok,
id,
attempt: rec.attempts,
status,
reason,
delivered: rec.delivered,
age_ms: ageMs,
recovers_in_ms: rec.delivered ? 0 : Math.max(0, RECOVERY_AFTER_MS - ageMs),
message: ok
? 'Accepted.'
: 'Simulated flaky failure — please retry. This id will succeed once it is ' +
formatDuration(RECOVERY_AFTER_MS) + ' old.',
});
}
return json(res, 404, { ok: false, error: 'not found' });
});
function json(res, status, obj) {
const body = JSON.stringify(obj, null, 2);
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
res.end(body);
}
function formatDuration(ms) {
if (ms < 1000) return ms + 'ms';
const s = Math.floor(ms / 1000);
if (s < 60) return s + 's';
const m = Math.floor(s / 60);
if (m < 60) return m + 'm' + (s % 60 ? ' ' + (s % 60) + 's' : '');
const h = Math.floor(m / 60);
return h + 'h' + (m % 60 ? ' ' + (m % 60) + 'm' : '');
}
// ---------------------------------------------------------------------------
// Dashboard (self-contained HTML, auto-refresh)
// ---------------------------------------------------------------------------
function renderDashboard() {
const s = summary();
const MAX_DASHBOARD_ROWS = 150; // keep the page light over thousands of webhooks
const allRecords = Object.values(state).sort((a, b) => b.firstSeenAt - a.firstSeenAt);
const rows = allRecords
.slice(0, MAX_DASHBOARD_ROWS)
.map((r) => {
const now = Date.now();
const age = formatDuration(now - r.firstSeenAt);
const badge = r.delivered
? '<span class="badge ok">delivered</span>'
: '<span class="badge pending">retrying</span>';
const recoversIn = r.delivered ? '—' : formatDuration(Math.max(0, RECOVERY_AFTER_MS - (now - r.firstSeenAt)));
return `<tr>
<td><code>${escapeHtml(r.id)}</code></td>
<td>${escapeHtml(r.type || '')}</td>
<td>${badge}</td>
<td class="num">${r.attempts}</td>
<td>${age}</td>
<td>${recoversIn}</td>
<td>${r.deliveredOnAttempt ? 'attempt #' + r.deliveredOnAttempt : ''}</td>
</tr>`;
})
.join('\n');
const pct = s.totalIds ? Math.round((s.delivered / s.totalIds) * 100) : 0;
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="refresh" content="5">
<title>flakey-script · durable retries demo</title>
<style>
:root { color-scheme: light dark; }
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; margin: 0; background: #0b1020; color: #e6e9f2; }
.wrap { max-width: 980px; margin: 0 auto; padding: 2rem 1.25rem 4rem; }
h1 { font-size: 1.4rem; margin: 0 0 .25rem; }
.sub { color: #97a0bd; margin: 0 0 1.5rem; font-size: .9rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .75rem; margin-bottom: 1.5rem; }
.card { background: #131a30; border: 1px solid #222c49; border-radius: 12px; padding: 1rem; }
.card .k { color: #97a0bd; font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.card .v { font-size: 1.6rem; font-weight: 700; margin-top: .25rem; }
.bar { height: 10px; background: #222c49; border-radius: 999px; overflow: hidden; margin: .35rem 0 1.25rem; }
.bar > span { display: block; height: 100%; background: linear-gradient(90deg,#6366f1,#22c55e); width: ${pct}%; transition: width .4s; }
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th, td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid #1d2540; }
th { color: #97a0bd; font-weight: 600; font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
td.num, th.num { text-align: right; }
code { background: #1d2540; padding: .1rem .35rem; border-radius: 6px; font-size: .82rem; }
.badge { font-size: .72rem; padding: .15rem .5rem; border-radius: 999px; font-weight: 600; }
.badge.ok { background: rgba(34,197,94,.16); color: #4ade80; }
.badge.pending { background: rgba(245,158,11,.16); color: #fbbf24; }
.empty { color: #97a0bd; padding: 2rem 0; }
a { color: #818cf8; }
</style>
</head>
<body>
<div class="wrap">
<h1>flakey-script <span style="font-weight:400;color:#97a0bd">· durable retries demo</span></h1>
<p class="sub">Fails ${Math.round(FAILURE_RATE * 100)}% of early attempts. Every id is guaranteed to succeed once it is ${formatDuration(RECOVERY_AFTER_MS)} old. Powered by <a href="https://webhookrelay.com/features/durable-retries/">Webhook Relay durable retries</a>.</p>
<div class="cards">
<div class="card"><div class="k">Webhooks</div><div class="v">${s.totalIds}</div></div>
<div class="card"><div class="k">Delivered</div><div class="v" style="color:#4ade80">${s.delivered}</div></div>
<div class="card"><div class="k">Retrying</div><div class="v" style="color:#fbbf24">${s.pending}</div></div>
<div class="card"><div class="k">Total attempts</div><div class="v">${s.totalAttempts}</div></div>
</div>
<div class="bar"><span></span></div>
${
s.totalIds
? `<table>
<thead><tr><th>id</th><th>type</th><th>status</th><th class="num">attempts</th><th>age</th><th>recovers in</th><th>delivered</th></tr></thead>
<tbody>${rows}</tbody>
</table>${allRecords.length > MAX_DASHBOARD_ROWS ? `<p class="sub" style="margin-top:1rem">Showing the latest ${MAX_DASHBOARD_ROWS} of ${allRecords.length} webhooks.</p>` : ''}`
: '<p class="empty">No webhooks yet. Send one to the Webhook Relay endpoint and watch it converge here.</p>'
}
</div>
</body>
</html>`;
}
function escapeHtml(s) {
return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
}
// ---------------------------------------------------------------------------
fs.mkdirSync(DATA_DIR, { recursive: true });
loadState();
server.listen(PORT, () => {
console.log(`flakey-script listening on :${PORT}`);
console.log(` failure rate: ${Math.round(FAILURE_RATE * 100)}% of early attempts`);
console.log(` recovers after: ${formatDuration(RECOVERY_AFTER_MS)} per id`);
console.log(` state file: ${STATE_FILE}`);
console.log(` dashboard: http://localhost:${PORT}/`);
});