Skip to content

Commit

Permalink
Cancel context when HTTP connection is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Reiterer committed Feb 2, 2016
1 parent 94e1f27 commit 4a05d13
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4a05d13

Please sign in to comment.