-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor DNS resolvers for concurrent lookups
This commit significantly improves the performance of DNS lookups by implementing concurrent query execution across all resolver types. It also refactors the common lookup logic into a shared function to reduce code duplication and improve maintainability. Performance improvements: - Reduced lookup time for multiple queries by ~78% (from 1.356s to 0.297s for a sample query with 10 record types) - Improved CPU utilization (from 1% to 4%) indicating better resource use
- Loading branch information
Showing
8 changed files
with
136 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package resolvers | ||
|
||
import ( | ||
"log/slog" | ||
"sync" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
// QueryFunc represents the signature of a query function | ||
type QueryFunc func(question dns.Question, flags QueryFlags) (Response, error) | ||
|
||
// ConcurrentLookup performs concurrent DNS lookups | ||
func ConcurrentLookup(questions []dns.Question, flags QueryFlags, queryFunc QueryFunc, logger *slog.Logger) ([]Response, error) { | ||
var wg sync.WaitGroup | ||
responses := make([]Response, len(questions)) | ||
errors := make([]error, len(questions)) | ||
|
||
for i, q := range questions { | ||
wg.Add(1) | ||
go func(i int, q dns.Question) { | ||
defer wg.Done() | ||
resp, err := queryFunc(q, flags) | ||
responses[i] = resp | ||
errors[i] = err | ||
}(i, q) | ||
} | ||
|
||
wg.Wait() | ||
|
||
// Collect non-nil responses and handle errors | ||
var validResponses []Response | ||
for i, resp := range responses { | ||
if errors[i] != nil { | ||
logger.Error("error in lookup", "error", errors[i]) | ||
} else { | ||
validResponses = append(validResponses, resp) | ||
} | ||
} | ||
|
||
return validResponses, nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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