func (wh *watcherHub) watch(key string, recursive, stream bool, index, storeIndex uint64) (Watcher, *etcdErr.Error) {
	reportWatchRequest()
	event, err := wh.EventHistory.scan(key, recursive, index)
	if err != nil {
		err.Index = storeIndex
		return nil, err
	}
	w := &watcher{
		eventChan:  make(chan *Event, 100),  
		recursive:  recursive,
		stream:     stream,
		sinceIndex: index,
		startIndex: storeIndex,
		hub:        wh,
	}
	wh.mutex.Lock()
	defer wh.mutex.Unlock()
	if event != nil {
		ne := event.Clone()
		ne.EtcdIndex = storeIndex
		w.eventChan <- ne
		return w, nil
	}
	l, ok := wh.watchers[key]
	var elem *list.Element
	if ok {  
		elem = l.PushBack(w)
	} else {  
		l = list.New()
		elem = l.PushBack(w)
		wh.watchers[key] = l
	}
	w.remove = func() {
		if w.removed {  
			return
		}
		w.removed = true
		l.Remove(elem)
		atomic.AddInt64(&wh.count, -1)
		reportWatcherRemoved()
		if l.Len() == 0 {
			delete(wh.watchers, key)
		}
	}
	atomic.AddInt64(&wh.count, 1)
	reportWatcherAdded()
	return w, nil
}
Watch function returns a Watcher.
If recursive is true, the first change after index under key will be sent to the event channel of the watcher.
If recursive is false, the first change after index at key will be sent to the event channel of the watcher.
If index is zero, watch will start from the current index + 1.
package store
import (
	"container/list"
	"path"
	"strings"
	"sync"
	"sync/atomic"
	etcdErr "github.com/coreos/etcd/error"
)
type watcherHub struct {
	count int64  
	mutex        sync.Mutex
	watchers     map[string]*list.List
	EventHistory *EventHistory
}
func newWatchHub(capacity int) *watcherHub {
	return &watcherHub{
		watchers:     make(map[string]*list.List),
		EventHistory: newEventHistory(capacity),
	}
}