Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set keepalive: false in http(s) agent to ipfs #2065

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/cli/src/build-ipfs-connection.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@ import * as ipfsClient from 'ipfs-http-client'
import { DiagnosticsLogger, IpfsApi } from '@ceramicnetwork/common'
import { IpfsMode } from './daemon-config.js'
import { path } from 'go-ipfs'
import * as http from 'http'
import * as https from 'https'

const IPFS_DHT_SERVER_MODE = process.env.IPFS_DHT_SERVER_MODE === 'true'
const IPFS_GET_TIMEOUT = 60000 // 1 minute

const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })

const ipfsHttpAgent = (ipfsEndpoint: string) => {
const agentOptions = {
keepAlive: false,
maxSockets: Infinity
}
if (ipfsEndpoint.startsWith('https')) {
return new https.Agent(agentOptions)
} else {
return new http.Agent(agentOptions)
}
}

const ipfsHttpModule = {
create: (ipfsEndpoint: string) => {
return ipfsClient.create({
url: ipfsEndpoint,
ipld: { codecs: [dagJose] },
agent: ipfsHttpAgent(ipfsEndpoint)
})
},
}
Expand All @@ -30,6 +45,7 @@ export async function buildIpfsConnection(
url: ipfsEndpoint,
ipld: { codecs: [dagJose] },
timeout: IPFS_GET_TIMEOUT,
agent: ipfsHttpAgent(ipfsEndpoint)
})
} else {
return createGoIPFS()
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ export class Utils {
signal: AbortSignal
) {
if (typeof cid === 'string') cid = CID.parse(cid.replace('ipfs://', ''))
let format = await ipfsApi.codecs.getCodec(cid.code).then((f) => f.name)
// The try catch behaviour of the js-ipfs-http-client was causing timing issues (socket hangup)
// https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs-http-client/src/block/put.js#L34
if (format === 'dag-cbor') format = 'cbor'
else if (format === 'dag-pb') format = 'protobuf'
const format = await ipfsApi.codecs.getCodec(cid.code).then((f) => f.name)
smrz2001 marked this conversation as resolved.
Show resolved Hide resolved
const mhtype = await ipfsApi.hashers.getHasher(cid.multihash.code).then((mh) => mh.name)
const version = cid.version
await ipfsApi.block.put(block, { format, mhtype, version, signal })
Expand Down