watch.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "fmt"
  17. "sync"
  18. "time"
  19. v3rpc "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. mvccpb "github.com/coreos/etcd/mvcc/mvccpb"
  22. "golang.org/x/net/context"
  23. "google.golang.org/grpc"
  24. )
  25. const (
  26. EventTypeDelete = mvccpb.DELETE
  27. EventTypePut = mvccpb.PUT
  28. closeSendErrTimeout = 250 * time.Millisecond
  29. )
  30. type Event mvccpb.Event
  31. type WatchChan <-chan WatchResponse
  32. type Watcher interface {
  33. // Watch watches on a key or prefix. The watched events will be returned
  34. // through the returned channel.
  35. // If the watch is slow or the required rev is compacted, the watch request
  36. // might be canceled from the server-side and the chan will be closed.
  37. // 'opts' can be: 'WithRev' and/or 'WithPrefix'.
  38. Watch(ctx context.Context, key string, opts ...OpOption) WatchChan
  39. // Close closes the watcher and cancels all watch requests.
  40. Close() error
  41. }
  42. type WatchResponse struct {
  43. Header pb.ResponseHeader
  44. Events []*Event
  45. // CompactRevision is the minimum revision the watcher may receive.
  46. CompactRevision int64
  47. // Canceled is used to indicate watch failure.
  48. // If the watch failed and the stream was about to close, before the channel is closed,
  49. // the channel sends a final response that has Canceled set to true with a non-nil Err().
  50. Canceled bool
  51. // created is used to indicate the creation of the watcher.
  52. created bool
  53. closeErr error
  54. }
  55. // IsCreate returns true if the event tells that the key is newly created.
  56. func (e *Event) IsCreate() bool {
  57. return e.Type == EventTypePut && e.Kv.CreateRevision == e.Kv.ModRevision
  58. }
  59. // IsModify returns true if the event tells that a new value is put on existing key.
  60. func (e *Event) IsModify() bool {
  61. return e.Type == EventTypePut && e.Kv.CreateRevision != e.Kv.ModRevision
  62. }
  63. // Err is the error value if this WatchResponse holds an error.
  64. func (wr *WatchResponse) Err() error {
  65. switch {
  66. case wr.closeErr != nil:
  67. return v3rpc.Error(wr.closeErr)
  68. case wr.CompactRevision != 0:
  69. return v3rpc.ErrCompacted
  70. case wr.Canceled:
  71. return v3rpc.ErrFutureRev
  72. }
  73. return nil
  74. }
  75. // IsProgressNotify returns true if the WatchResponse is progress notification.
  76. func (wr *WatchResponse) IsProgressNotify() bool {
  77. return len(wr.Events) == 0 && !wr.Canceled && !wr.created && wr.CompactRevision == 0 && wr.Header.Revision != 0
  78. }
  79. // watcher implements the Watcher interface
  80. type watcher struct {
  81. remote pb.WatchClient
  82. // mu protects the grpc streams map
  83. mu sync.RWMutex
  84. // streams holds all the active grpc streams keyed by ctx value.
  85. streams map[string]*watchGrpcStream
  86. }
  87. // watchGrpcStream tracks all watch resources attached to a single grpc stream.
  88. type watchGrpcStream struct {
  89. owner *watcher
  90. remote pb.WatchClient
  91. // ctx controls internal remote.Watch requests
  92. ctx context.Context
  93. // ctxKey is the key used when looking up this stream's context
  94. ctxKey string
  95. cancel context.CancelFunc
  96. // substreams holds all active watchers on this grpc stream
  97. substreams map[int64]*watcherStream
  98. // resuming holds all resuming watchers on this grpc stream
  99. resuming []*watcherStream
  100. // reqc sends a watch request from Watch() to the main goroutine
  101. reqc chan *watchRequest
  102. // respc receives data from the watch client
  103. respc chan *pb.WatchResponse
  104. // donec closes to broadcast shutdown
  105. donec chan struct{}
  106. // errc transmits errors from grpc Recv to the watch stream reconn logic
  107. errc chan error
  108. // closingc gets the watcherStream of closing watchers
  109. closingc chan *watcherStream
  110. // resumec closes to signal that all substreams should begin resuming
  111. resumec chan struct{}
  112. // closeErr is the error that closed the watch stream
  113. closeErr error
  114. }
  115. // watchRequest is issued by the subscriber to start a new watcher
  116. type watchRequest struct {
  117. ctx context.Context
  118. key string
  119. end string
  120. rev int64
  121. // progressNotify is for progress updates.
  122. progressNotify bool
  123. // get the previous key-value pair before the event happens
  124. prevKV bool
  125. // retc receives a chan WatchResponse once the watcher is established
  126. retc chan chan WatchResponse
  127. }
  128. // watcherStream represents a registered watcher
  129. type watcherStream struct {
  130. // initReq is the request that initiated this request
  131. initReq watchRequest
  132. // outc publishes watch responses to subscriber
  133. outc chan WatchResponse
  134. // recvc buffers watch responses before publishing
  135. recvc chan *WatchResponse
  136. // donec closes when the watcherStream goroutine stops.
  137. donec chan struct{}
  138. // closing is set to true when stream should be scheduled to shutdown.
  139. closing bool
  140. // id is the registered watch id on the grpc stream
  141. id int64
  142. // buf holds all events received from etcd but not yet consumed by the client
  143. buf []*WatchResponse
  144. }
  145. func NewWatcher(c *Client) Watcher {
  146. return &watcher{
  147. remote: pb.NewWatchClient(c.conn),
  148. streams: make(map[string]*watchGrpcStream),
  149. }
  150. }
  151. // never closes
  152. var valCtxCh = make(chan struct{})
  153. var zeroTime = time.Unix(0, 0)
  154. // ctx with only the values; never Done
  155. type valCtx struct{ context.Context }
  156. func (vc *valCtx) Deadline() (time.Time, bool) { return zeroTime, false }
  157. func (vc *valCtx) Done() <-chan struct{} { return valCtxCh }
  158. func (vc *valCtx) Err() error { return nil }
  159. func (w *watcher) newWatcherGrpcStream(inctx context.Context) *watchGrpcStream {
  160. ctx, cancel := context.WithCancel(&valCtx{inctx})
  161. wgs := &watchGrpcStream{
  162. owner: w,
  163. remote: w.remote,
  164. ctx: ctx,
  165. ctxKey: fmt.Sprintf("%v", inctx),
  166. cancel: cancel,
  167. substreams: make(map[int64]*watcherStream),
  168. respc: make(chan *pb.WatchResponse),
  169. reqc: make(chan *watchRequest),
  170. donec: make(chan struct{}),
  171. errc: make(chan error, 1),
  172. closingc: make(chan *watcherStream),
  173. resumec: make(chan struct{}),
  174. }
  175. go wgs.run()
  176. return wgs
  177. }
  178. // Watch posts a watch request to run() and waits for a new watcher channel
  179. func (w *watcher) Watch(ctx context.Context, key string, opts ...OpOption) WatchChan {
  180. ow := opWatch(key, opts...)
  181. wr := &watchRequest{
  182. ctx: ctx,
  183. key: string(ow.key),
  184. end: string(ow.end),
  185. rev: ow.rev,
  186. progressNotify: ow.progressNotify,
  187. prevKV: ow.prevKV,
  188. retc: make(chan chan WatchResponse, 1),
  189. }
  190. ok := false
  191. ctxKey := fmt.Sprintf("%v", ctx)
  192. // find or allocate appropriate grpc watch stream
  193. w.mu.Lock()
  194. if w.streams == nil {
  195. // closed
  196. w.mu.Unlock()
  197. ch := make(chan WatchResponse)
  198. close(ch)
  199. return ch
  200. }
  201. wgs := w.streams[ctxKey]
  202. if wgs == nil {
  203. wgs = w.newWatcherGrpcStream(ctx)
  204. w.streams[ctxKey] = wgs
  205. }
  206. donec := wgs.donec
  207. reqc := wgs.reqc
  208. w.mu.Unlock()
  209. // couldn't create channel; return closed channel
  210. closeCh := make(chan WatchResponse, 1)
  211. // submit request
  212. select {
  213. case reqc <- wr:
  214. ok = true
  215. case <-wr.ctx.Done():
  216. case <-donec:
  217. if wgs.closeErr != nil {
  218. closeCh <- WatchResponse{closeErr: wgs.closeErr}
  219. break
  220. }
  221. // retry; may have dropped stream from no ctxs
  222. return w.Watch(ctx, key, opts...)
  223. }
  224. // receive channel
  225. if ok {
  226. select {
  227. case ret := <-wr.retc:
  228. return ret
  229. case <-ctx.Done():
  230. case <-donec:
  231. if wgs.closeErr != nil {
  232. closeCh <- WatchResponse{closeErr: wgs.closeErr}
  233. break
  234. }
  235. // retry; may have dropped stream from no ctxs
  236. return w.Watch(ctx, key, opts...)
  237. }
  238. }
  239. close(closeCh)
  240. return closeCh
  241. }
  242. func (w *watcher) Close() (err error) {
  243. w.mu.Lock()
  244. streams := w.streams
  245. w.streams = nil
  246. w.mu.Unlock()
  247. for _, wgs := range streams {
  248. if werr := wgs.Close(); werr != nil {
  249. err = werr
  250. }
  251. }
  252. return err
  253. }
  254. func (w *watchGrpcStream) Close() (err error) {
  255. w.cancel()
  256. <-w.donec
  257. select {
  258. case err = <-w.errc:
  259. default:
  260. }
  261. return toErr(w.ctx, err)
  262. }
  263. func (w *watcher) closeStream(wgs *watchGrpcStream) {
  264. w.mu.Lock()
  265. close(wgs.donec)
  266. wgs.cancel()
  267. if w.streams != nil {
  268. delete(w.streams, wgs.ctxKey)
  269. }
  270. w.mu.Unlock()
  271. }
  272. func (w *watchGrpcStream) addSubstream(resp *pb.WatchResponse, ws *watcherStream) {
  273. if resp.WatchId == -1 {
  274. // failed; no channel
  275. close(ws.recvc)
  276. return
  277. }
  278. ws.id = resp.WatchId
  279. w.substreams[ws.id] = ws
  280. }
  281. func (w *watchGrpcStream) sendCloseSubstream(ws *watcherStream, resp *WatchResponse) {
  282. select {
  283. case ws.outc <- *resp:
  284. case <-ws.initReq.ctx.Done():
  285. case <-time.After(closeSendErrTimeout):
  286. }
  287. close(ws.outc)
  288. }
  289. func (w *watchGrpcStream) closeSubstream(ws *watcherStream) {
  290. // send channel response in case stream was never established
  291. select {
  292. case ws.initReq.retc <- ws.outc:
  293. default:
  294. }
  295. // close subscriber's channel
  296. if closeErr := w.closeErr; closeErr != nil && ws.initReq.ctx.Err() == nil {
  297. go w.sendCloseSubstream(ws, &WatchResponse{closeErr: w.closeErr})
  298. } else if ws.outc != nil {
  299. close(ws.outc)
  300. }
  301. if ws.id != -1 {
  302. delete(w.substreams, ws.id)
  303. return
  304. }
  305. for i := range w.resuming {
  306. if w.resuming[i] == ws {
  307. w.resuming[i] = nil
  308. return
  309. }
  310. }
  311. }
  312. // run is the root of the goroutines for managing a watcher client
  313. func (w *watchGrpcStream) run() {
  314. var wc pb.Watch_WatchClient
  315. var closeErr error
  316. // substreams marked to close but goroutine still running; needed for
  317. // avoiding double-closing recvc on grpc stream teardown
  318. closing := make(map[*watcherStream]struct{})
  319. defer func() {
  320. w.closeErr = closeErr
  321. // shutdown substreams and resuming substreams
  322. for _, ws := range w.substreams {
  323. if _, ok := closing[ws]; !ok {
  324. close(ws.recvc)
  325. }
  326. }
  327. for _, ws := range w.resuming {
  328. if _, ok := closing[ws]; ws != nil && !ok {
  329. close(ws.recvc)
  330. }
  331. }
  332. w.joinSubstreams()
  333. for toClose := len(w.substreams) + len(w.resuming); toClose > 0; toClose-- {
  334. w.closeSubstream(<-w.closingc)
  335. }
  336. w.owner.closeStream(w)
  337. }()
  338. // start a stream with the etcd grpc server
  339. if wc, closeErr = w.newWatchClient(); closeErr != nil {
  340. return
  341. }
  342. cancelSet := make(map[int64]struct{})
  343. for {
  344. select {
  345. // Watch() requested
  346. case wreq := <-w.reqc:
  347. outc := make(chan WatchResponse, 1)
  348. ws := &watcherStream{
  349. initReq: *wreq,
  350. id: -1,
  351. outc: outc,
  352. // unbufffered so resumes won't cause repeat events
  353. recvc: make(chan *WatchResponse),
  354. }
  355. ws.donec = make(chan struct{})
  356. go w.serveSubstream(ws, w.resumec)
  357. // queue up for watcher creation/resume
  358. w.resuming = append(w.resuming, ws)
  359. if len(w.resuming) == 1 {
  360. // head of resume queue, can register a new watcher
  361. wc.Send(ws.initReq.toPB())
  362. }
  363. // New events from the watch client
  364. case pbresp := <-w.respc:
  365. switch {
  366. case pbresp.Created:
  367. // response to head of queue creation
  368. if ws := w.resuming[0]; ws != nil {
  369. w.addSubstream(pbresp, ws)
  370. w.dispatchEvent(pbresp)
  371. w.resuming[0] = nil
  372. }
  373. if ws := w.nextResume(); ws != nil {
  374. wc.Send(ws.initReq.toPB())
  375. }
  376. case pbresp.Canceled:
  377. delete(cancelSet, pbresp.WatchId)
  378. if ws, ok := w.substreams[pbresp.WatchId]; ok {
  379. // signal to stream goroutine to update closingc
  380. close(ws.recvc)
  381. closing[ws] = struct{}{}
  382. }
  383. default:
  384. // dispatch to appropriate watch stream
  385. if ok := w.dispatchEvent(pbresp); ok {
  386. break
  387. }
  388. // watch response on unexpected watch id; cancel id
  389. if _, ok := cancelSet[pbresp.WatchId]; ok {
  390. break
  391. }
  392. cancelSet[pbresp.WatchId] = struct{}{}
  393. cr := &pb.WatchRequest_CancelRequest{
  394. CancelRequest: &pb.WatchCancelRequest{
  395. WatchId: pbresp.WatchId,
  396. },
  397. }
  398. req := &pb.WatchRequest{RequestUnion: cr}
  399. wc.Send(req)
  400. }
  401. // watch client failed to recv; spawn another if possible
  402. case err := <-w.errc:
  403. if toErr(w.ctx, err) == v3rpc.ErrNoLeader {
  404. closeErr = err
  405. return
  406. }
  407. if wc, closeErr = w.newWatchClient(); closeErr != nil {
  408. return
  409. }
  410. if ws := w.nextResume(); ws != nil {
  411. wc.Send(ws.initReq.toPB())
  412. }
  413. cancelSet = make(map[int64]struct{})
  414. case <-w.ctx.Done():
  415. return
  416. case ws := <-w.closingc:
  417. w.closeSubstream(ws)
  418. delete(closing, ws)
  419. if len(w.substreams)+len(w.resuming) == 0 {
  420. // no more watchers on this stream, shutdown
  421. return
  422. }
  423. }
  424. }
  425. }
  426. // nextResume chooses the next resuming to register with the grpc stream. Abandoned
  427. // streams are marked as nil in the queue since the head must wait for its inflight registration.
  428. func (w *watchGrpcStream) nextResume() *watcherStream {
  429. for len(w.resuming) != 0 {
  430. if w.resuming[0] != nil {
  431. return w.resuming[0]
  432. }
  433. w.resuming = w.resuming[1:len(w.resuming)]
  434. }
  435. return nil
  436. }
  437. // dispatchEvent sends a WatchResponse to the appropriate watcher stream
  438. func (w *watchGrpcStream) dispatchEvent(pbresp *pb.WatchResponse) bool {
  439. ws, ok := w.substreams[pbresp.WatchId]
  440. if !ok {
  441. return false
  442. }
  443. events := make([]*Event, len(pbresp.Events))
  444. for i, ev := range pbresp.Events {
  445. events[i] = (*Event)(ev)
  446. }
  447. wr := &WatchResponse{
  448. Header: *pbresp.Header,
  449. Events: events,
  450. CompactRevision: pbresp.CompactRevision,
  451. created: pbresp.Created,
  452. Canceled: pbresp.Canceled,
  453. }
  454. select {
  455. case ws.recvc <- wr:
  456. case <-ws.donec:
  457. return false
  458. }
  459. return true
  460. }
  461. // serveWatchClient forwards messages from the grpc stream to run()
  462. func (w *watchGrpcStream) serveWatchClient(wc pb.Watch_WatchClient) {
  463. for {
  464. resp, err := wc.Recv()
  465. if err != nil {
  466. select {
  467. case w.errc <- err:
  468. case <-w.donec:
  469. }
  470. return
  471. }
  472. select {
  473. case w.respc <- resp:
  474. case <-w.donec:
  475. return
  476. }
  477. }
  478. }
  479. // serveSubstream forwards watch responses from run() to the subscriber
  480. func (w *watchGrpcStream) serveSubstream(ws *watcherStream, resumec chan struct{}) {
  481. if ws.closing {
  482. panic("created substream goroutine but substream is closing")
  483. }
  484. // nextRev is the minimum expected next revision
  485. nextRev := ws.initReq.rev
  486. resuming := false
  487. defer func() {
  488. if !resuming {
  489. ws.closing = true
  490. }
  491. close(ws.donec)
  492. if !resuming {
  493. w.closingc <- ws
  494. }
  495. }()
  496. emptyWr := &WatchResponse{}
  497. for {
  498. curWr := emptyWr
  499. outc := ws.outc
  500. if len(ws.buf) > 0 && ws.buf[0].created {
  501. select {
  502. case ws.initReq.retc <- ws.outc:
  503. default:
  504. }
  505. ws.buf = ws.buf[1:]
  506. }
  507. if len(ws.buf) > 0 {
  508. curWr = ws.buf[0]
  509. } else {
  510. outc = nil
  511. }
  512. select {
  513. case outc <- *curWr:
  514. if ws.buf[0].Err() != nil {
  515. return
  516. }
  517. ws.buf[0] = nil
  518. ws.buf = ws.buf[1:]
  519. case wr, ok := <-ws.recvc:
  520. if !ok {
  521. // shutdown from closeSubstream
  522. return
  523. }
  524. // TODO pause channel if buffer gets too large
  525. ws.buf = append(ws.buf, wr)
  526. nextRev = wr.Header.Revision
  527. if len(wr.Events) > 0 {
  528. nextRev = wr.Events[len(wr.Events)-1].Kv.ModRevision + 1
  529. }
  530. ws.initReq.rev = nextRev
  531. case <-w.ctx.Done():
  532. return
  533. case <-ws.initReq.ctx.Done():
  534. return
  535. case <-resumec:
  536. resuming = true
  537. return
  538. }
  539. }
  540. // lazily send cancel message if events on missing id
  541. }
  542. func (w *watchGrpcStream) newWatchClient() (pb.Watch_WatchClient, error) {
  543. // mark all substreams as resuming
  544. close(w.resumec)
  545. w.resumec = make(chan struct{})
  546. w.joinSubstreams()
  547. for _, ws := range w.substreams {
  548. ws.id = -1
  549. w.resuming = append(w.resuming, ws)
  550. }
  551. // strip out nils, if any
  552. var resuming []*watcherStream
  553. for _, ws := range w.resuming {
  554. if ws != nil {
  555. resuming = append(resuming, ws)
  556. }
  557. }
  558. w.resuming = resuming
  559. w.substreams = make(map[int64]*watcherStream)
  560. // connect to grpc stream while accepting watcher cancelation
  561. stopc := make(chan struct{})
  562. donec := w.waitCancelSubstreams(stopc)
  563. wc, err := w.openWatchClient()
  564. close(stopc)
  565. <-donec
  566. // serve all non-closing streams, even if there's a client error
  567. // so that the teardown path can shutdown the streams as expected.
  568. for _, ws := range w.resuming {
  569. if ws.closing {
  570. continue
  571. }
  572. ws.donec = make(chan struct{})
  573. go w.serveSubstream(ws, w.resumec)
  574. }
  575. if err != nil {
  576. return nil, v3rpc.Error(err)
  577. }
  578. // receive data from new grpc stream
  579. go w.serveWatchClient(wc)
  580. return wc, nil
  581. }
  582. func (w *watchGrpcStream) waitCancelSubstreams(stopc <-chan struct{}) <-chan struct{} {
  583. var wg sync.WaitGroup
  584. wg.Add(len(w.resuming))
  585. donec := make(chan struct{})
  586. for i := range w.resuming {
  587. go func(ws *watcherStream) {
  588. defer wg.Done()
  589. if ws.closing {
  590. return
  591. }
  592. select {
  593. case <-ws.initReq.ctx.Done():
  594. // closed ws will be removed from resuming
  595. ws.closing = true
  596. close(ws.outc)
  597. ws.outc = nil
  598. go func() { w.closingc <- ws }()
  599. case <-stopc:
  600. }
  601. }(w.resuming[i])
  602. }
  603. go func() {
  604. defer close(donec)
  605. wg.Wait()
  606. }()
  607. return donec
  608. }
  609. // joinSubstream waits for all substream goroutines to complete
  610. func (w *watchGrpcStream) joinSubstreams() {
  611. for _, ws := range w.substreams {
  612. <-ws.donec
  613. }
  614. for _, ws := range w.resuming {
  615. if ws != nil {
  616. <-ws.donec
  617. }
  618. }
  619. }
  620. // openWatchClient retries opening a watchclient until retryConnection fails
  621. func (w *watchGrpcStream) openWatchClient() (ws pb.Watch_WatchClient, err error) {
  622. for {
  623. select {
  624. case <-w.ctx.Done():
  625. if err == nil {
  626. return nil, w.ctx.Err()
  627. }
  628. return nil, err
  629. default:
  630. }
  631. if ws, err = w.remote.Watch(w.ctx, grpc.FailFast(false)); ws != nil && err == nil {
  632. break
  633. }
  634. if isHaltErr(w.ctx, err) {
  635. return nil, v3rpc.Error(err)
  636. }
  637. }
  638. return ws, nil
  639. }
  640. // toPB converts an internal watch request structure to its protobuf messagefunc (wr *watchRequest)
  641. func (wr *watchRequest) toPB() *pb.WatchRequest {
  642. req := &pb.WatchCreateRequest{
  643. StartRevision: wr.rev,
  644. Key: []byte(wr.key),
  645. RangeEnd: []byte(wr.end),
  646. ProgressNotify: wr.progressNotify,
  647. PrevKv: wr.prevKV,
  648. }
  649. cr := &pb.WatchRequest_CreateRequest{CreateRequest: req}
  650. return &pb.WatchRequest{RequestUnion: cr}
  651. }