@@ -23,6 +23,7 @@ import (
23
23
"time"
24
24
25
25
pb "go.etcd.io/etcd/api/v3/etcdserverpb"
26
+ "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
26
27
clientv3 "go.etcd.io/etcd/client/v3"
27
28
)
28
29
@@ -95,18 +96,6 @@ func (c *Cache) Watch(ctx context.Context, key string, opts ...clientv3.OpOption
95
96
op := clientv3 .OpWatch (key , opts ... )
96
97
startRev := op .Rev ()
97
98
98
- if startRev != 0 {
99
- if oldest := c .demux .PeekOldest (); oldest != 0 && startRev < oldest {
100
- ch := make (chan clientv3.WatchResponse , 1 )
101
- ch <- clientv3.WatchResponse {
102
- Canceled : true ,
103
- CompactRevision : startRev ,
104
- }
105
- close (ch )
106
- return ch
107
- }
108
- }
109
-
110
99
pred , err := c .validateWatch (key , op )
111
100
if err != nil {
112
101
ch := make (chan clientv3.WatchResponse , 1 )
@@ -132,6 +121,13 @@ func (c *Cache) Watch(ctx context.Context, key string, opts ...clientv3.OpOption
132
121
return
133
122
case events , ok := <- w .eventQueue :
134
123
if ! ok {
124
+ if w .cancelResp != nil {
125
+ select {
126
+ case <- ctx .Done ():
127
+ case <- c .internalCtx .Done ():
128
+ case responseChan <- * w .cancelResp :
129
+ }
130
+ }
135
131
return
136
132
}
137
133
select {
@@ -299,7 +295,11 @@ func (c *Cache) watchEvents(watchCh clientv3.WatchChan, applyErr <-chan error, r
299
295
readyOnce .Do (func () { c .ready .Set () })
300
296
if err := resp .Err (); err != nil {
301
297
c .ready .Reset ()
302
- c .demux .Purge ()
298
+ if errors .Is (err , rpctypes .ErrCompacted ) || resp .CompactRevision > 0 {
299
+ c .demux .Compact (resp .CompactRevision )
300
+ } else {
301
+ c .demux .Purge ()
302
+ }
303
303
return err
304
304
}
305
305
c .demux .Broadcast (resp .Events )
0 commit comments