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

.

parent 7461a018
......@@ -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.
//
// 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) {
closed := false
fdone := make(chan error)
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 {
// 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
}
}
if err != nil {
if !closed {
......@@ -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.
func WithCloseOnRetCancel(ctx context.Context, c io.Closer, f func() error) error {
var errf error
err := WithCloseOnErrCancel(ctx, c, func() error {
errf = f()
e := errf
e := f()
if e == nil {
e = retOK // force c close
}
return e
})
if err == retOK {
err = errf
err = nil
}
return err
}
......
......@@ -135,6 +135,9 @@ func _handshakeClient(ctx context.Context, conn net.Conn, version uint32, encPre
return nil
})
if err != nil {
if ctx.Err() != nil {
err = ctx.Err() // error was due to ctx cancel
}
return 0, nil, err
}
......@@ -179,6 +182,9 @@ func _handshakeServer(ctx context.Context, conn net.Conn, version uint32) (enc p
return nil
})
if err != nil {
if ctx.Err() != nil {
err = ctx.Err() // error was due to ctx cancel
}
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