|
@@ -1,17 +1,25 @@
|
|
|
package common
|
|
package common
|
|
|
|
|
|
|
|
|
|
+import "v2ray.com/core/common/errors"
|
|
|
|
|
+
|
|
|
// Closable is the interface for objects that can release its resources.
|
|
// Closable is the interface for objects that can release its resources.
|
|
|
|
|
+//
|
|
|
|
|
+// v2ray:api:beta
|
|
|
type Closable interface {
|
|
type Closable interface {
|
|
|
// Close release all resources used by this object, including goroutines.
|
|
// Close release all resources used by this object, including goroutines.
|
|
|
Close() error
|
|
Close() error
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Interruptible is an interface for objects that can be stopped before its completion.
|
|
// Interruptible is an interface for objects that can be stopped before its completion.
|
|
|
|
|
+//
|
|
|
|
|
+// v2ray:api:beta
|
|
|
type Interruptible interface {
|
|
type Interruptible interface {
|
|
|
Interrupt()
|
|
Interrupt()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Close closes the obj if it is a Closable.
|
|
// Close closes the obj if it is a Closable.
|
|
|
|
|
+//
|
|
|
|
|
+// v2ray:api:beta
|
|
|
func Close(obj interface{}) error {
|
|
func Close(obj interface{}) error {
|
|
|
if c, ok := obj.(Closable); ok {
|
|
if c, ok := obj.(Closable); ok {
|
|
|
return c.Close()
|
|
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.
|
|
// Interrupt calls Interrupt() if object implements Interruptible interface, or Close() if the object implements Closable interface.
|
|
|
|
|
+//
|
|
|
|
|
+// v2ray:api:beta
|
|
|
func Interrupt(obj interface{}) error {
|
|
func Interrupt(obj interface{}) error {
|
|
|
if c, ok := obj.(Interruptible); ok {
|
|
if c, ok := obj.(Interruptible); ok {
|
|
|
c.Interrupt()
|
|
c.Interrupt()
|
|
@@ -43,15 +53,16 @@ type HasType interface {
|
|
|
Type() interface{}
|
|
Type() interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// ChainedClosable is a Closable that consists of multiple Closable objects.
|
|
|
type ChainedClosable []Closable
|
|
type ChainedClosable []Closable
|
|
|
|
|
|
|
|
-func NewChainedClosable(c ...Closable) ChainedClosable {
|
|
|
|
|
- return ChainedClosable(c)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
|
|
+// Close implements Closable.
|
|
|
func (cc ChainedClosable) Close() error {
|
|
func (cc ChainedClosable) Close() error {
|
|
|
|
|
+ var errs []error
|
|
|
for _, c := range cc {
|
|
for _, c := range cc {
|
|
|
- c.Close()
|
|
|
|
|
|
|
+ if err := c.Close(); err != nil {
|
|
|
|
|
+ errs = append(errs, err)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- return nil
|
|
|
|
|
|
|
+ return errors.Combine(errs...)
|
|
|
}
|
|
}
|