-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patching stuck-go routines causing deadline errors (#381)
* feat: added timeout to dns + singleflight for caching initial bulk resolutions * fixing singleflight * . * atomic * removing log + finalize * fix routine leak * fix race --------- Co-authored-by: Ice3man <nizamulrana@gmail.com>
- Loading branch information
1 parent
93a6f7b
commit 3c45460
Showing
9 changed files
with
203 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
// this example is to test the concurrency of the dialer along | ||
// with ensuring that maximum connection time doesn't exceed 3 seconds | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/projectdiscovery/fastdialer/fastdialer" | ||
) | ||
|
||
func main() { | ||
err := BenchmarkDial("scanme.sh", 1000) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type connResult struct { | ||
target string | ||
elapsed time.Duration | ||
err error | ||
} | ||
|
||
func BenchmarkDial(target string, iterations int) error { | ||
options := fastdialer.DefaultOptions | ||
fd, err := fastdialer.NewDialer(options) | ||
if err != nil { | ||
return errors.Join(err, errors.New("failed to create dialer")) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
tasks := make(chan string, iterations) | ||
results := make(chan connResult, iterations) | ||
|
||
var wg sync.WaitGroup | ||
for w := 0; w < 10; w++ { | ||
wg.Add(1) | ||
go worker(ctx, fd, tasks, results, &wg) | ||
} | ||
|
||
go func() { | ||
for i := 0; i < iterations; i++ { | ||
tasks <- target | ||
} | ||
close(tasks) | ||
}() | ||
|
||
go func() { | ||
wg.Wait() | ||
close(results) | ||
}() | ||
|
||
for result := range results { | ||
if result.err != nil { | ||
return result.err | ||
} | ||
if result.elapsed.Seconds() > 3 { | ||
return errors.New("connection took too long") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func worker(ctx context.Context, fd *fastdialer.Dialer, tasks <-chan string, results chan<- connResult, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
|
||
for task := range tasks { | ||
start := time.Now() | ||
conn, err := fd.Dial(ctx, "tcp", task+":443") | ||
elapsed := time.Since(start) | ||
|
||
if err == nil && conn != nil { | ||
conn.Close() | ||
} | ||
|
||
results <- connResult{ | ||
target: task, | ||
elapsed: elapsed, | ||
err: err, | ||
} | ||
} | ||
} |
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
Empty file.
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
Oops, something went wrong.