Close outbound output stream properly. fix #1082

This commit is contained in:
Darien Raymond 2018-05-11 16:36:20 +02:00
parent 0301222e22
commit 8eb84a2025
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 31 additions and 4 deletions

View File

@ -0,0 +1,23 @@
package functions
import "v2ray.com/core/common"
// Task is a function that may return an error.
type Task func() error
// CloseOnSuccess returns a Task to run a follow task if pre-condition passes, otherwise the error in pre-condition is returned.
func CloseOnSuccess(pre func() error, followup Task) Task {
return func() error {
if err := pre(); err != nil {
return err
}
return followup()
}
}
// Close returns a Task to close the object.
func Close(obj interface{}) Task {
return func() error {
return common.Close(obj)
}
}

View File

@ -10,6 +10,7 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/functions"
"v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/common/signal"
@ -135,7 +136,7 @@ func (h *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia
return nil
}
if err := signal.ExecuteParallel(ctx, requestDone, responseDone); err != nil {
if err := signal.ExecuteParallel(ctx, requestDone, functions.CloseOnSuccess(responseDone, functions.Close(output))); err != nil {
return newError("connection ends").Base(err)
}

View File

@ -6,6 +6,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/functions"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
@ -157,7 +158,7 @@ func (c *Client) Process(ctx context.Context, link *core.Link, dialer proxy.Dial
return nil
}
if err := signal.ExecuteParallel(ctx, requestDone, responseDone); err != nil {
if err := signal.ExecuteParallel(ctx, requestDone, functions.CloseOnSuccess(responseDone, functions.Close(link.Writer))); err != nil {
return newError("connection ends").Base(err)
}

View File

@ -7,6 +7,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/functions"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
@ -129,7 +130,7 @@ func (c *Client) Process(ctx context.Context, link *core.Link, dialer proxy.Dial
}
}
if err := signal.ExecuteParallel(ctx, requestFunc, responseFunc); err != nil {
if err := signal.ExecuteParallel(ctx, requestFunc, functions.CloseOnSuccess(responseFunc, functions.Close(link.Writer))); err != nil {
return newError("connection ends").Base(err)
}

View File

@ -11,6 +11,7 @@ import (
"v2ray.com/core"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/functions"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
@ -159,7 +160,7 @@ func (v *Handler) Process(ctx context.Context, link *core.Link, dialer proxy.Dia
return buf.Copy(bodyReader, output, buf.UpdateActivity(timer))
}
if err := signal.ExecuteParallel(ctx, requestDone, responseDone); err != nil {
if err := signal.ExecuteParallel(ctx, requestDone, functions.CloseOnSuccess(responseDone, functions.Close(output))); err != nil {
return newError("connection ends").Base(err)
}