Skip to content
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
127ewyo3 wants to merge 1 commit intomasterfrom
127ewyo3-patch-1
Closed

feat: Add worker pool for concurrent DNS request handling#23
127ewyo3 wants to merge 1 commit intomasterfrom
127ewyo3-patch-1

Conversation

@127ewyo3
Copy link
Contributor

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.

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.
@127ewyo3 127ewyo3 closed this Aug 18, 2025
@127ewyo3 127ewyo3 deleted the 127ewyo3-patch-1 branch August 18, 2025 09:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant