Skip to content

Commit efed0df

Browse files
committedJan 2, 2018
Adds commenting to filter-resolved
1 parent 1f16e5d commit efed0df

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
 

‎filter-resolved/main.go

+27
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,30 @@ import (
1111

1212
func main() {
1313

14+
// configure the concurrency flag
1415
concurrency := 20
1516
flag.IntVar(&concurrency, "c", 20, "Set the concurrency level")
1617

18+
// parse the flags
1719
flag.Parse()
1820

21+
// jobs is a channel of strings. We'll send domains on the
22+
// channel so that a bunch of workers can receive them and
23+
// try to resolve them
1924
jobs := make(chan string)
2025

26+
// A WaitGroup is useful if you have lots of goroutines
27+
// and you want to know when they're all done.
2128
var wg sync.WaitGroup
29+
30+
// spin up a whole bunch of workers
2231
for i := 0; i < concurrency; i++ {
32+
// tell the waitgroup about the new worker
2333
wg.Add(1)
2434

35+
// launch a goroutine that takes domains off the
36+
// jobs channel, tries to resolve them and outputs
37+
// them only if there was no error
2538
go func() {
2639
for domain := range jobs {
2740
_, err := net.ResolveIPAddr("ip4", domain)
@@ -30,19 +43,33 @@ func main() {
3043
}
3144
fmt.Println(domain)
3245
}
46+
47+
// when the jobs channel is closed the loop
48+
// above will stop; then we need to tell the
49+
// waitgroup that the worker is done
3350
wg.Done()
3451
}()
3552
}
3653

54+
// open stdin as a scanner. That makes it super easy
55+
// to deal with line-delimited input
3756
sc := bufio.NewScanner(os.Stdin)
3857
for sc.Scan() {
58+
// send each line (a domain) on the jobs channel
3959
jobs <- sc.Text()
4060
}
61+
62+
// as soon as we're done sending all the jobs we can
63+
// close the jobs channel. If we don't the workers
64+
// will never stop.
4165
close(jobs)
4266

67+
// check there were no errors reading stdin (unlikely)
4368
if err := sc.Err(); err != nil {
4469
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
4570
}
71+
72+
// wait for the workers to finish doing their thing
4673
wg.Wait()
4774

4875
}

0 commit comments

Comments
 (0)