-
-
Notifications
You must be signed in to change notification settings - Fork 656
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: make edge use token's cache #6893
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Ignored Deployments
|
apiToken, | ||
contextFrom(req), | ||
) | ||
? await apiTokenService.getUserForToken(apiToken) |
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.
Fully rolled out, no need for flag context
expect( | ||
await apiTokenService.getUserForToken(invalidToken), | ||
await apiTokenService.getTokenWithCache(invalidToken), |
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.
Reducing the scope of the test to just fetching the token
secret: string, | ||
flagContext?: IFlagContext, // temporarily added, expected from the middleware | ||
): Promise<IApiUser | undefined> { | ||
async getTokenWithCache(secret: string): Promise<IApiToken | undefined> { |
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.
This method is extracted from getUserForToken
it was moved up to be close to getToken
method above
const validatedTokens: EdgeTokenSchema[] = []; | ||
for (const token of tokens) { | ||
const found = | ||
await this.apiTokenService.getTokenWithCache(token); | ||
if (found) { | ||
validatedTokens.push({ | ||
token: token, | ||
type: found.type, | ||
projects: found.projects, | ||
}); | ||
} |
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.
New behavior checking the tokens one by one, hitting the cache unless one is not present. Invalid tokens are also cached
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.
Nice!
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.
Adding a few metrics
@@ -140,10 +147,12 @@ export class ApiTokenService { | |||
// prevent querying the same invalid secret multiple times. Expire after 5 minutes | |||
this.queryAfter.set(secret, addMinutes(new Date(), 5)); | |||
|
|||
const stopCacheTimer = this.timer('getTokenWithCache.query'); |
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.
This should help answer: how many times do we hit the DB when validating api tokens?
} | ||
|
||
async getValidTokens(tokens: string[]): Promise<ValidatedEdgeTokensSchema> { | ||
if (this.flagResolver.isEnabled('checkEdgeValidTokensFromCache')) { | ||
const stopTimer = this.timer('validateTokensWithCache'); |
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.
This should help answer which one is better in terms of time
About the changes
This PR removes the feature flag
queryMissingTokens
that was fully rolled out.It introduces a new way of checking edgeValidTokens controlled by the flag
checkEdgeValidTokensFromCache
that relies in the cached data but hits the DB if needed.The assumption is that most of the times edge will find tokens in the cache, except for a few cases in which a new token is queried. From all tokens we expect at most one to hit the DB and in this case querying a single token should be better than querying all the tokens.