Request Cancellation

Every week in my Tuesday Lunch & Learn livestream, we choose a useful software development skill, define a challenge related to that skill, and solve the challenge live. This week, we’re cancelling network requests.

In past episodes, we built a small command-line application that uses a microservice to encode text. We put a lot of effort into error handling, including making sure that the client times out if the microservice doesn’t respond.

There’s a catch, though: although the client times out and displays an error, it doesn’t actually cancel the request, so the application doesn’t exit. In today’s episode, we fix that problem.

This turns out to be a design problem. Although cancelling requests is easy to do in Node.js—you just call request.destroy() on the Node request object—the challenge is doing it cleanly. How can we make our low-level HTTP code cancel a request without exposing the internal details? How can we make our API convenient to use and understandable? How can we make it testable, so we can test-drive our application-layer code?

To follow along, download the code from GitHub and check out the 2020-09-22 tag. To see the final result, check out the 2020-09-22-end tag or view it on GitHub.

Visit the Lunch & Learn archive for more.

If you liked this entry, check out my best writing and presentations, and consider subscribing to updates by email or RSS.