interfaces.go 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package common
  2. // Closable is the interface for objects that can release its resources.
  3. type Closable interface {
  4. // Close release all resources used by this object, including goroutines.
  5. Close() error
  6. }
  7. // Close closes the obj if it is a Closable.
  8. func Close(obj interface{}) error {
  9. if c, ok := obj.(Closable); ok {
  10. return c.Close()
  11. }
  12. return nil
  13. }
  14. // Runnable is the interface for objects that can start to work and stop on demand.
  15. type Runnable interface {
  16. // Start starts the runnable object. Upon the method returning nil, the object begins to function properly.
  17. Start() error
  18. Closable
  19. }
  20. // HasType is the interface for objects that knows its type.
  21. type HasType interface {
  22. // Type returns the type of the object.
  23. Type() interface{}
  24. }
  25. type ChainedClosable []Closable
  26. func NewChainedClosable(c ...Closable) ChainedClosable {
  27. return ChainedClosable(c)
  28. }
  29. func (cc ChainedClosable) Close() error {
  30. for _, c := range cc {
  31. c.Close()
  32. }
  33. return nil
  34. }