mirror of
https://github.com/besoeasy/file-drop.git
synced 2026-01-27 13:48:50 -06:00
feat: implement request cooldown for pinCid to prevent duplicate requests
This commit is contained in:
@@ -4,6 +4,10 @@ const http = require("http");
|
||||
const https = require("https");
|
||||
const { IPFS_API } = require("./config");
|
||||
|
||||
// Track pin requests to avoid duplicates (CID -> timestamp)
|
||||
const pinRequestCache = new Map();
|
||||
const PIN_REQUEST_COOLDOWN = 3 * 60 * 60 * 1000; // 3 hours in milliseconds
|
||||
|
||||
// Create persistent HTTP agents with keep-alive for long-running connections
|
||||
const httpAgent = new http.Agent({
|
||||
keepAlive: true,
|
||||
@@ -42,12 +46,29 @@ const isPinned = async (cid) => {
|
||||
*/
|
||||
const pinCid = async (cid) => {
|
||||
try {
|
||||
// Check if we recently requested this CID
|
||||
const lastRequest = pinRequestCache.get(cid);
|
||||
const now = Date.now();
|
||||
|
||||
if (lastRequest && (now - lastRequest) < PIN_REQUEST_COOLDOWN) {
|
||||
const hoursAgo = ((now - lastRequest) / (1000 * 60 * 60)).toFixed(1);
|
||||
console.log(`[IPFS] PIN_REQUEST_BLOCKED cid=${cid} reason="requested ${hoursAgo}h ago, cooldown=3h"`);
|
||||
return {
|
||||
success: false,
|
||||
size: 0,
|
||||
message: `Pin request blocked (already requested ${hoursAgo}h ago)`,
|
||||
blocked: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Check if already pinned
|
||||
const alreadyPinned = await isPinned(cid);
|
||||
if (alreadyPinned) {
|
||||
const size = await getCidSize(cid);
|
||||
const sizeMB = (size / 1024 / 1024).toFixed(2);
|
||||
console.log(`[IPFS] PIN_ALREADY_EXISTS cid=${cid} size_mb=${sizeMB}`);
|
||||
// Remove from cache if it's already pinned
|
||||
pinRequestCache.delete(cid);
|
||||
return {
|
||||
success: true,
|
||||
size,
|
||||
@@ -55,6 +76,9 @@ const pinCid = async (cid) => {
|
||||
alreadyPinned: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Record this pin request
|
||||
pinRequestCache.set(cid, now);
|
||||
|
||||
// Fire and forget - just start the pin operation, don't wait for completion
|
||||
// IPFS daemon will handle the pinning in the background
|
||||
@@ -185,4 +209,5 @@ module.exports = {
|
||||
getPinnedSize,
|
||||
checkIPFSHealth,
|
||||
getIPFSStats,
|
||||
pinRequestCache, // Export for debugging/admin purposes
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user