UNPKG

@clickup/pg-microsharding

Version:
62 lines 2.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSlotInfo = getSlotInfo; const createConnectedClient_1 = require("./createConnectedClient"); const parseLsn_1 = require("./parseLsn"); /** * Returns the difference between src LSN and dst LSN (gap), along with some * estimation on how much seconds they are apart from each other. To measure * that time, we "latch" an LSN on the src and then wait until dst crosses that * latched LSN. Once it does, we remember the actual astronomical time it took * to cross, and then latch the src LSN again (towards the next estimation * cycle). When the caller decides that the time is small enough, it stops * polling; and if the time is large (e.g. more than 20 seconds), it continues * to call getSlotInfo() over and over again until that time drops below the * needed 20 seconds threshold. */ async function getSlotInfo({ dsn, slotName, }, prevSlotInfo) { const client = await (0, createConnectedClient_1.createConnectedClient)(dsn); try { const srcLsn = await client.currentWalInsertLsn(); const dstLsn = await client.confirmedFlushLsn(slotName); const gap = (0, parseLsn_1.subtractLsn)(srcLsn, dstLsn); if (!prevSlotInfo) { prevSlotInfo = { srcLsn, dstLsn, gap, gapTillLatch: gap, lagSec: null, // means "unknown" latched: { srcLsn, at: performance.now(), }, }; } const gapTillLatch = (0, parseLsn_1.subtractLsn)(prevSlotInfo.latched.srcLsn, dstLsn); return gapTillLatch <= 0 ? { srcLsn, dstLsn, gap, gapTillLatch, lagSec: Math.round((performance.now() - prevSlotInfo.latched.at) / 1000), latched: { srcLsn, at: performance.now(), }, } : { srcLsn, dstLsn, gap, gapTillLatch, lagSec: prevSlotInfo.lagSec, latched: prevSlotInfo.latched, }; } finally { await client.end().catch(() => { }); } } //# sourceMappingURL=getSlotInfo.js.map