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

feat: make edge use token's cache #6893

Merged
merged 3 commits into from
Apr 19, 2024
Merged

feat: make edge use token's cache #6893

merged 3 commits into from
Apr 19, 2024

Conversation

gastonfournier
Copy link
Contributor

@gastonfournier gastonfournier commented Apr 19, 2024

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.

Copy link

vercel bot commented Apr 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Apr 19, 2024 1:23pm
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Visit Preview Apr 19, 2024 1:23pm

codescene-delta-analysis[bot]

This comment was marked as resolved.

codescene-delta-analysis[bot]

This comment was marked as resolved.

@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
apiToken,
contextFrom(req),
)
? await apiTokenService.getUserForToken(apiToken)
Copy link
Contributor Author

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),
Copy link
Contributor Author

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> {
Copy link
Contributor Author

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

Comment on lines +32 to +42
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,
});
}
Copy link
Contributor Author

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

Copy link
Contributor

@chriswk chriswk left a comment

Choose a reason for hiding this comment

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

Nice!

codescene-delta-analysis[bot]

This comment was marked as resolved.

Copy link
Contributor Author

@gastonfournier gastonfournier left a 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');
Copy link
Contributor Author

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');
Copy link
Contributor Author

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

@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@Unleash Unleash deleted a comment from codescene-delta-analysis bot Apr 19, 2024
@gastonfournier gastonfournier merged commit 126b788 into main Apr 19, 2024
7 checks passed
@gastonfournier gastonfournier deleted the api-token-cleanup branch April 19, 2024 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants