github.com/imannamdari/v2ray-core/v5@v5.0.5/common/interfaces.go (about) 1 package common 2 3 import "github.com/imannamdari/v2ray-core/v5/common/errors" 4 5 // Closable is the interface for objects that can release its resources. 6 // 7 // v2ray:api:beta 8 type Closable interface { 9 // Close release all resources used by this object, including goroutines. 10 Close() error 11 } 12 13 // Interruptible is an interface for objects that can be stopped before its completion. 14 // 15 // v2ray:api:beta 16 type Interruptible interface { 17 Interrupt() 18 } 19 20 // Close closes the obj if it is a Closable. 21 // 22 // v2ray:api:beta 23 func Close(obj interface{}) error { 24 if c, ok := obj.(Closable); ok { 25 return c.Close() 26 } 27 return nil 28 } 29 30 // Interrupt calls Interrupt() if object implements Interruptible interface, or Close() if the object implements Closable interface. 31 // 32 // v2ray:api:beta 33 func Interrupt(obj interface{}) error { 34 if c, ok := obj.(Interruptible); ok { 35 c.Interrupt() 36 return nil 37 } 38 return Close(obj) 39 } 40 41 // Runnable is the interface for objects that can start to work and stop on demand. 42 type Runnable interface { 43 // Start starts the runnable object. Upon the method returning nil, the object begins to function properly. 44 Start() error 45 Closable 46 } 47 48 // HasType is the interface for objects that knows its type. 49 type HasType interface { 50 // Type returns the type of the object. 51 // Usually it returns (*Type)(nil) of the object. 52 Type() interface{} 53 } 54 55 // ChainedClosable is a Closable that consists of multiple Closable objects. 56 type ChainedClosable []Closable 57 58 // Close implements Closable. 59 func (cc ChainedClosable) Close() error { 60 var errs []error 61 for _, c := range cc { 62 if err := c.Close(); err != nil { 63 errs = append(errs, err) 64 } 65 } 66 return errors.Combine(errs...) 67 }