Code with Go
Ask or search…
K
Links

Context

This program is a simple HTTP server written in Go that defines an HTTP handler function called hello. This handler uses the request's context to implement a timeout for processing the request.
Here's a breakdown of the code:
package main
import (
"fmt"
"net/http"
"time"
)
// hello is an HTTP handler function that responds with "hello\n".
func hello(w http.ResponseWriter, req *http.Request) {
// Get the request context.
ctx := req.Context()
fmt.Println("server: hello handler started")
// Defer a statement to print a message when the handler ends.
defer fmt.Println("server: hello handler ended")
// Use a select statement to implement a timeout using time.After.
select {
case <-time.After(10 * time.Second):
// Respond with "hello\n" if the operation completes within 10 seconds.
fmt.Fprintf(w, "hello\n")
case <-ctx.Done():
// If the context is canceled (due to a timeout or cancellation),
// handle the error and respond with an internal server error.
err := ctx.Err()
fmt.Println("server:", err)
internalError := http.StatusInternalServerError
http.Error(w, err.Error(), internalError)
}
}
func main() {
// Register the /hello path with the hello handler.
http.HandleFunc("/hello", hello)
// Start the HTTP server on port 8090.
http.ListenAndServe(":8090", nil)
}
Explanation:
  1. 1.
    hello is an HTTP handler function that uses the request's context (ctx). It prints messages when the handler starts and ends.
  2. 2.
    Inside the select statement, there are two cases:
    • If the operation (responding with "hello\n") completes within 10 seconds, the response is sent.
    • If the context is canceled (due to a timeout or cancellation), an error message is printed, and an internal server error response is sent.
  3. 3.
    In the main function:
    • The /hello path is registered with the hello handler using http.HandleFunc.
    • The server is started using http.ListenAndServe on port 8090.
This program demonstrates a simple way to handle timeouts in an HTTP server using the request context.