| 475 | } |
| 476 | |
| 477 | func (c *Client) write(ctx context.Context, msg interface{}) error { |
| 478 | deadline, ok := ctx.Deadline() |
| 479 | if !ok { |
| 480 | deadline = time.Now().Add(defaultWriteTimeout) |
| 481 | } |
| 482 | // The previous write failed. Try to establish a new connection. |
| 483 | if c.writeConn == nil { |
| 484 | if err := c.reconnect(ctx); err != nil { |
| 485 | return err |
| 486 | } |
| 487 | } |
| 488 | c.writeConn.SetWriteDeadline(deadline) |
| 489 | err := json.NewEncoder(c.writeConn).Encode(msg) |
| 490 | if err != nil { |
| 491 | c.writeConn = nil |
| 492 | } |
| 493 | return err |
| 494 | } |
| 495 | |
| 496 | func (c *Client) reconnect(ctx context.Context) error { |
| 497 | newconn, err := c.connectFunc(ctx) |