Skip to content

Commit 44c365c

Browse files
zzy96fjl
authored andcommitted
rpc: reset writeConn when conn is closed on readErr (#20414)
This change makes the client attempt to reconnect when a write fails. We already had reconnect support, but the reconnect would previously happen on the next call after an error. Being more eager leads to a smoother experience overall.
1 parent 7b68975 commit 44c365c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

rpc/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func (c *Client) newMessage(method string, paramsIn ...interface{}) (*jsonrpcMes
465465
func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error {
466466
select {
467467
case c.reqInit <- op:
468-
err := c.write(ctx, msg)
468+
err := c.write(ctx, msg, false)
469469
c.reqSent <- err
470470
return err
471471
case <-ctx.Done():
@@ -477,7 +477,7 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
477477
}
478478
}
479479

480-
func (c *Client) write(ctx context.Context, msg interface{}) error {
480+
func (c *Client) write(ctx context.Context, msg interface{}, retry bool) error {
481481
// The previous write failed. Try to establish a new connection.
482482
if c.writeConn == nil {
483483
if err := c.reconnect(ctx); err != nil {
@@ -487,6 +487,9 @@ func (c *Client) write(ctx context.Context, msg interface{}) error {
487487
err := c.writeConn.writeJSON(ctx, msg)
488488
if err != nil {
489489
c.writeConn = nil
490+
if !retry {
491+
return c.write(ctx, msg, true)
492+
}
490493
}
491494
return err
492495
}

0 commit comments

Comments
 (0)