这是watcher包中最后一个文件了
streamwatcher.go先看数据结构
type Decoder interface {
Decode() (action EventType, object runtime.Object, err error)
Close()
}
type StreamWatcher struct {
sync.Mutex
source Decoder
result chan Event
stopped bool
}
再看看方法
StreamWatcher 有两 ResultChan()个stop()方法,实现Interface
func (sw *StreamWatcher) ResultChan() <-chan Event {
return sw.result
}
// Stop implements Interface.
func (sw *StreamWatcher) Stop() {
// Call Close() exactly once by locking and setting a flag.
sw.Lock()
defer sw.Unlock()
if !sw.stopped {
sw.stopped = true
sw.source.Close()
}
}
实例化一个StreamWatcher并且有一个goroutine
func NewStreamWatcher(d Decoder) *StreamWatcher {
sw := &StreamWatcher{
source: d,
// It's easy for a consumer to add buffering via an extra
// goroutine