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: add cache to check endpoint #1391

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
43 changes: 37 additions & 6 deletions packages/api/src/routes/nfts-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,50 @@ import { DBClient } from '../utils/db-client'
import { parseCid } from '../utils/utils.js'
import { toCheckNftResponse } from '../utils/db-transforms.js'

const CACHE_MAX_AGE_NO_DEAL_YET = 10 * 60 // in seconds (10 minutes)
const db = new DBClient(database.url, secrets.database)

/** @type {import('../bindings').Handler} */
export const nftCheck = async (event, { params }) => {
const cache = await caches.open('nft:check')
// @ts-ignore match function not found in type https://developer.mozilla.org/en-US/docs/Web/API/Cache
let res = await caches.match(event.request.url)

if (res) {
return res
}

const cid = parseCid(params.cid)
const content = await db.getContent(cid.contentCid)

if (content) {
return new JSONResponse({
ok: true,
value: toCheckNftResponse(cid.sourceCid, content),
})
} else {
if (!content) {
throw new HTTPError('NFT not found', 404)
}

const nftCheckResponseValue = toCheckNftResponse(cid.sourceCid, content)
const headers =
!!nftCheckResponseValue.deals.find((d) => d.status === 'active') &&
nftCheckResponseValue.pin.status === 'pinned'
? {
// cache status response with max age
'Cache-Control': `public, max-age=${CACHE_MAX_AGE_NO_DEAL_YET}`,
}
: undefined // cache default
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the default though?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should really be specific about the cache period. In this case it's unknown when another deal for the CID may become available so I expect we'd only cache for a relatively short period of time...

Was there an observed perf issue here? This makes the route more complicated and it's used in our monitoring so could mask any underlying issue...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Per https://developers.cloudflare.com/workers/runtime-apis/cache/#headers and https://developers.cloudflare.com/cache/how-to/configure-cache-status-code/#edge-ttl default would be 120 minutes.

We can probably use default if we have at least a deal instead?

Was there an observed perf issue here? This makes the route more complicated and it's used in our monitoring so could mask any underlying issue...

Yesterday we had some timeouts for instance, I don't have empirical answers for this at the moment. The main motivation for this is the work in #1386 which will increase significantly the calls to check API

Copy link
Contributor

Choose a reason for hiding this comment

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

If a CID is pinned and in has deals, then some caching of the repsonse would be sensible. Otherwise, caching should be minimal or avoided here.

I'm against landing #1386 as I dont want us to hit the nft.storage api for every single CID fetched from the gateway.


res = new JSONResponse(
{
ok: true,
value: nftCheckResponseValue,
},
{
headers,
}
)

// Cache if pin status is pinned
if (nftCheckResponseValue.pin.status === 'pinned') {
event.waitUntil(cache.put(event.request, res.clone()))
}

return res
}
7 changes: 4 additions & 3 deletions packages/api/src/utils/json-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export class JSONResponse extends Response {
* @param {ResponseInitializerDict} [init]
*/
constructor(body, init = {}) {
const headers = {
super(JSON.stringify(body), {
...init,
headers: {
'content-type': 'application/json;charset=UTF-8',
...init.headers,
},
}
super(JSON.stringify(body), { ...init, ...headers })
})
}
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,9 @@
integrity sha512-hh7qzfT0+1rkKiZrZnttRZxjZLzcHHZNQ7XmzA8De0YJxhg/tEovmczM1AjuGZJr8sr69gfOFtfZgqz2s1/p5Q==

"@cloudflare/workers-types@^3.3.1":
version "3.4.0"
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.4.0.tgz#80311e14df2f7f8c2cfcdce945b4f4ad8f9b03b1"
integrity sha512-i/3czUrt6YqbOWl44OtIqd0cSZvEVXp/1oD/DZylC4PHZL3q/BhbamdEVeVhc/HPk4iD/7MZ2HGaIMO4Z4b12A==
version "3.3.1"
resolved "https://registry.yarnpkg.com/@cloudflare/workers-types/-/workers-types-3.3.1.tgz#8847543bda320472252708c29aaf7bac3374e814"
integrity sha512-GJFDgWd8ZHlr/m+Q2mp4xUl0/FIPpR6kf0Ix2C78E9HeJyUCW0c0w2EKCvgEDFFKB2rkzIX3eBqZCnLsbsw8zQ==

"@concordance/react@^2.0.0":
version "2.0.0"
Expand Down