-
Notifications
You must be signed in to change notification settings - Fork 186
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
WIP: feat(server/auth): cache auth context results in Redis #3882
base: main
Are you sure you want to change the base?
Conversation
📸 Preview service has generated an image. |
📸 Preview service has generated an image. |
📸 Preview service has generated an image. |
📸 Preview service has generated an image. |
📸 Preview service has generated an image. |
📸 Preview service has generated an image. |
} | ||
}): RetrieveFromCache<T> => { | ||
const { options, retrieveFromSource, cache } = deps | ||
const reqLogger = maybeLoggerWithContext({ logger: deps.options.logger }) |
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.
I know it was created in a different PR, but maybeLoggerWithContext()
typing is a bit lacking, as shown in this PR - even tho deps.options.logger is definitely defined, the return type is optional.
That would be fixed through generics:
export const maybeLoggerWithContext = <L extends Optional<Logger>>({ logger }: { logger: L }): L => {
...
}
But honestly it's not clear to me why you'd want to return undefined if no logger was passed in anyway. Wouldn't it be better to create a new logger instance, if one wasn't passed in?
req.log = req.log.child({ authContext: loggedContext }) | ||
if (!authContext.auth && authContext.err) { | ||
const defaultMessage = 'Unknown Auth context error' | ||
//NOTE due to the possibility of the auth context being rehydrated from cache, we cannot assert the error with `instanceof` and must check the name |
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.
i don't like that it breaks the error in the first place, but also the fact that AuthContext - the TS type - is now conditionally wrong, because if its retrieved from cache the error key most certainly won't be an Error subtype
can we properly rebuild the error, even if its just new Error(oldMessage)
? retrieveViaCache() could support a custom formatter, if it doesn't already
Description & motivation
Caches the result of the whole auth pipeline in Redis for a configurable amount of time. Defaults to a couple of seconds.
Changes:
Factory
keywordTo-do before merge:
Screenshots:
Validation of changes:
Logs (read from bottom up):
Showing a cache miss and then the subsequent auth-related database calls and the cache being set before getting to the business-related database calls.
Showing a cache hit, and no auth-related database calls:
![Screenshot 2025-02-06 at 10 21 35](https://private-user-images.githubusercontent.com/68657/410411072-782d1a12-e0e0-4d80-b32a-7228d57f8092.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk5MDU0ODEsIm5iZiI6MTczOTkwNTE4MSwicGF0aCI6Ii82ODY1Ny80MTA0MTEwNzItNzgyZDFhMTItZTBlMC00ZDgwLWIzMmEtNzIyOGQ1N2Y4MDkyLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE4VDE4NTk0MVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY2YTBlZWRkNTQ2YmFhODJlNTg2ZTJkOGFjZjcwZTk3MzhlNzc2ZThkMDRkMmIxMWNhODE3ZTY0YWI2MGI3NDcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.8losNKcLe9FlWdVAgogvaLJ0ZdyTe94Gm7UDzrtdgO8)
Checklist:
References