I am using CloseNotifier in my application, in a code that looks like this
func Handler(res http.ResonseWriter, req *http.Request) {
notify := res.(CloseNotifier).CloseNotify()
someLogic();
select {
case <-notify:
someCleanup()
return;
default:
}
someOtherLogic();
}
I have noticed CloseNotifier is now deprecated. From source code:
// Deprecated: the CloseNotifier interface predates Go's context package.
// New code should use Request.Context instead.
However, I am not sure how to use Request.Context exactly here.
It seems rather simple actually. From this blogpost:
func Handler(res http.ResonseWriter, req *http.Request) {
ctx := req.Context()
someLogic();
select {
case <-ctx.Done():
someCleanup(ctx.Err())
return;
default:
}
someOtherLogic();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With