2018-01-10 06:22:37 -05:00
|
|
|
package common
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
// Closable is the interface for objects that can release its resources.
|
|
|
|
type Closable interface {
|
|
|
|
// Close release all resources used by this object, including goroutines.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the obj if it is a Closable.
|
|
|
|
func Close(obj interface{}) error {
|
|
|
|
if c, ok := obj.(Closable); ok {
|
|
|
|
return c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
// 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 09:39:46 -05:00
|
|
|
Closable
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
2018-02-14 11:35:09 -05:00
|
|
|
|
|
|
|
// HasType is the interface for objects that knows its type.
|
|
|
|
type HasType interface {
|
|
|
|
// Type returns the type of the object.
|
2018-10-12 17:57:56 -04:00
|
|
|
// Usually it returns (*Type)(nil) of the object.
|
2018-02-14 11:35:09 -05:00
|
|
|
Type() interface{}
|
|
|
|
}
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
type ChainedClosable []Closable
|
|
|
|
|
|
|
|
func NewChainedClosable(c ...Closable) ChainedClosable {
|
|
|
|
return ChainedClosable(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc ChainedClosable) Close() error {
|
|
|
|
for _, c := range cc {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|