-
-
Notifications
You must be signed in to change notification settings - Fork 287
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: dns caching manager #1294
base: develop
Are you sure you want to change the base?
Conversation
This is done as tmdb ttl is very less like 40 seconds so to make sure any issues wont be caused due to cached dns (previously we were caching for 5 minutes no matter what ttl)
server/routes/settings/index.ts
Outdated
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 will be removed. It was for testing quickly
// Add DNS caching | ||
dns.lookup = (() => { | ||
const wrappedLookup = function ( | ||
hostname: string, | ||
options: | ||
| number | ||
| dns.LookupOneOptions | ||
| dns.LookupOptions | ||
| dns.LookupAllOptions, | ||
callback: ( | ||
err: NodeJS.ErrnoException | null, | ||
address: string | dns.LookupAddress[] | undefined, | ||
family?: number | ||
) => void | ||
): void { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} | ||
|
||
dnsCache | ||
.lookup(hostname) | ||
.then((result) => { | ||
if ((options as dns.LookupOptions).all) { | ||
callback(null, [ | ||
{ address: result.address, family: result.family }, | ||
]); | ||
} else { | ||
callback(null, result.address, result.family); | ||
} | ||
}) | ||
.catch((error) => { | ||
callback(error, undefined, undefined); | ||
}); | ||
} as typeof dns.lookup; | ||
|
||
(wrappedLookup as any).__promisify__ = async function ( | ||
hostname: string, | ||
options?: dns.LookupAllOptions | dns.LookupOneOptions | ||
): Promise<dns.LookupAddress[] | { address: string; family: number }> { | ||
const result = await dnsCache.lookup(hostname); | ||
|
||
if (options && 'all' in options && options.all === true) { | ||
return [{ address: result.address, family: result.family }]; | ||
} | ||
return { address: result.address, family: result.family }; | ||
}; | ||
|
||
return wrappedLookup; | ||
})(); | ||
|
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.
Couldn't you implement this inside the dnsCacheManagerItself? It floods the server a bit.
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.
The flood is coming from the debug logging right? Thats to be removed. I have it added rn to sort of test it. Or if not that what do you mean?
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.
No not that, i meant you could create a function inside server/utils/dnsCacheManager.ts
that contains all this, because all this code to overwrite the lookup function takes a lot of space inside the startup function of the server.
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.
Ah. And then call it here?
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.
yes
const cached = this.cache.get(hostname); | ||
if (cached) { | ||
const age = Date.now() - cached.timestamp; | ||
const ttlRemaining = Math.max(0, cached.ttl - age); |
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.
What's the point of the Math.max
? Your code would work the same without it
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 had some issues where ttl went to negative values. That's why
Description
(wip)
Screenshot (if UI-related)
To-Dos
pnpm build
pnpm i18n:extract
Issues Fixed or Closed