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

feat: use blockstore sessions #50

Merged
merged 9 commits into from
May 9, 2024
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Apr 11, 2024

Adds a configurable session cache that creates sessions based on the base URL of the requested resource.

E.g. https://Qmfoo.ipfs.gateway.com/foo.txt andhttps://Qmfoo.ipfs.gateway.com/bar.txt will be loaded from the same session.

Defaults to 100 sessions maximum with a TTL of one minute. These are arbitrary numbers that will require some tweaking.

Adds allowInsecure and allowLocal options to createVerifiedFetch factory - because we search for gateways in the routing we need to exclude those that have local or http addresses, otherwise we'll get mixed content/connection errors.

This makes testing against local gateways difficult so pass true for these options to not filter out private/insecure hosts.

Depends on:

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@achingbrain
Copy link
Member Author

achingbrain commented Apr 11, 2024

In draft because it needs the next Helia release to work and also because the abort-handling tests are skipped pending a refactor - they feature extensive stubbing of internals which have now changed.

These need refactoring to land nodejs/node#42 anyway so...

Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, i ran tests locally here and I get:

  custom dns-resolvers
(node:47763) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 101 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
# lots of these...
(node:47763) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 585 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
^C                                                                                                                                                                                   [1m-16.536s]

* - https://gateway.org/ipfs/Qmfoo/bar.txt -> /ipfs/Qmfoo
* - etc
*/
export function resourceToSessionCacheKey (url: string | CID): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor nit that has no actual impact: I would prefer ip[fn]s://${cidOrPeerIdOrDnsLink}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it makes no difference at all 🤣

I've made the change.

@@ -9,6 +9,7 @@ import { peerIdFromString } from '@libp2p/peer-id'
import { Key } from 'interface-datastore'
import { exporter } from 'ipfs-unixfs-exporter'
import toBrowserReadableStream from 'it-to-browser-readablestream'
import { LRUCache } from 'lru-cache'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we have src/utils/tlru.ts we could use instead of pulling in another dep

Copy link
Member Author

@achingbrain achingbrain Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/utils/tlru.ts is based on hashlru which doesn't have the equivalent of lru-cache's dispose option which we need to clean up sessions before they are evicted from the cache.

TBH lru-cache is 10x more downloaded on npm than hashlru and it does everything src/utils/tlru.ts does, maybe we should swap that out for lru-cache as a follow-up?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good callout, lets do it

packages/verified-fetch/test/abort-handling.spec.ts Outdated Show resolved Hide resolved
@SgtPooki
Copy link
Member

FYI, i ran tests locally here and I get:

(node:47763) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 585 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit

Enabling debug messaging:

helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg session is ready +0ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg found new providers re-retrieving QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg +0ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg fetching BLOCK for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg from https://cesginc.com/p2p/12D3KooWPNbkEgjdBNeaCGpsgCrPRETe4uBZf1ShFXStobdN18ys +0ms
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 254 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 255 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 256 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 257 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 258 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 259 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 260 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 261 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 262 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 263 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 264 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 265 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
(node:48764) MaxListenersExceededWarning: Possible EventTarget memory leak detected. 266 abort listeners added to [AbortSignal]. Use events.setMaxListeners() to increase limit
  helia:trustless-gateway-block-broker:cesginc.com GET https://cesginc.com/ipfs/QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg?format=raw 401 +0ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg no session peers had block for for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg, finding new providers +34ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg finding 1-5 new provider(s) for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg +0ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg:error error retrieving session block for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg Error: unable to fetch raw block for CID QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg
    at TrustlessGateway.getRawBlock (file:///Users/sgtpooki/code/work/protocol.ai/ipfs/helia-verified-fetch/node_modules/@helia/block-brokers/src/trustless-gateway/trustless-gateway.ts:90:13)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at TrustlessGatewaySession.queryProvider (file:///Users/sgtpooki/code/work/protocol.ai/ipfs/helia-verified-fetch/node_modules/@helia/block-brokers/src/trustless-gateway/session.ts:41:19)
    at raceSignal (file:///Users/sgtpooki/code/work/protocol.ai/ipfs/helia-verified-fetch/node_modules/race-signal/src/index.ts:46:12)
    at Job.run (file:///Users/sgtpooki/code/work/protocol.ai/ipfs/helia-verified-fetch/node_modules/@libp2p/utils/src/queue/job.ts:78:22) +237ms
  helia:trustless-gateway-block-broker:cesginc.com GET https://cesginc.com/ipfs/QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg?format=raw 429 +3ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg no session peers had block for for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg, finding new providers +3ms
  helia:trustless-gateway:session:QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg finding 1-5 new provider(s) for QmVP2ip92jQuMDezVSzQBWDqWFbp9nyCHNQSiciRauPLDg +0ms

Adds a configurable session cache that creates sessions based on
the base URL of the requested resource.

E.g. `https://Qmfoo.ipfs.gateway.com/foo.txt` and`https://Qmfoo.ipfs.gateway.com/bar.txt`
will be loaded from the same session.

Defaults to 100 sessions maximum with a TTL of one minute. These
are arbitrary numbers that will require some tweaking.
@achingbrain
Copy link
Member Author

achingbrain commented Apr 22, 2024

I think that might actually be a bug in node - nodejs/undici#3157 - there's a workaround here: ipfs/helia#511

Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. I would love to give this a run locally. Is there anything else we're waiting on here?

@achingbrain achingbrain marked this pull request as ready for review May 1, 2024 11:20
@achingbrain achingbrain requested a review from a team as a code owner May 1, 2024 11:20
@achingbrain
Copy link
Member Author

Is there anything else we're waiting on here?

Just the Helia release with HTTP Gateway routing - this has shipped now so we're not waiting on anything else 👍

Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed updated changes that came from #73 & merge from main

@SgtPooki SgtPooki merged commit 541dd64 into main May 9, 2024
19 checks passed
@SgtPooki SgtPooki deleted the feat/use-blockstore-sessions branch May 9, 2024 17:57
github-actions bot pushed a commit that referenced this pull request May 9, 2024
Copy link

github-actions bot commented May 9, 2024

🎉 This PR is included in version 1.4.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

github-actions bot pushed a commit that referenced this pull request May 9, 2024
## @helia/verified-fetch-interop [1.22.0](https://github.com/ipfs/helia-verified-fetch/compare/@helia/verified-fetch-interop-1.21.1...@helia/verified-fetch-interop-1.22.0) (2024-05-09)

### Features

* use blockstore sessions ([#50](#50)) ([541dd64](541dd64))

### Dependencies

* **@helia/verified-fetch:** upgraded to 1.4.0
Copy link

github-actions bot commented May 9, 2024

🎉 This PR is included in version 1.22.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented May 9, 2024

🎉 This PR is included in version 1.0.0 🎉

The release is available on npm package (@latest dist-tag)

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants