client: Close connection on non-nil error in Dial #282
1 changed files with 12 additions and 7 deletions
|
@ -106,16 +106,21 @@ func (c *Client) Dial(ctx context.Context, prm PrmDial) error {
|
||||||
client.WithContext(ctx),
|
client.WithContext(ctx),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var ctxErr error
|
||||||
|
|
||||||
// return context errors since they signal about dial problem
|
// return context errors since they signal about dial problem
|
||||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||||
return err
|
ctxErr = err
|
||||||
|
} else if st, ok := status.FromError(err); ok && st.Code() == codes.Canceled {
|
||||||
|
ctxErr = context.Canceled
|
||||||
|
} else if ok && st.Code() == codes.DeadlineExceeded {
|
||||||
|
ctxErr = context.DeadlineExceeded
|
||||||
}
|
}
|
||||||
st, ok := status.FromError(err)
|
if ctxErr != nil {
|
||||||
if ok && st.Code() == codes.Canceled {
|
if conn := c.c.Conn(); conn != nil {
|
||||||
return context.Canceled
|
_ = conn.Close()
|
||||||
}
|
}
|
||||||
if ok && st.Code() == codes.DeadlineExceeded {
|
return ctxErr
|
||||||
return context.DeadlineExceeded
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue