6 Synchronous APIs for concurrency

 

This chapter covers

  • Designing synchronous APIs that treat concurrency as an implementation detail.
  • Standardizing the processing of sequences of values using push iterators.
  • Structuring concurrent processing with the pipeline pattern.

Having finished the HIT tool, we can now focus on the HIT client within the hit package. It will allow our tool to send concurrent HTTP requests to measure HTTP server performance.

As the first chapter hinted, channels use a message-passing approach that simplifies concurrent programming by making the data flow between goroutines explicit. This chapter dives into the concurrent pipeline pattern to demonstrate this approach in practice.

The hit package will show us how to structure a straightforward package API that appears synchronous from the outside but runs concurrently inside. We'll begin with a package that works sequentially and then refactor it to work concurrently without changing its exported API. By the chapter's end, you'll learn how to effectively structure concurrent code in Go.

NOTE

HTTP provides a realistic context for concurrency patterns, helping us see how and where to use them. These patterns apply broadly, not just to HTTP. We'll skip HTTP details until the next chapter to keep the initial focus purely on concurrency.

6.1 Overview

6.1.1 Package hit

6.1.2 Package API

6.1.3 Directory structure

6.2 Foundations

6.2.1 Result

6.2.2 Send

6.3 Iterators

6.3.1 Push iterators

6.3.2 Producing values

6.3.3 Consuming values

6.3.4 Testing

6.4 Options

6.4.1 Providing options

6.4.2 Accepting options

6.5 Integration

6.5.1 Printing a summary

6.5.2 Integration

6.5.3 Demonstration

6.6 Concurrent pipeline pattern

6.6.1 Benefits of concurrent pipelines

6.6.2 Designing a concurrent pipeline

6.7 Producer stage

6.7.1 Implementation

6.7.2 Integration

6.8 Throttler stage

6.8.1 Implementation

6.8.2 Integration

6.9 Dispatcher stage

6.9.1 Implementation

6.9.2 Integration

6.9.3 Demonstration

6.9.4 Outro

6.10 Exercises

6.11 Summary