func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) WatchChan {
	ow := opWatch(key, opts...)
	var filters []pb.WatchCreateRequest_FilterType
	if ow.filterPut {
		filters = append(filters, pb.WatchCreateRequest_NOPUT)
	}
	if ow.filterDelete {
		filters = append(filters, pb.WatchCreateRequest_NODELETE)
	}
	wr := &watchRequest{
		ctx:            ctx,
		createdNotify:  ow.createdNotify,
		key:            string(ow.key),
		end:            string(ow.end),
		rev:            ow.rev,
		progressNotify: ow.progressNotify,
		filters:        filters,
		prevKV:         ow.prevKV,
		retc:           make(chan chan WatchResponse, 1),
	}
	ok := false
	ctxKey := fmt.Sprintf("%v", ctx)
	w.mu.Lock()
	if w.streams == nil {
		w.mu.Unlock()
		ch := make(chan WatchResponse)
		close(ch)
		return ch
	}
	wgs := w.streams[ctxKey]
	if wgs == nil {
		wgs = w.newWatcherGrpcStream(ctx)
		w.streams[ctxKey] = wgs
	}
	donec := wgs.donec
	reqc := wgs.reqc
	w.mu.Unlock()
	closeCh := make(chan WatchResponse, 1)
	select {
	case reqc <- wr:
		ok = true
	case <-wr.ctx.Done():
	case <-donec:
		if wgs.closeErr != nil {
			closeCh <- WatchResponse{closeErr: wgs.closeErr}
			break
		}
		return w.Watch(ctx, key, opts...)
	}
	if ok {
		select {
		case ret := <-wr.retc:
			return ret
		case <-ctx.Done():
		case <-donec:
			if wgs.closeErr != nil {
				closeCh <- WatchResponse{closeErr: wgs.closeErr}
				break
			}
			return w.Watch(ctx, key, opts...)
		}
	}
	close(closeCh)
	return closeCh
}
Watch posts a watch request to run() and waits for a new watcher channel
package clientv3
import (
	"fmt"
	"sync"
	"time"
	v3rpc "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
	pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
	mvccpb "github.com/coreos/etcd/mvcc/mvccpb"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)
const (
	EventTypeDelete = mvccpb.DELETE
	EventTypePut    = mvccpb.PUT
	closeSendErrTimeout = 250 * time.Millisecond
)
type Event mvccpb.Event
type WatchChan <-chan WatchResponse
type Watcher interface {
	Watch(ctx context.Context, key string, opts ...OpOption) WatchChan
	Close() error
}
type WatchResponse struct {
	Header pb.ResponseHeader
	Events []*Event
	CompactRevision int64
	Canceled bool
	Created bool
	closeErr error
}
func (e *Event) IsCreate() bool {
	return e.Type == EventTypePut && e.Kv.CreateRevision == e.Kv.ModRevision
}
func (e *Event) IsModify() bool {
	return e.Type == EventTypePut && e.Kv.CreateRevision != e.Kv.ModRevision
}
func (wr *WatchResponse) Err() error {
	switch {
	case wr.closeErr != nil:
		return v3rpc.Error(wr.closeErr)
	case wr.CompactRevision != 0:
		return v3rpc.ErrCompacted
	case wr.Canceled:
		return v3rpc.ErrFutureRev
	}
	return nil
}
func (wr *WatchResponse) IsProgressNotify() bool {
	return len(wr.Events) == 0 && !wr.Canceled && !wr.Created && wr.CompactRevision == 0 && wr.Header.Revision != 0
}
type watcher struct {
	remote pb.WatchClient
	mu sync.RWMutex
	streams map[string]*watchGrpcStream
}
type watchGrpcStream struct {
	owner  *watcher
	remote pb.WatchClient
	ctx context.Context
	ctxKey string
	cancel context.CancelFunc
	substreams map[int64]*watcherStream
	resuming []*watcherStream
	reqc chan *watchRequest
	respc chan *pb.WatchResponse
	donec chan struct{}
	errc chan error
	closingc chan *watcherStream
	wg sync.WaitGroup
	resumec chan struct{}
	closeErr error
}
type watchRequest struct {
	ctx context.Context
	key string
	end string
	rev int64
	createdNotify bool
	progressNotify bool
	filters []pb.WatchCreateRequest_FilterType
	prevKV bool
	retc chan chan WatchResponse
}
type watcherStream struct {
	initReq watchRequest
	outc chan WatchResponse
	recvc chan *WatchResponse
	donec chan struct{}
	closing bool
	id int64
	buf []*WatchResponse
}
func NewWatcher(c *Client) Watcher {
	return NewWatchFromWatchClient(pb.NewWatchClient(c.conn))
}
func NewWatchFromWatchClient(wc pb.WatchClient) Watcher {
	return &watcher{
		remote:  wc,
		streams: make(map[string]*watchGrpcStream),
	}
}
var valCtxCh = make(chan struct{})
var zeroTime = time.Unix(0, 0)
type valCtx struct{ context.Context }
func (vc *valCtx) Deadline() (time.Time, bool) { return zeroTime, false }
func (vc *valCtx) Done() <-chan struct{}       { return valCtxCh }
func (vc *valCtx) Err() error                  { return nil }
func (w *watcher) newWatcherGrpcStream(inctx context.Context) *watchGrpcStream {
	ctx, cancel := context.WithCancel(&valCtx{inctx})
	wgs := &watchGrpcStream{
		owner:      w,
		remote:     w.remote,
		ctx:        ctx,
		ctxKey:     fmt.Sprintf("%v", inctx),
		cancel:     cancel,
		substreams: make(map[int64]*watcherStream),
		respc:    make(chan *pb.WatchResponse),
		reqc:     make(chan *watchRequest),
		donec:    make(chan struct{}),
		errc:     make(chan error, 1),
		closingc: make(chan *watcherStream),
		resumec:  make(chan struct{}),
	}
	go wgs.run()
	return wgs
}