api/handler/rpc: not log error on client disconnect (#1482)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-04-04 00:37:18 +03:00 committed by GitHub
parent 7f8b35e295
commit 38aed6f0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -135,27 +135,38 @@ func serveWebsocket(ctx context.Context, w http.ResponseWriter, r *http.Request,
// receive from stream and send to client // receive from stream and send to client
for { for {
// read backend response body select {
buf, err := rsp.Read() case <-ctx.Done():
if err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err)
}
return return
} case <-stream.Context().Done():
return
default:
// read backend response body
buf, err := rsp.Read()
if err != nil {
// wants to avoid import grpc/status.Status
if strings.Contains(err.Error(), "context canceled") {
return
}
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err)
}
return
}
// write the response // write the response
if err := wsutil.WriteServerMessage(rw, op, buf); err != nil { if err := wsutil.WriteServerMessage(rw, op, buf); err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) { if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err) logger.Error(err)
}
return
} }
return if err = rw.Flush(); err != nil {
} if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
if err = rw.Flush(); err != nil { logger.Error(err)
if logger.V(logger.ErrorLevel, logger.DefaultLogger) { }
logger.Error(err) return
} }
return
} }
} }
} }
@ -166,30 +177,40 @@ func writeLoop(rw io.ReadWriter, stream client.Stream) {
defer stream.Close() defer stream.Close()
for { for {
buf, op, err := wsutil.ReadClientData(rw) select {
if err != nil { case <-stream.Context().Done():
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err)
}
return return
}
switch op {
default: default:
// not relevant buf, op, err := wsutil.ReadClientData(rw)
continue if err != nil {
case ws.OpText, ws.OpBinary: wserr := err.(wsutil.ClosedError)
break switch wserr.Code {
} case ws.StatusNormalClosure, ws.StatusNoStatusRcvd:
// send to backend return
// default to trying json default:
// if the extracted payload isn't empty lets use it if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
request := &raw.Frame{Data: buf} logger.Error(err)
}
if err := stream.Send(request); err != nil { return
if logger.V(logger.ErrorLevel, logger.DefaultLogger) { }
logger.Error(err) }
switch op {
default:
// not relevant
continue
case ws.OpText, ws.OpBinary:
break
}
// send to backend
// default to trying json
// if the extracted payload isn't empty lets use it
request := &raw.Frame{Data: buf}
if err := stream.Send(request); err != nil {
if logger.V(logger.ErrorLevel, logger.DefaultLogger) {
logger.Error(err)
}
return
} }
return
} }
} }
} }