Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 228766e

Browse files
committedJul 29, 2015
cancel contexts when client disconnects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 33a0095 commit 228766e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed
 

‎commands/http/handler.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"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"
1314

1415
cmds "github.com/ipfs/go-ipfs/commands"
1516
u "github.com/ipfs/go-ipfs/util"
@@ -119,7 +120,11 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
119120

120121
//ps: take note of the name clash - commands.Context != context.Context
121122
req.SetInvocContext(i.ctx)
122-
err = req.SetRootContext(node.Context())
123+
124+
ctx, cancel := context.WithCancel(node.Context())
125+
defer cancel()
126+
127+
err = req.SetRootContext(ctx)
123128
if err != nil {
124129
http.Error(w, err.Error(), http.StatusInternalServerError)
125130
return
@@ -204,7 +209,12 @@ func sendResponse(w http.ResponseWriter, req cmds.Request, res cmds.Response) {
204209
h.Set(transferEncodingHeader, "chunked")
205210

206211
if err := writeResponse(status, w, out); err != nil {
207-
log.Error("error while writing stream", err)
212+
if strings.Contains(err.Error(), "broken pipe") {
213+
log.Info("client disconnect while writing stream ", err)
214+
return
215+
}
216+
217+
log.Error("error while writing stream ", err)
208218
}
209219
}
210220

0 commit comments

Comments
 (0)
Please sign in to comment.