Go concurrency and HTTP services
You run work in parallel with goroutines and channels and write an HTTP service with safe defaults that shuts down cleanly.
Level 201 · 19 lessons · 15 practice labs
Lessons
Concurrency
- Goroutines: lightweight concurrent tasks: You start tasks concurrently with the go keyword and see why the main program must wait.
- Channels: communication between goroutines: You send and receive over channels, close them and tell buffered from unbuffered channels.
- Buffered channels, closing and reading with range
- Multiple channels and timeouts with select: You wait on several channels at once, add timeouts and prevent work from hanging forever.
- sync.WaitGroup and Mutex: protecting shared data: You learn to wait for goroutines to finish and to protect a shared variable with a lock.
- Worker pool and pipeline patterns: You spread many jobs across a limited number of workers and build a staged data pipeline.
- Cancellation and deadlines with context: You cancel long-running work with context, set deadlines and prevent leaked goroutines.
- Finding race conditions: go test -race: You detect and fix bugs caused by simultaneous access to the same data using the race detector.
- Short task: a tool that hashes files in parallel
HTTP services
- Your first server with net/http: You start an HTTP server with the standard library, write a handler and set response status codes.
- Routing and a JSON API: You set up endpoints split by path and method and build an API that reads and writes JSON.
- Middleware: logging and recovery: You write middleware applied to every request, log requests and recover from crashing handlers.
- Static files and safe pages with html/template
- The HTTP client and timeouts: You make requests to other services with the required settings such as timeouts and closing the response body.
- Secure HTTP defaults: request size, timeouts, headers: You harden a server against abuse and overload by adding read timeouts, body size limits and security headers.
- Testing handlers with httptest
- Graceful shutdown: finishing in-flight requests: You catch the shutdown signal, finish requests in progress and stop the server cleanly.
- Short task: a small service with health and version endpoints
Capstone
- Capstone: a site-checking service with a worker pool: You write a service that checks your own sites in parallel, serves the results over HTTP and shuts down cleanly.
Practice labs
- Spreading jobs over goroutines and waiting: Compute file sizes in separate goroutines, wait with a WaitGroup and gather results over a channel.
- Closing a channel and consuming with range: Fix a program that hangs because the producer never closes the channel and test every item is processed exactly once.
- A timeout and fallback with select: Write a function that returns the first reply from two sources, falls back to a default when time runs out and leaves no goroutine behind.
- Guarding a visit counter with a Mutex: Protect a map-based counter that undercounts under concurrent increments with sync.Mutex and show go test -race comes out clean.
- Repairing a cache bug found by the race detector: Read the race report for a cache that reads without locking and fix the read and write paths with an RWMutex.
- Closing a check-then-act race: Bring code that releases the lock between the stock check and the decrement and goes negative into one critical section so stock never goes below zero over a thousand concurrent orders.
- A job queue with a fixed number of workers: Process fake thumbnail jobs with a four-worker pool and return results in input order.
- Cancelling a pipeline with context: Add a context to a three-stage pipeline so every stage stops on the first error and no goroutine is left blocked on a channel.
- Hashing files in parallel under a deadline: Compute SHA-256 hashes of a directory's files with a worker pool and return partial results plus the list of unfinished files when the deadline passes.
- Writing health and version endpoints: Write two endpoints with net/http that return status and version as JSON and answer 405 for the wrong method.
- Writing logging and recovery middleware: Write a middleware chain that logs request duration and status code and returns 500 while keeping the server up when a handler panics.
- Closing unescaped output in a template: Move a page that prints user comments with text/template to html/template and prove with httptest that a comment containing script shows as harmless text.
- Adding a timeout to an HTTP client: Add a timeout to a checker that waits forever with the default client and close the response body on every path.
- Putting size and time limits on a server: Limit the request body with MaxBytesReader, set read and write timeouts, add security headers and verify with httptest.
- Shutting down while finishing open requests: Add a shutdown that refuses new connections on a signal and finishes in-flight requests within a deadline, showing by test that no response is cut off.
About this course
You run work in parallel with goroutines and channels and write an HTTP service with safe defaults that shuts down cleanly.
What you will learn
- Concurrency
- HTTP services
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.