Skip to content

Commit

Permalink
fix: add stale-while-revalidate to limits (#906)
Browse files Browse the repository at this point in the history
* fix: add stale-while-revalidate to limits

* improve: body send

* improve: tune parameters

* chore: update docs

* improve: removed defaults

* chore: make simple function
  • Loading branch information
james-a-morris authored Nov 8, 2023
1 parent a0047c1 commit 1fd9b1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
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(
response: VercelResponse,
body: Record<string, unknown>,
statusCode: number,
cacheSeconds: number,
staleWhileRevalidateSeconds: number
) {
// 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

2 comments on commit 1fd9b1c

@vercel
Copy link

@vercel vercel bot commented on 1fd9b1c Nov 8, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

goerli-frontend-v2 – ./

goerli-frontend-v2.vercel.app
goerli-frontend-v2-uma.vercel.app
goerli-frontend-v2-git-master-uma.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1fd9b1c Nov 8, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.