Repository navigation
Exercises
Your first improvement to the starter program above is to launch each "scan" of a port in its own goroutine. When you launch your goroutines, ensure that your program waits for them to finish their work.
Hint: The
syncpackage'sWaitGroupis your friend.
- Use the
gokeyword to launch an anonymous function for each port you need to scan. - Watch out for loop iteration gotchas with goroutines and closures.
- Leverage a
sync.WaitGroupto ensure themaingoroutine waits for all the active goroutines you've spun off.
...
2024/05/30 17:24:13 5326 CLOSED (dial tcp 127.0.0.1:5326: connect: connection refused)
2024/05/30 17:24:13 5404 CLOSED (dial tcp 127.0.0.1:5404: connect: connection refused)
2024/05/30 17:24:13 5433 CLOSED (dial tcp 127.0.0.1:5433: connect: connection refused)
2024/05/30 17:24:13 5376 CLOSED (dial tcp 127.0.0.1:5376: connect: connection refused)
2024/05/30 17:24:13 5432 OPEN
2024/05/30 17:24:13 5382 CLOSED (dial tcp 127.0.0.1:5382: connect: connection refused)
2024/05/30 17:24:13 5303 CLOSED (dial tcp 127.0.0.1:5303: connect: connection refused)
2024/05/30 17:24:13 5446 CLOSED (dial tcp 127.0.0.1:5446: connect: connection refused)
2024/05/30 17:24:13 5436 CLOSED (dial tcp 127.0.0.1:5436: connect: connection refused)
2024/05/30 17:24:13 5437 CLOSED (dial tcp 127.0.0.1:5437: connect: connection refused)
...In the above excerpt, you'll notice that the ports being scanned are no longer in sequential order.
e1-synchronized/main.go
Now let's generate and compare a pprof visualization of this concurrent program with the sequential one.
e1-synchronized-pprof/main.go

Observations:
- On the right we have the goroutine that performs the profiling.
- On the left is where the port scans happen, each in their own goroutines.
- Numbers on arrows indicate the number of goroutines that have seen the transition of the specified functions at the time of the sample.
To control the number of goroutines we're spinning up, we can use the "worker pool" pattern.
The worker pool pattern is a common one used to control the number of goroutines that are running concurrently at any given time. Before diving in, you'll need to be familiar with Channels, Buffered Channels, and Channel Synchronization.
- Create a buffered channel that will contain up to n number of ports you want to be scanning at any given moment. This number also correlates to the number of workers you'll have in play.
- When you invoke the worker, you'll need to pass in a ports channel (optionally set to receive-only) and a results channel on which to send results (optionally set to send-only).
- Capture your results in a slice as you read open ports from the results channel.
e2-worker-pool/main.go
In this exercise, you must update the previous exercise and swap the channels mechanism controlling the number of goroutines in flight for a semaphore mechanism instead.
Using your solution to the previous exercise, remove the channels that manage the worker pool and use a semaphore instead.
e3-semaphore/main.go
Using your solution to the previous exercise, modify the proram so that new semaphore acquisitions are done with a context.Context that's been initialized with a timeout.
e4-semaphore-timeout/main.go
Use the pipeline pattern to work through the scans and store the output in a CSV.
- Include a step to generate scan operations
- Include a step to scan
- Include a step filter the scans to open ports only
- Include a step to store the results as they get through the pipeline
e5-pipeline/main.go
The pipeline reintroduced a sequential scan into our port scanner. Let's use the fan-out/fan-in pattern to bring back the concurrent scanning.
- Distribute the scanning process across multiple goroutines
- Include a merge step in addition to the generate, scan, and filter steps from the previous solution.
- Launch n workers to perform the scan step
- Modify the merge step to wait for all scans to complete
e6-fan-out-fan-in/main.go e7-fan-out-fan-in-workers/main.go