reset writeConn in write instead of dispatch

This commit is contained in:
zzy96 2019-12-02 20:36:44 +08:00
parent f0febb61fb
commit 13f060698c

View file

@ -465,7 +465,7 @@ func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMes
func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error { func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error {
select { select {
case c.reqInit <- op: case c.reqInit <- op:
err := c.write(ctx, msg) err := c.write(ctx, msg, false)
c.reqSent <- err c.reqSent <- err
return err return err
case <-ctx.Done(): case <-ctx.Done():
@ -477,7 +477,7 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
} }
} }
func (c *Client) write(ctx context.Context, msg interface{}) error { func (c *Client) write(ctx context.Context, msg interface{}, retry bool) error {
// The previous write failed. Try to establish a new connection. // The previous write failed. Try to establish a new connection.
if c.writeConn == nil { if c.writeConn == nil {
if err := c.reconnect(ctx); err != nil { if err := c.reconnect(ctx); err != nil {
@ -487,6 +487,9 @@ func (c *Client) write(ctx context.Context, msg interface{}) error {
err := c.writeConn.writeJSON(ctx, msg) err := c.writeConn.writeJSON(ctx, msg)
if err != nil { if err != nil {
c.writeConn = nil c.writeConn = nil
if !retry {
return c.write(ctx, msg, true)
}
} }
return err return err
} }
@ -555,7 +558,6 @@ func (c *Client) dispatch(codec ServerCodec) {
conn.handler.log.Debug("RPC connection read error", "err", err) conn.handler.log.Debug("RPC connection read error", "err", err)
conn.close(err, lastOp) conn.close(err, lastOp)
reading = false reading = false
c.writeConn = nil
// Reconnect: // Reconnect:
case newcodec := <-c.reconnected: case newcodec := <-c.reconnected: