Commit 2cbdd1da authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 7461a018
...@@ -83,16 +83,21 @@ func LClose(ctx context.Context, c io.Closer) { ...@@ -83,16 +83,21 @@ func LClose(ctx context.Context, c io.Closer) {
// WithCloseOnErrCancel closes c on ctx cancel while f is run, or if f returns with an error. // WithCloseOnErrCancel closes c on ctx cancel while f is run, or if f returns with an error.
// //
// It is usually handy to propagate cancellation to interrupt IO. // It is usually handy to propagate cancellation to interrupt IO.
//
// Returned error is what f returned, or ctx.Err() if f retrned nil despite that
// ctx was canceled and c closed.
func WithCloseOnErrCancel(ctx context.Context, c io.Closer, f func() error) (err error) { func WithCloseOnErrCancel(ctx context.Context, c io.Closer, f func() error) (err error) {
closed := false closed := false
fdone := make(chan error) fdone := make(chan error)
defer func() { defer func() {
errf, ok := <-fdone // wait for f to complete XXX return f's error errf, ok := <-fdone // wait for f to complete
if ok { if ok {
// it was ctx cancel and `return ctx.Err()` vvv // it was ctx cancel and `return ctx.Err()` vvv
// -> change return to be what f returned // -> change return to be what f returned if !nil
if errf != nil {
err = errf err = errf
} }
}
if err != nil { if err != nil {
if !closed { if !closed {
...@@ -124,17 +129,15 @@ func WithCloseOnErrCancel(ctx context.Context, c io.Closer, f func() error) (err ...@@ -124,17 +129,15 @@ func WithCloseOnErrCancel(ctx context.Context, c io.Closer, f func() error) (err
// //
// It is usually handy to propagate cancellation to interrupt IO. // It is usually handy to propagate cancellation to interrupt IO.
func WithCloseOnRetCancel(ctx context.Context, c io.Closer, f func() error) error { func WithCloseOnRetCancel(ctx context.Context, c io.Closer, f func() error) error {
var errf error
err := WithCloseOnErrCancel(ctx, c, func() error { err := WithCloseOnErrCancel(ctx, c, func() error {
errf = f() e := f()
e := errf
if e == nil { if e == nil {
e = retOK // force c close e = retOK // force c close
} }
return e return e
}) })
if err == retOK { if err == retOK {
err = errf err = nil
} }
return err return err
} }
......
...@@ -135,6 +135,9 @@ func _handshakeClient(ctx context.Context, conn net.Conn, version uint32, encPre ...@@ -135,6 +135,9 @@ func _handshakeClient(ctx context.Context, conn net.Conn, version uint32, encPre
return nil return nil
}) })
if err != nil { if err != nil {
if ctx.Err() != nil {
err = ctx.Err() // error was due to ctx cancel
}
return 0, nil, err return 0, nil, err
} }
...@@ -179,6 +182,9 @@ func _handshakeServer(ctx context.Context, conn net.Conn, version uint32) (enc p ...@@ -179,6 +182,9 @@ func _handshakeServer(ctx context.Context, conn net.Conn, version uint32) (enc p
return nil return nil
}) })
if err != nil { if err != nil {
if ctx.Err() != nil {
err = ctx.Err() // error was due to ctx cancel
}
return 0, nil, err return 0, nil, err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment