From 4a05d131f0413254f84a0f42f7161f6db9ffddac Mon Sep 17 00:00:00 2001 From: Florian Reiterer Date: Tue, 2 Feb 2016 16:25:02 +0100 Subject: [PATCH] Cancel context when HTTP connection is closed --- protoc-gen-grpc-gateway/gengateway/template.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index c7d3e8d91bf..57bd5210ac1 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -245,6 +245,14 @@ func Register{{$svc.GetName}}Handler(ctx context.Context, mux *runtime.ServeMux, {{range $m := $svc.Methods}} {{range $b := $m.Bindings}} mux.Handle({{$b.HTTPMethod | printf "%q"}}, pattern_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(ctx) + closeNotifier, ok := w.(http.CloseNotifier) + if ok { + go func() { + <-closeNotifier.CloseNotify() + cancel() + }() + } resp, err := request_{{$svc.GetName}}_{{$m.GetName}}_{{$b.Index}}(runtime.AnnotateContext(ctx, req), client, req, pathParams) if err != nil { runtime.HTTPError(ctx, w, req, err)