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: add in-memory cache for DNS #6073

Merged
merged 4 commits into from
Oct 21, 2024
Merged

feat: add in-memory cache for DNS #6073

merged 4 commits into from
Oct 21, 2024

Conversation

link2xt
Copy link
Collaborator

@link2xt link2xt commented Oct 20, 2024

This adds "stale-while-revalidate" in-memory cache for DNS. Instead of calling tokio::net::lookup_host we use previous result of tokio::net::lookup_host immediately and spawn revalidation task in the background. This way all lookups after the first successful return immediately.

Most of the time results returned by resolvers are the same anyway, but with this cache we avoid waiting 60 second timeout if DNS request is lost. Common reason result may be different is round-robin DNS load balancing and switching from IPv4 to IPv6 network. For round-robin DNS we don't break load balancing but simply use a different result, and for IPv6 we anyway likely have a result in persistent cache and can use IPv4 otherwise.

Especially frequest should be the case when you send a message over SMTP and SMTP connection is stale (older than 60 s), so we open a new one. With this change new connection will be set up faster as you don't need to wait for DNS resolution, so message will be sent faster.

@link2xt link2xt force-pushed the link2xt/dns-in-memory-cache branch 3 times, most recently from 2acaa2d to 6485951 Compare October 20, 2024 05:58
@link2xt link2xt self-assigned this Oct 20, 2024
@link2xt link2xt force-pushed the link2xt/dns-in-memory-cache branch from 6485951 to 58cab7b Compare October 20, 2024 15:38
@link2xt link2xt requested review from iequidoo, hpk42 and Hocuri October 20, 2024 15:41
src/net/dns.rs Show resolved Hide resolved
src/net/dns.rs Outdated Show resolved Hide resolved
src/net/dns.rs Outdated Show resolved Hide resolved
src/net/dns.rs Outdated Show resolved Hide resolved
@link2xt link2xt force-pushed the link2xt/dns-in-memory-cache branch from 58cab7b to ad20db5 Compare October 20, 2024 18:14
@link2xt link2xt force-pushed the link2xt/dns-in-memory-cache branch from ad20db5 to 2c317ce Compare October 20, 2024 18:32
src/net/dns.rs Outdated Show resolved Hide resolved
src/net/dns.rs Outdated Show resolved Hide resolved
src/net/dns.rs Outdated Show resolved Hide resolved
@link2xt link2xt merged commit c5cadd9 into main Oct 21, 2024
35 checks passed
@link2xt link2xt deleted the link2xt/dns-in-memory-cache branch October 21, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants