cancel contexts when client disconnects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jul 29, 2015 at 11:00 UTC
b10e26a4295c1ca78ffaf5d9edd11f82c792623f
1 file changed
+12
-2
commands/http/handler.go
+12
-2
@@ -10,6 +10,7 @@ import (
10
"strings"
11
12
cors "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
13
+ context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
14
15
cmds "github.com/ipfs/go-ipfs/commands"
16
u "github.com/ipfs/go-ipfs/util"
@@ -150,7 +151,11 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
151
152
//ps: take note of the name clash - commands.Context != context.Context
153
req.SetInvocContext(i.ctx)
153
- err = req.SetRootContext(node.Context())
154
+
155
+ ctx, cancel := context.WithCancel(node.Context())
156
+ defer cancel()
157
+
158
+ err = req.SetRootContext(ctx)
159
if err != nil {
160
http.Error(w, err.Error(), http.StatusInternalServerError)
161
return
@@ -246,7 +251,12 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
251
}
252
253
if err := writeResponse(status, w, out); err != nil {
249
- log.Error("error while writing stream", err)
254
+ if strings.Contains(err.Error(), "broken pipe") {
255
+ log.Info("client disconnect while writing stream ", err)
256
+ return
257
+ }
258
+
259
+ log.Error("error while writing stream ", err)
260
}
261
}
262