-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add vercel blobs to cache historical subgraph data #240
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { fetchAllRequests } from "@libs/oracle-sdk-v2/services/oraclev1/gql/queries"; | ||
import type { OracleType } from "@shared/types"; | ||
|
||
import { oracleTypes } from "@shared/constants"; | ||
import { kv } from "@vercel/kv"; | ||
import { put } from "@vercel/blob"; | ||
import { waitUntil } from "@vercel/functions"; | ||
|
||
type BlobValue = { | ||
url: string; | ||
createdAt: number; | ||
}; | ||
|
||
async function updateBlob( | ||
blobName: string, | ||
url: string, | ||
queryName: string, | ||
oracleType: OracleType, | ||
) { | ||
const uuid = crypto.randomUUID(); | ||
const requests = await fetchAllRequests(url, queryName, oracleType); | ||
const blobPath = `${blobName}-${uuid}.json`; | ||
const { url: blobUrl } = await put(blobPath, JSON.stringify(requests), { | ||
access: "public", | ||
addRandomSuffix: false, | ||
}); | ||
await kv.set( | ||
blobName, | ||
JSON.stringify({ | ||
url: blobUrl, | ||
createdAt: Math.floor(Date.now() / 1000), | ||
}), | ||
); | ||
} | ||
|
||
const BLOB_TTL = 60 * 60 * 24; // 12 hours | ||
|
||
function isBlobExpired(blobValue: BlobValue) { | ||
return Math.floor(Date.now() / 1000) - blobValue.createdAt > BLOB_TTL; | ||
} | ||
|
||
export default async function handler( | ||
_request: NextApiRequest, | ||
response: NextApiResponse, | ||
) { | ||
response.setHeader("Cache-Control", "max-age=0, s-maxage=300"); // Cache for 5 minutes, reset on re-deployment. | ||
|
||
const url = _request.query.url; | ||
const queryName = _request.query.queryName; | ||
const oracleType = _request.query.oracleType; | ||
|
||
if ( | ||
!url || | ||
typeof url !== "string" || | ||
!queryName || | ||
typeof queryName !== "string" || | ||
!oracleType || | ||
typeof oracleType !== "string" || | ||
!oracleTypes.includes(oracleType as OracleType) | ||
) { | ||
response.status(400).send({ | ||
message: "Missing required parameters", | ||
}); | ||
return; | ||
} | ||
|
||
const safeUrl = url.replaceAll("/", "-"); // never allow slashes in names | ||
|
||
const blobName = `${process.env.BLOB_KEY_PREFIX}-${safeUrl}-${queryName}-${oracleType}`; | ||
|
||
const blobValue = await kv.get<BlobValue>(blobName); | ||
|
||
if (blobValue === null || isBlobExpired(blobValue)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that ALL requests are potentially stale by up to 12 hours? For example if request A has been proposed, in this cached data will still be marked as "Requested" for 12 hours until the cache is updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, so the blob will be out of date, but we augment the blob data with local data from the subgraph. One thing I'm assuming is that the graph data doesn't change once its timestamp has passed (i.e. each element that we get from the graph effectively represents an event). So this is a good point. If that's not true, then this may allow for stale state if the timestamp isn't updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These entities from the subgraph don't represent events per se, they are single PriceRequest object that get updated as they change in state ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmmm, yeah, so this may be a problem. |
||
// This forces the vercel function to update the blob in the background. | ||
waitUntil(updateBlob(blobName, url, queryName, oracleType as OracleType)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know |
||
} | ||
|
||
response.status(200).send({ | ||
url: blobValue?.url ?? null, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So does this mean we're sorting from oldest to newest? Then we'd have some newest (< 12hrs old ) requests not stored in the blob, those will be fetched from the subgraph?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Technically, everything is in descending order of timestamp and both are generated with the same code. The local one just has a single parameter difference that it filters for elements after the last timestamp in the blob.