justforfunc #9: The Context Package
Why Stack Overflow recommends it
Stack Overflow users link it when explaining how Go servers detect client-aborted requests and cancellations through r.Context() and ctx.Done().
Recommended by 1 people across 1 different months, 2017 to 2019.
What it is
Francesc Campoy's justforfunc episode on Go's standard library context package. It walks through using contexts for request-scoped values, cancellation, and deadlines, with a live demo of an HTTP server detecting a client that aborts its request.
What people said
Recommends adrpino · 2017-09 · Stack Overflow · on “Detecting client-aborted requests in AppEngine”
Way late but this may be useful. In Golang you can detect interrupts using the Context package. Here is a useful video of Francesc Campoy explaining it: [this video]
Cites Ralph · 2018-06 · Stack Overflow · on “Shutting down a Go server on browser close”
I wrote a small checkbook ledger in Go as a server that runs in localhost and opens the default browser to a small web app front-end ( [LINK] ). To automatically shut down the server when the browser tab is closed, I have the browser call a "heartbeat" URL every few seconds. If that heartbeat does not arrive, the server uses (*Server) Shutdown [LINK] to stop running. Is there any way to do the same thing using contexts ( [LINK] )? It is my understanding from watching this [this video] episode of JustForFunc that a context passed down to a handler will be notified if the client cancels a request.
Cites Jonah · 2019-10 · Stack Overflow · on “Carrying deadlines and cancelation signals across processes”
In this video [this video] (linked at relevant timestamp), he creates an http server which gets `r.Context()` from the request, and then `select`s on `ctx.Done()`. He runs the server in one terminal, a client in another, and the `ctrl-C`'s the client before the request finishes. The server detects this even in `ctx.Done()`. How is this happening?