6 Concurrent API Design
This chapter covers
- Designing, implementing, testing, and optimizing a concurrent HTTP client.
- Learning about the http and httptest packages.
- Using the context and signal packages for cancellation.
- Implementing Rob Pike's Self-Referential Option Functions pattern.
Between 1989 and 1991, Tim Berners-Lee and his team at CERN were working on a protocol called HTTP that would be the enabler of the World Wide Web we have today. The protocol was based on exchanging plain text messages between a server and client, where the client sends a request with a simple text message, and the server returns a response body.
The last chapter taught you to create a command-line tool called hit that parses and validates command-line flags. This chapter will teach you to write an idiomatic HTTP client package called the hit library and integrate it into the hit tool. The package will send concurrent requests to a server to squeeze every bit of juice out of it to give general info to users about the server's performance.
Imagine you want to send thousands of requests to a server to analyze its performance using the hit tool. In Figure 6.1, the tool parses and validates the command-line flags and imports the hit library. The library sends concurrent requests, collects results, and returns an aggregated result to the tool. Finally, the tool shows the aggregated result to the user.
Figure 6.1 The hit project's overall architecture.
