Skip to content

Commit

Permalink
Check and log error when write net failed
Browse files Browse the repository at this point in the history
  • Loading branch information
shafreeck committed Nov 19, 2018
1 parent c3b75af commit 94d52e5
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion client.go
Expand Up @@ -29,6 +29,21 @@ func newClient(cliCtx *context.ClientContext, s *Server, exec *command.Executor)
}
}

// Write to conn and log error if needed
func (c *client) Write(p []byte) (int, error) {
n, err := c.conn.Write(p)
if err != nil {
zap.L().Error("write net failed", zap.String("addr", c.cliCtx.RemoteAddr),
zap.Int64("clientid", c.cliCtx.ID),
zap.String("namespace", c.cliCtx.Namespace),
zap.Bool("multi", c.cliCtx.Multi),
zap.Bool("watching", c.cliCtx.Txn != nil),
zap.String("command", c.cliCtx.LastCmd))
c.conn.Close()
}
return n, err
}

func (c *client) serve(conn net.Conn) error {
c.conn = conn
c.r = bufio.NewReader(conn)
Expand Down Expand Up @@ -78,7 +93,7 @@ func (c *client) serve(conn net.Conn) error {
Name: cmd[0],
Args: cmd[1:],
In: c.r,
Out: c.conn,
Out: c,
TraceID: GenerateTraceID(),
}
innerCtx, cancel := context.WithCancel(rootCtx)
Expand Down

0 comments on commit 94d52e5

Please sign in to comment.