Skip to content

Commit

Permalink
Merge PR: support the batch call in a simple way (#957)
Browse files Browse the repository at this point in the history
Co-authored-by: MengXiangJian <[email protected]>
Co-authored-by: Zhong Qiu <[email protected]>
  • Loading branch information
3 people authored Aug 16, 2021
1 parent 9e670c0 commit 1b60d40
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions app/rpc/websockets/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ func (s *Server) readLoop(wsConn *wsConn) {
}

var msg map[string]interface{}
err = json.Unmarshal(mb, &msg)
if err != nil {
s.sendErrResponse(wsConn, "invalid request")
if err = json.Unmarshal(mb, &msg); err != nil {
if err = s.batchCall(mb, wsConn); err != nil {
s.sendErrResponse(wsConn, "invalid request")
}
continue
}

Expand Down Expand Up @@ -289,3 +290,24 @@ func (s *Server) closeWsConnection(subIds map[rpc.ID]struct{}) {
<-s.connPool
s.currentConnNum.Set(float64(len(s.connPool)))
}

func (s *Server) batchCall(mb []byte, wsConn *wsConn) error {
var msgs []interface{}
if err := json.Unmarshal(mb, &msgs); err != nil {
return err
}

for i := 0; i < len(msgs); i++ {
b, err := json.Marshal(msgs[i])
if err != nil {
s.sendErrResponse(wsConn, err.Error())
continue
}

err = s.tcpGetAndSendResponse(wsConn, b)
if err != nil {
s.sendErrResponse(wsConn, err.Error())
}
}
return nil
}

0 comments on commit 1b60d40

Please sign in to comment.