Improve CID pinning process with random selection and faster isPinned check

This commit is contained in:
besoeasy
2025-12-31 06:35:20 +05:30
parent de1546b2f4
commit 25d1e4f765
2 changed files with 48 additions and 16 deletions
+47 -15
View File
@@ -507,27 +507,59 @@ const pinnerJob = async () => {
// Process self queue: pin CID
if (selfCidQueue.length > 0) {
const randomIndex = Math.floor(Math.random() * selfCidQueue.length);
const cid = selfCidQueue[randomIndex];
let cidToPinIndex = -1;
let cidToPin = null;
const checkedIndices = new Set();
console.log(`\n[Self] Processing CID (${selfCidQueue.length} in queue): ${cid}`);
// Keep trying random CIDs until we find one that's not pinned
while (checkedIndices.size < selfCidQueue.length) {
const randomIndex = Math.floor(Math.random() * selfCidQueue.length);
if (checkedIndices.has(randomIndex)) {
continue; // Already checked this one
}
checkedIndices.add(randomIndex);
const cid = selfCidQueue[randomIndex];
const alreadyPinned = await isPinned(cid);
if (alreadyPinned) {
console.log(`✓ Already pinned: ${cid}`);
selfCidQueue.splice(randomIndex, 1);
totalPinnedSelf++;
console.log(`📊 Counter updated: totalPinnedSelf = ${totalPinnedSelf}`);
console.log(`📋 Queue updated: ${selfCidQueue.length} CIDs remaining`);
didWork = true;
} else {
await pinCid(cid);
console.log(`✓ Successfully pinned: ${cid}`);
selfCidQueue.splice(randomIndex, 1);
console.log(`\n[Self] Checking CID (${selfCidQueue.length} in queue): ${cid}`);
const alreadyPinned = await isPinned(cid);
if (alreadyPinned) {
console.log(`⏭️ Already pinned, removing from queue: ${cid}`);
selfCidQueue.splice(randomIndex, 1);
totalPinnedSelf++;
didWork = true;
// Adjust checked indices after splice
const newCheckedIndices = new Set();
checkedIndices.forEach(idx => {
if (idx < randomIndex) {
newCheckedIndices.add(idx);
} else if (idx > randomIndex) {
newCheckedIndices.add(idx - 1);
}
});
checkedIndices.clear();
newCheckedIndices.forEach(idx => checkedIndices.add(idx));
} else {
// Found an unpinned CID
cidToPinIndex = randomIndex;
cidToPin = cid;
break;
}
}
if (cidToPin) {
console.log(`\n[Self] Pinning CID: ${cidToPin}`);
await pinCid(cidToPin);
console.log(`✓ Successfully pinned: ${cidToPin}`);
selfCidQueue.splice(cidToPinIndex, 1);
totalPinnedSelf++;
console.log(`📊 Counter updated: totalPinnedSelf = ${totalPinnedSelf}`);
console.log(`📋 Queue updated: ${selfCidQueue.length} CIDs remaining`);
didWork = true;
} else if (checkedIndices.size > 0) {
console.log(`✓ All checked CIDs were already pinned and removed`);
}
} else {
console.log(`[Self] Queue empty, nothing to process`);
+1 -1
View File
@@ -238,7 +238,7 @@ const isPinned = async (cid, ipfsApi = IPFS_API) => {
const startTime = Date.now();
try {
const endpoint = `${ipfsApi}/api/v0/pin/ls?arg=${encodeURIComponent(cid)}&type=recursive`;
const res = await axios.post(endpoint, null, { timeout: 5000 });
const res = await axios.post(endpoint, null, { timeout: 1000 });
const isPinned = res.data?.Keys && Object.keys(res.data.Keys).length > 0;
const duration = Date.now() - startTime;
console.log(`[isPinned] ${cid}${isPinned ? 'PINNED' : 'NOT PINNED'} (${duration}ms)`);