From 649f9477738174bb790f194d24fad0f53c649d35 Mon Sep 17 00:00:00 2001 From: BrianWu Date: Thu, 17 Oct 2024 10:41:46 +0800 Subject: [PATCH] Connect to gateway peers before pin instead of connect periodically --- src/ipfs/index.ts | 22 ++++++++++++++- src/tasks/index.ts | 2 -- src/tasks/ipfs-connect-peers-task.ts | 40 ---------------------------- 3 files changed, 21 insertions(+), 43 deletions(-) delete mode 100644 src/tasks/ipfs-connect-peers-task.ts diff --git a/src/ipfs/index.ts b/src/ipfs/index.ts index eb02317..90b87c9 100644 --- a/src/ipfs/index.ts +++ b/src/ipfs/index.ts @@ -2,9 +2,13 @@ import { BigNumber } from 'bignumber.js'; import { addrToHostPort } from '../utils'; import IpfsHttpClient from 'ipfs-http-client'; import { AbortController } from 'native-abort-controller'; +import { CrustGWPeers, DefaultCrustGWPeers } from '../tasks/ipfs-update-peers-list-task'; +import { createChildLogger } from '../utils/logger'; const CID = (IpfsHttpClient as any).CID; // eslint-disable-line +const logger = createChildLogger({ moduleId: 'ipfs-api' }); + export default class IpfsApi { private readonly ipfs: any; // eslint-disable-line @@ -28,8 +32,9 @@ export default class IpfsApi { pin(c: string, to: number): [AbortController, Promise] { const controller = new AbortController(); const signal = controller.signal; - + const result = async () => { + await this.connectGatewayPeers(); const cid = new CID(c); const pin = await this.ipfs.pin.add(cid, { timeout: to, signal }); return cid.equals(pin) as boolean; @@ -38,6 +43,21 @@ export default class IpfsApi { return [controller, result()]; } + async connectGatewayPeers(): Promise { + const peersToConnect = CrustGWPeers.size > 0 ? Array.from(CrustGWPeers): DefaultCrustGWPeers; + + let successCount = 0; + for (const peer of peersToConnect) { + try { + await this.ipfs.swarm.connect(peer); + successCount++; + } catch (error) { + logger.debug(`Failed to connect to peer: '${peer}'. Error: ${error}`); + } + } + logger.debug(`Connect to ${successCount} dedicated crust gateway peers.`); + } + /** * NO USE * Pin remove file by a given cid diff --git a/src/tasks/index.ts b/src/tasks/index.ts index 4d449c2..ee2c123 100644 --- a/src/tasks/index.ts +++ b/src/tasks/index.ts @@ -12,7 +12,6 @@ import { createSealCleanupTask } from './seal-cleanup-task'; import { createSealStatuUpdater } from './seal-status-updater-task'; import { createTelemetryReportTask } from './telemetry-task'; import { createIpfsUpdatePeersListTask } from './ipfs-update-peers-list-task'; -import { createIpfsConnectPeersTask } from './ipfs-connect-peers-task'; /** * create simpile tasks which only handle start/stop @@ -32,7 +31,6 @@ export async function createSimpleTasks( createNodeInfoUpdateTask, createSealCleanupTask, createIpfsUpdatePeersListTask, - createIpfsConnectPeersTask ]; return Bluebird.mapSeries(tasks, (t) => { return t(context, logger); diff --git a/src/tasks/ipfs-connect-peers-task.ts b/src/tasks/ipfs-connect-peers-task.ts deleted file mode 100644 index 30b20d0..0000000 --- a/src/tasks/ipfs-connect-peers-task.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { Logger } from 'winston'; -import { AppContext } from '../types/context'; -import { SimpleTask } from '../types/tasks'; -import { makeIntervalTask } from './task-utils'; -import { CrustGWPeers, DefaultCrustGWPeers } from './ipfs-update-peers-list-task'; - - -/** - * task to connect to dedicated crust gateway peers periodly to improve the ipfs file pin success rate - */ -async function handleIpfsConnectPeers(context: AppContext, logger: Logger): Promise { - - const peersToConnect = CrustGWPeers.size > 0 ? Array.from(CrustGWPeers): DefaultCrustGWPeers; - - let successCount = 0; - for (const peer of peersToConnect) { - try { - await context.ipfsApi.connectPeer(peer); - successCount++; - } catch (error) { - logger.error(`Failed to connect to peer: '${peer}'. Error: ${error}`); - } - } - logger.info(`Connect to ${successCount} dedicated crust gateway peers.`); -} - -export async function createIpfsConnectPeersTask( - context: AppContext, - loggerParent: Logger, -): Promise { - const ipfsInterval = 3 * 60 * 1000; // Connect to dedicated peers every 3 minutes - return makeIntervalTask( - 30 * 1000, - ipfsInterval, - 'ipfs-connect-peers', - context, - loggerParent, - handleIpfsConnectPeers, - ); -}