etcd-io / etcd

Distributed reliable key-value store for the most critical data of a distributed system
https://etcd.io
Apache License 2.0
47.83k stars 9.77k forks source link

The events etcd watched were lost #11092

Closed whitetower957 closed 5 years ago

whitetower957 commented 5 years ago

I used etcd3 to write golang project on windows10, The problem is when i registered 10 pairs of different key-value,etcd3 watch was only watched 2 pairs of key-value.

func EtcdWatch() {
watcher := clientv3.NewWatcher(Cli)
   watchChan := watcher.Watch(context.TODO(), DefaultPrefix, clientv3.WithPrefix(), clientv3.WithRev(e.Latestrevision))
   var evType string
   var Evchan chan string
   Evchan = make(chan string,1)
   go func() {
      for v := range watchChan {
         for _, event := range v.Events {
            Rwmutex.Lock()
            switch event.Type {
            case mvccpb.PUT:
               evType = v"put"
            case mvccpb.DELETE:
               evType = "delete"
            }
            Evchan <- evType
            Rwmutex.Unlock()
         }
      }
   }()
} 

I promised 10 key-value were registered.Because i used client.get(context.todo,key) to get the key-value. And 10 pairs of key-value had the same DefaultPrefix and leaseID

jingyih commented 5 years ago

What is e.Latestrevision? Keys that created or modified prior to this revision will not be returned in watch channel.

whitetower957 commented 5 years ago
resp, err := e.Cli.Get(timeoutctx, DefaultPrefix, clientv3.WithPrefix())
e.Latestrevision = resp.Header.Revision + 1
whitetower957 commented 5 years ago

I solved it ,because i used channel to transfer the etcd watched events ,it blocks the watch channel i guess.

go func(){
     Evchan <- evType
}