parallel first dial using singleflight #287
Merged
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.
Observed behaviour/issue
If a domain, such as
hackerone.com
, resolves to multiple IPs, Fastdialer previously used sequential iteration. This was inefficient because if a port on the address was closed, Fastdialer would iterate over all IPs and each failed iteration would add to the total time. This issue was exacerbated when Nuclei called this function on 1000 goroutines, creating a bottleneck and delay in execution.Proposed Changes
After multiple investigations and experiments in #284, the issue is mitigated by the following changes:
dialWrap
is created. This is a wrapper aroundnet.Dialer
that is synchronized across all current dials using a pattern similar tosingleflight/simpleflight
. This blocks all concurrent calls to this particular address temporarily until thefirstdial
function returns.firstdial
function dials the given address using all existing IPs in parallel. If all dials to all IPs fail, we assume that this port is closed/filtered and use ErrPortClosedOrFiltered. This error is returned to all paused dials as well as subsequent dials to this address until this address is evicted from the cache. If at least one connection is established, this is considered successful and all successful connections are distributed across paused dials. For remaining paused dials, a new dial is initiated usingdialWrap
.Happy Eyeballs Algorithm
just like net.Dialer does internally when a domain is used instead of serial/sequential dials which happened earlier. In the Happy Eyeballs algorithm, IPs are split into IPv4 & IPv6 groups and each group is dialed sequentially. The first IPv6 group is given a head start with 300ms. The connection that is established first is used while the other is cancelled. This algorithm is derived from the understanding that IPv6 connections can easily be established to servers compared to IPv4 because there is nohole punching
orNAT
involved.Misc changes
errkit
library to standardize error handling and facilitate the differentiation of permanent and temporary network errors in Nuclei and other upstream projects.Note
almost all of the new logic is implemented by abstracting net.Dialer at
utils/dialwrap
to keep refactor / changes minimal and easy to replaceTip
Checkout Associated PR in nuclei for all benefits / improvements this PR provides