Restore an evicted server copy from an online holder

This commit is contained in:
Claude
2026-09-30 07:58:42 +00:00
parent 6d3cefa874
commit 60a28fc593
8 changed files with 112 additions and 7 deletions

View File

@@ -118,3 +118,28 @@ export async function holdersPayload({ videoId, cid, p2pDb, isOnline, staleDays,
}
return { ok: true, staleDays, cids: out };
}
// Flow 8 (plan 018): the source is gone and the server evicted its copy, so
// ask ONE online holder that shares to upload it through intake
// ({type:'upload-request', videoId, cid}). At most one request per video per
// 10 minutes; returns true when a device was asked (or recently was).
export function createRehydrator({ p2pDb, hub, hasServerCopy, enabled = () => true, now = () => Date.now() }) {
const asked = new Map(); // videoId -> ms
return async function rehydrate(videoId) {
if (!enabled()) return false;
if (await hasServerCopy(videoId)) return false;
const last = asked.get(videoId);
if (last && now() - last < 10 * 60_000) return true;
for (const c of await p2pDb.listContentForVideo(videoId)) {
for (const h of await p2pDb.listHolders(c.cid, 50)) {
if (Number(h.share) !== 1 || !hub.isOnline(h.device_id)) continue;
if (hub.send(h.device_id, { type: 'upload-request', videoId, cid: c.cid })) {
asked.set(videoId, now());
if (asked.size > 5000) asked.delete(asked.keys().next().value);
return true;
}
}
}
return false;
};
}