Skip to content

Commit

Permalink
fix: removing uneccessary retry and timeout from db.query
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 23, 2025
1 parent 94671be commit a81aec6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 74 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.59",
"version": "0.12.60",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
104 changes: 41 additions & 63 deletions relay/src/pinner/pinningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,85 +223,63 @@ export class PinningService {
});
}

/**
* Check if content should remain pinned
* @param {string} cid - Content identifier
* @returns {Promise<boolean>} - Should remain pinned
*/
async shouldRemainPinned(cid) {
if (!this.db) await this.initializeDatabase();

const MAX_RETRIES = 3;
const RETRY_DELAY = 1000;

// Validation and setup outside the queue
// Validation and setup
if (!this.db || !this.db.query) {
logger.error('Database connection is not properly initialized');
throw new Error('Database connection error');
}

if (!cid || typeof cid !== 'string') {
logger.error('Invalid CID format');
throw new Error('Invalid CID format');
}

for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
logger.info(`Attempt ${attempt}/${MAX_RETRIES} to check pin status for ${cid}`);

// Only queue the database operation

try {
logger.info(`Checking pin status for ${cid}`);

// Queue the database operation
const allDocs = await this.queue.add(async () => {
const queryPromise = this.db.query((doc) =>
return this.db.query((doc) =>
doc.cid === cid &&
(doc.isPinned === undefined || doc.isPinned === true) // Only consider pinned or undefined status
);
const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error('Database query timeout')), 5000)
(doc.isPinned === undefined || doc.isPinned === true)
);
return await Promise.race([queryPromise, timeoutPromise]);
});

if (!allDocs || allDocs.length === 0) {
logger.info(`No metadata found for CID ${cid}`);
if (attempt === MAX_RETRIES) {
return false;
}
throw new Error('Metadata not found, will retry');
}

const metadata = allDocs[0];

if (!metadata || !metadata.expirationDate) {
logger.error('Invalid metadata format');
throw new Error('Invalid metadata format');
}

const now = Date.now();
const shouldKeepPinned = (
now < metadata.expirationDate ||
!metadata.requirePayment ||
metadata.paymentSufficient
);

logger.info(`Pin status check for ${cid}:`, {
currentTime: new Date(now).toISOString(),
expirationDate: new Date(metadata.expirationDate).toISOString(),
requirePayment: metadata.requirePayment,
paymentSufficient: metadata.paymentSufficient,
decision: shouldKeepPinned ? 'keep pinned' : 'can unpin'
});

return shouldKeepPinned;
} catch (error) {
logger.error(`Attempt ${attempt}/${MAX_RETRIES} failed for ${cid}:`, error);

if (attempt === MAX_RETRIES) {
logger.error(`All attempts failed for ${cid}, defaulting to keep pinned for safety`);
return true;
}

await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));

if (!allDocs || allDocs.length === 0) {
logger.info(`No active pin metadata found for CID ${cid}`);
return true; // Keep pinned if no metadata found (safety measure)
}

const metadata = allDocs[0];

if (!metadata || !metadata.expirationDate) {
logger.error('Invalid metadata format');
throw new Error('Invalid metadata format');
}

const now = Date.now();
const shouldKeepPinned = (
now < metadata.expirationDate ||
!metadata.requirePayment ||
metadata.paymentSufficient
);

logger.info(`Pin status check for ${cid}:`, {
currentTime: new Date(now).toISOString(),
expirationDate: new Date(metadata.expirationDate).toISOString(),
requirePayment: metadata.requirePayment,
paymentSufficient: metadata.paymentSufficient,
decision: shouldKeepPinned ? 'keep pinned' : 'can unpin'
});

return shouldKeepPinned;
} catch (error) {
logger.error(`Error checking pin status for ${cid}:`, error);
// Default to keeping pinned if there's any error
return true;
}
}

Expand Down
30 changes: 20 additions & 10 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,28 @@ tipWatcher.on('newTip', async (tip) => {
}

for (const cid of pinnedCids) {
const shouldRemain = await pinningService.shouldRemainPinned(cid);
if (!shouldRemain) {
logger.info(`Unpinning expired content: ${cid}`);
await helia.pins.rm(CID.parse(cid));
try {
const shouldRemain = await pinningService.shouldRemainPinned(cid);

// Update metadata to mark as unpinned
try {
await pinningService.markAsUnpinned(cid);
logger.info(`Successfully updated metadata for unpinned content: ${cid}`);
} catch (error) {
logger.error(`Failed to update metadata for unpinned content ${cid}:`, error);
if (!shouldRemain) {
logger.info(`Unpinning expired content: ${cid}`);
await helia.pins.rm(CID.parse(cid));

// Update metadata to mark as unpinned
try {
await pinningService.markAsUnpinned(cid);
logger.info(`Successfully updated metadata for unpinned content: ${cid}`);
} catch (error) {
logger.error(`Failed to update metadata for unpinned content ${cid}:`, error);
}
}
} catch (error) {
if (error.message.includes('No active pin metadata found')) {
// If metadata is not found, keep the content pinned as a safety measure
logger.warn(`No metadata found for CID ${cid}, keeping pinned as safety measure`);
continue;
}
logger.error(`Error processing CID ${cid}:`, error);
}
}
} catch (error) {
Expand Down

0 comments on commit a81aec6

Please sign in to comment.