Skip to content

Exercises

Johnny Boursiquot edited this page May 31, 2024 · 3 revisions

Exercise 1: Using and synchronizing goroutines

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 sync package's WaitGroup is your friend.

Objectives

  1. Use the go keyword to launch an anonymous function for each port you need to scan.
  2. Watch out for loop iteration gotchas with goroutines and closures.
  3. Leverage a sync.WaitGroup to ensure the main goroutine waits for all the active goroutines you've spun off.

Sample Output

...
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.

⏰ Time: 15 min

Solution Walkthrough

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

Concurrent port scan goroutine profile

Observations:

  1. On the right we have the goroutine that performs the profiling.
  2. On the left is where the port scans happen, each in their own goroutines.
  3. Numbers on arrows indicate the number of goroutines that have seen the transition of the specified functions at the time of the sample.

Exercice 2: Manage unbounded concurrency with worker pools

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.

Objectives

  1. 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.
  2. 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).
  3. Capture your results in a slice as you read open ports from the results channel.

Resources

Solution Walkthrough

e2-worker-pool/main.go

Exercise 3: Use a semaphore in the port scanner

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.

Objectives

Using your solution to the previous exercise, remove the channels that manage the worker pool and use a semaphore instead.

What you need to know

Solution Walkthrough

e3-semaphore/main.go

Exervice 4: Use a context.Context to constrain semaphore acquisition

Objectives

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.

What you need to know

Solution Walkthrough

e4-semaphore-timeout/main.go

Exercise 5: Pipelines

Use the pipeline pattern to work through the scans and store the output in a CSV.

Objectives

  1. Include a step to generate scan operations
  2. Include a step to scan
  3. Include a step filter the scans to open ports only
  4. Include a step to store the results as they get through the pipeline

Solution Walkthrough

e5-pipeline/main.go

Exercise 6: Fan-Out Fan-In

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.

Objectives

  1. Distribute the scanning process across multiple goroutines
  2. Include a merge step in addition to the generate, scan, and filter steps from the previous solution.
  3. Launch n workers to perform the scan step
  4. Modify the merge step to wait for all scans to complete

Solution Walkthrough

e6-fan-out-fan-in/main.go e7-fan-out-fan-in-workers/main.go

Clone this wiki locally