This repository was archived by the owner on Aug 18, 2025. It is now read-only.
feat: Add worker pool for concurrent DNS request handling#23
Closed
feat: Add worker pool for concurrent DNS request handling#23
Conversation
feat: Add worker pool for concurrent DNS request handling This commit introduces a goroutine worker pool to optimize the handling of incoming DNS requests. Instead of launching a new goroutine for every single request, a fixed number of worker goroutines are started, and incoming requests are placed into a buffered channel. This approach provides the following benefits: - **Resource Control:** It limits the maximum number of concurrent requests being processed, preventing the server from becoming overwhelmed during traffic spikes. - **Improved Stability:** By bounding concurrency, it ensures predictable resource usage, reducing the risk of resource exhaustion on the server (e.g., too many open files, excessive memory usage). - **Reduced Overhead (for very high-frequency, short-lived tasks):** While Go's goroutines are lightweight, a worker pool can offer a slight performance edge by amortizing the cost of goroutine creation and garbage collection over many requests. The core changes are in `handler.go`: - A new `request` struct is defined to hold the network type, `ResponseWriter`, and `dns.Msg`. - A buffered channel, `requestChan`, is added to the `GODNSHandler` struct to serve as a work queue. - The `NewHandler` function is modified to initialize the worker pool by starting a predefined number of `worker` goroutines. - The `DoTCP` and `DoUDP` methods are simplified to push incoming requests onto the `requestChan`. - The original request processing logic from `DoTCP` and `DoUDP` is moved into a new `handleRequest` method, which is executed by the worker goroutines. This change is a production-level optimization that enhances the server's resilience and resource management under high load. The `server.go` file remains unchanged as it correctly delegates request handling to the `handler` instance.
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
feat: Add worker pool for concurrent DNS request handling
This commit introduces a goroutine worker pool to optimize the handling of incoming DNS requests. Instead of launching a new goroutine for every single request, a fixed number of worker goroutines are started, and incoming requests are placed into a buffered channel.
This approach provides the following benefits:
The core changes are in
handler.go:requeststruct is defined to hold the network type,ResponseWriter, anddns.Msg.requestChan, is added to theGODNSHandlerstruct to serve as a work queue.NewHandlerfunction is modified to initialize the worker pool by starting a predefined number ofworkergoroutines.DoTCPandDoUDPmethods are simplified to push incoming requests onto therequestChan.DoTCPandDoUDPis moved into a newhandleRequestmethod, which is executed by the worker goroutines.This change is a production-level optimization that enhances the server's resilience and resource management under high load. The
server.gofile remains unchanged as it correctly delegates request handling to thehandlerinstance.