mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 17:36:38 +08:00
cancel contexts when client disconnects
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
cors "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/rs/cors"
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
u "github.com/ipfs/go-ipfs/util"
|
||||
@ -150,7 +151,11 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
//ps: take note of the name clash - commands.Context != context.Context
|
||||
req.SetInvocContext(i.ctx)
|
||||
err = req.SetRootContext(node.Context())
|
||||
|
||||
ctx, cancel := context.WithCancel(node.Context())
|
||||
defer cancel()
|
||||
|
||||
err = req.SetRootContext(ctx)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@ -246,6 +251,11 @@ func sendResponse(w http.ResponseWriter, r *http.Request, res cmds.Response, req
|
||||
}
|
||||
|
||||
if err := writeResponse(status, w, out); err != nil {
|
||||
if strings.Contains(err.Error(), "broken pipe") {
|
||||
log.Info("client disconnect while writing stream ", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Error("error while writing stream ", err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user