Context
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)
}Last updated