rpcxio / rpcx-etcd

etcd plugin for rpcx
Apache License 2.0
29 stars 24 forks source link

watch channel 被关闭后,会无限循环 #28

Closed learnd closed 4 months ago

learnd commented 1 year ago

func (s EtcdV3) WatchTree(directory string, stopCh <-chan struct{}) (<-chan []store.KVPair, error) { watchCh := make(chan []*store.KVPair)

go func() {
    defer close(watchCh)

    list, err := s.List(directory)
    if err != nil {
        if !s.AllowKeyNotFound || err != store.ErrKeyNotFound {
            return
        }
    }

    watchCh <- list

    rch := s.client.Watch(context.Background(), directory, clientv3.WithPrefix())
    for {
        select {
        case <-s.done:
            return
        case resp := <-rch:
            if resp.Canceled { // watch is canceled
                return
            }

            list, err := s.List(directory)
            if err != nil {
                if !s.AllowKeyNotFound || err != store.ErrKeyNotFound {
                    continue
                }
            }
            watchCh <- list
        }
    }
}()

rch被关掉,导致case resp := <-rch:无限循环执行