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: add stale-while-revalidate to limits #906

Merged
merged 6 commits into from
Nov 8, 2023
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
28 changes: 28 additions & 0 deletions api/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,31 @@ export function getDefaultRelayerAddress(
return sdk.constants.DEFAULT_SIMULATED_RELAYER_ADDRESS;
}
}

/**
* Performs the needed function calls to return a Vercel Response
* @param response The response client provided by Vercel
* @param body A payload in JSON format to send to the client
* @param statusCode The status code - defaults to 200
* @param cacheSeconds The cache time in non-negative whole seconds
* @param staleWhileRevalidateSeconds The stale while revalidate time in non-negative whole seconds
* @returns The response object
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
* @see https://datatracker.ietf.org/doc/html/rfc7234
* @note Be careful to not set anything negative please. The comment in the fn explains why
*/
export function sendResponse(
Copy link
Contributor Author

@james-a-morris james-a-morris Nov 6, 2023

Choose a reason for hiding this comment

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

The logic in this function is essentially copied/pasted throughout the codebase. I think it would make sense to centralize this as the redundancy makes it error-prone.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. Is it worth setting some defaults on the input arguments? statusCode might be one where we could default to 200 right?

response: VercelResponse,
body: Record<string, unknown>,
statusCode: number,
cacheSeconds: number,
staleWhileRevalidateSeconds: number
Comment on lines +1265 to +1267
Copy link
Contributor

@pxrl pxrl Nov 8, 2023

Choose a reason for hiding this comment

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

It might be nice if these were part of an object so they could be set by name, rather than inferred by order. Then you could probably nuke the comment as well.

options

) {
// Invalid (non-positive/non-integer) values will be considered undefined per RFC-7234.
// Most browsers will consider these invalid and will request fresh data.
response.setHeader(
"Cache-Control",
`s-maxage=${cacheSeconds}, stale-while-revalidate=${staleWhileRevalidateSeconds}`
);
return response.status(statusCode).json(body);
}
11 changes: 4 additions & 7 deletions api/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
HUB_POOL_CHAIN_ID,
ENABLED_ROUTES,
getDefaultRelayerAddress,
sendResponse,
} from "./_utils";

const LimitsQueryParamsSchema = object({
Expand Down Expand Up @@ -225,18 +226,14 @@ const handler = async (
liquidReserves
).toString(),
};

// Instruct Vercel to cache limit data for this token for 5 minutes. Caching can be used to limit number of
// Vercel invocations and run time for this serverless function and trades off potential inaccuracy in times of
// high volume. "max-age=0" instructs browsers not to cache, while s-maxage instructs Vercel edge caching
// to cache the responses and invalidate when deployments update.
logger.debug({
at: "Limits",
message: "Response data",
responseJson,
});
response.setHeader("Cache-Control", "s-maxage=300");
response.status(200).json(responseJson);
// Respond with a 200 status code and 4 minutes of cache cache with
// a minute of stale-while-revalidate.
sendResponse(response, responseJson, 200, 240, 60);
} catch (error: unknown) {
return handleErrorCondition("limits", response, logger, error);
}
Expand Down
Loading