1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-19 22:15:23 +00:00
v2fly/common/interfaces.go

68 lines
1.6 KiB
Go
Raw Normal View History

package common
import "github.com/v2fly/v2ray-core/v5/common/errors"
2019-01-01 19:16:04 +00:00
2018-02-08 14:39:46 +00:00
// Closable is the interface for objects that can release its resources.
2019-01-01 19:16:04 +00:00
//
// v2ray:api:beta
2018-02-08 14:39:46 +00:00
type Closable interface {
// Close release all resources used by this object, including goroutines.
Close() error
}
2018-12-31 20:25:10 +00:00
// Interruptible is an interface for objects that can be stopped before its completion.
2019-01-01 19:16:04 +00:00
//
// v2ray:api:beta
2018-12-31 20:25:10 +00:00
type Interruptible interface {
Interrupt()
}
2018-02-08 14:39:46 +00:00
// Close closes the obj if it is a Closable.
2019-01-01 19:16:04 +00:00
//
// v2ray:api:beta
2018-02-08 14:39:46 +00:00
func Close(obj interface{}) error {
if c, ok := obj.(Closable); ok {
return c.Close()
}
return nil
}
2018-12-31 20:25:10 +00:00
// Interrupt calls Interrupt() if object implements Interruptible interface, or Close() if the object implements Closable interface.
2019-01-01 19:16:04 +00:00
//
// v2ray:api:beta
2018-12-31 20:25:10 +00:00
func Interrupt(obj interface{}) error {
if c, ok := obj.(Interruptible); ok {
c.Interrupt()
return nil
}
return Close(obj)
}
// Runnable is the interface for objects that can start to work and stop on demand.
type Runnable interface {
// Start starts the runnable object. Upon the method returning nil, the object begins to function properly.
Start() error
2018-02-08 14:39:46 +00:00
Closable
}
2018-02-14 16:35:09 +00:00
// HasType is the interface for objects that knows its type.
type HasType interface {
// Type returns the type of the object.
2018-10-12 21:57:56 +00:00
// Usually it returns (*Type)(nil) of the object.
2018-02-14 16:35:09 +00:00
Type() interface{}
}
2018-03-01 12:16:52 +00:00
2019-01-01 19:16:04 +00:00
// ChainedClosable is a Closable that consists of multiple Closable objects.
2018-03-01 12:16:52 +00:00
type ChainedClosable []Closable
2019-01-01 19:16:04 +00:00
// Close implements Closable.
2018-03-01 12:16:52 +00:00
func (cc ChainedClosable) Close() error {
2019-01-01 19:16:04 +00:00
var errs []error
2018-03-01 12:16:52 +00:00
for _, c := range cc {
2019-01-01 19:16:04 +00:00
if err := c.Close(); err != nil {
errs = append(errs, err)
}
2018-03-01 12:16:52 +00:00
}
2019-01-01 19:16:04 +00:00
return errors.Combine(errs...)
2018-03-01 12:16:52 +00:00
}