In this lesson, you will learn how Mizu manages the lifecycle of an HTTP request. Every request in Mizu has its own context, which lets you handle timeouts, cancellations, and background operations safely. This helps you write clean, responsive handlers that stop when the client disconnects or when the server shuts down.Documentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
Code
Create a file namedmain.go and add this code:
Run
Run the server with:curl:
- http://localhost:8080/slow Wait a few seconds to see the message āFinished after 3 secondsā. If you stop loading early, the handler detects it and stops.
- http://localhost:8080/timed Returns quickly with āCompleted within timeoutā. If you increase the delay past 2 seconds, it will show āOperation timed outā.
How it works
Each request in Go includes acontext.Context that ends when the request finishes. Mizu provides c.Context() to access it easily. You can use it to cancel work early, set timeouts, or handle cleanup when the request is done.
In the /slow example, the handler simulates a long operation. If the user closes the browser or the server shuts down, the context is canceled and your code exits early.
In /timed, you create your own timeout with context.WithTimeout, giving full control over how long the handler should run.
This approach is important for tasks like database queries, API calls, or background jobs where you should stop safely when the request ends.
Try something new
Change the values insidetime.After() or context.WithTimeout() to see how timeouts behave. You can also print ctx.Err() to understand why the context ended.
Now you know how Mizuās context system helps manage request lifecycles in a simple, predictable way.
Next, continue to Query, Form, and Path to learn how to read data from incoming requests.