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 2 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
25 changes: 25 additions & 0 deletions api/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,28 @@ 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 cache The cache time in seconds - defaults to 300
* @returns The response object
*/
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 = 200,
cache?: number
james-a-morris marked this conversation as resolved.
Show resolved Hide resolved
) {
// We only want to cache if the status code is 200 and the
// caching time has been defined.
if (statusCode === 200 && sdk.utils.isDefined(cache)) {
response.setHeader(
"Cache-Control",
`s-max-age=${cache}, stale-while-revalidate=${cache}`
);
}
james-a-morris marked this conversation as resolved.
Show resolved Hide resolved
return response.status(statusCode).json(body);
}
4 changes: 2 additions & 2 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 @@ -235,8 +236,7 @@ const handler = async (
message: "Response data",
responseJson,
});
response.setHeader("Cache-Control", "s-maxage=300");
response.status(200).json(responseJson);
sendResponse(response, responseJson, 200, 300);
james-a-morris marked this conversation as resolved.
Show resolved Hide resolved
} catch (error: unknown) {
return handleErrorCondition("limits", response, logger, error);
}
Expand Down
Loading