fix: replace Mutex<DnsState> with OnceCell in NonLocalDnsResolver#2449
Merged
svix-jplatte merged 4 commits intoJul 10, 2026
Merged
Conversation
svix-jplatte
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! I'm wondering why we ever made new_resolver async given that it does no async I/O. Would you mind also changing that function to do all its actual work in a blocking tokio task?
| builder.build() | ||
| }) | ||
| .await | ||
| .unwrap_or_else(|e| std::panic::resume_unwind(e.into_panic())) |
Member
There was a problem hiding this comment.
Nice! I hadn't encountered this pattern before, but it seems more sensible than a plain unwrap.
Contributor
Author
Welcome looking forward to contribute more! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
NonLocalDnsResolverusedArc<Mutex<DnsState>>for lazy initialization of the DNS resolver. During the Init state, the mutex lock was held across an.awaitboundary specifically across thenew_resolver().awaitcall which performs I/O (reads system DNS config and builds the resolver).This meant that under concurrent webhook dispatch, all DNS calls arriving before initialization completed would block waiting to acquire the mutex, serializing them behind a single I/O operation. On a high-throughput instance processing thousands of webhooks per second, this is a contention point at startup.
Solution
Replaced
Arc<Mutex<DnsState>>withArc<tokio::sync::OnceCell<TokioResolver>>. Theget_or_try_initmethod handles the lazy initialization correctly only one caller runs the initializer, concurrent callers wait on a lightweight internal notifier (not a mutex), and all subsequent calls after initialization is complete are lock-free atomic loads.The
DnsStateenum and the Arc wrapper aroundTokioResolverare both removed as they are no longer needed.new_resolver()now returnsTokioResolverdirectly. No behavior change the DNS resolution logic and IP filtering are untouched.tokio::sync::OnceCellrequires no new dependency as tokio is already pulled in withfeatures = ["full"].