// How far server F's clock is from this machine's: the moment F's Date header // turns to the next second, against our clock at that moment (the middle of // the two requests around it). A ticket carries whole seconds, and F refuses // one from its future, so F must not be behind the dashboard's machine. // // node tools/live/clock.mjs [host] [port] import http from 'node:http'; const [host = '172.22.0.38', port = '8081'] = process.argv.slice(2); const agent = new http.Agent({ keepAlive: true, maxSockets: 1 }); const ask = () => new Promise((resolve, reject) => { const sent = Date.now(); http .get({ host, port, path: '/healthz', agent }, (r) => { r.resume(); r.on('end', () => resolve({ sent, got: Date.now(), date: Date.parse(r.headers.date) })); }) .on('error', reject); }); let last = await ask(); const offsets = []; for (const end = Date.now() + 6000; Date.now() < end;) { const now = await ask(); if (now.date !== last.date) offsets.push(Math.round(now.date - (last.got + now.sent) / 2) + `±${Math.ceil((now.sent - last.got) / 2)}`); last = now; } agent.destroy(); console.log(`${host}:${port} clock minus ours (ms): ${offsets.join(' ')}`);