diff --git a/common/interfaces.go b/common/interfaces.go index db82112fc..0e569b9fb 100644 --- a/common/interfaces.go +++ b/common/interfaces.go @@ -1,17 +1,25 @@ package common +import "v2ray.com/core/common/errors" + // Closable is the interface for objects that can release its resources. +// +// v2ray:api:beta type Closable interface { // Close release all resources used by this object, including goroutines. Close() error } // Interruptible is an interface for objects that can be stopped before its completion. +// +// v2ray:api:beta type Interruptible interface { Interrupt() } // Close closes the obj if it is a Closable. +// +// v2ray:api:beta func Close(obj interface{}) error { if c, ok := obj.(Closable); ok { return c.Close() @@ -20,6 +28,8 @@ func Close(obj interface{}) error { } // Interrupt calls Interrupt() if object implements Interruptible interface, or Close() if the object implements Closable interface. +// +// v2ray:api:beta func Interrupt(obj interface{}) error { if c, ok := obj.(Interruptible); ok { c.Interrupt() @@ -43,15 +53,16 @@ type HasType interface { Type() interface{} } +// ChainedClosable is a Closable that consists of multiple Closable objects. type ChainedClosable []Closable -func NewChainedClosable(c ...Closable) ChainedClosable { - return ChainedClosable(c) -} - +// Close implements Closable. func (cc ChainedClosable) Close() error { + var errs []error for _, c := range cc { - c.Close() + if err := c.Close(); err != nil { + errs = append(errs, err) + } } - return nil + return errors.Combine(errs...) } diff --git a/transport/internet/http/dialer.go b/transport/internet/http/dialer.go index 81147dae1..130670a37 100644 --- a/transport/internet/http/dialer.go +++ b/transport/internet/http/dialer.go @@ -110,7 +110,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me return net.NewConnection( net.ConnectionOutput(response.Body), net.ConnectionInput(bwriter), - net.ConnectionOnClose(common.NewChainedClosable(breader, bwriter, response.Body)), + net.ConnectionOnClose(common.ChainedClosable{breader, bwriter, response.Body}), ), nil } diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index 665c5f2ca..0c4da195c 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -80,7 +80,7 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) conn := net.NewConnection( net.ConnectionOutput(request.Body), net.ConnectionInput(flushWriter{w: writer, d: done}), - net.ConnectionOnClose(common.NewChainedClosable(done, request.Body)), + net.ConnectionOnClose(common.ChainedClosable{done, request.Body}), net.ConnectionLocalAddr(l.Addr()), net.ConnectionRemoteAddr(remoteAddr), )