예제 #1
0
파일: client.go 프로젝트: cpuguy83/drax
// WatchTree watches a dir and all subdirs for changes and notifies the caller
func (c *Client) WatchTree(dir string, stopCh <-chan struct{}) (<-chan []*store.KVPair, error) {
	req := &api.Request{
		Action: api.WatchTree,
		Key:    dir,
	}
	conn, err := c.stream(req)
	if err != nil {
		return nil, err
	}

	var kv []*api.KVPair
	dec := api.NewDecoder(conn)
	chKV := make(chan []*store.KVPair)
	go func() {
		defer conn.Close()
		for {
			select {
			case <-stopCh:
				return
			default:
				if err := dec.Decode(&kv); err != nil {
					if err != io.EOF {
						dec = api.NewDecoder(conn)
						continue
					}
					return
				}
				var kvList []*store.KVPair
				for _, apiKv := range kv {
					kvList = append(kvList, kvToLibKV(apiKv))
				}
				select {
				case chKV <- kvList:
				case <-stopCh:
					return
				}
			}
		}
	}()
	return chKV, err
}
예제 #2
0
파일: client.go 프로젝트: cpuguy83/drax
// Watch watches a key for changes and notifies the caller
// Watch is not implemented
func (c *Client) Watch(key string, stopCh <-chan struct{}) (<-chan *store.KVPair, error) {
	req := &api.Request{
		Action: api.Watch,
		Key:    key,
	}
	conn, err := c.stream(req)
	if err != nil {
		return nil, err
	}

	var kv api.KVPair
	dec := api.NewDecoder(conn)
	chKV := make(chan *store.KVPair)
	go func() {
		defer conn.Close()
		for {
			select {
			case <-stopCh:
				return
			default:
				if err := dec.Decode(&kv); err != nil {
					if err != io.EOF {
						dec = api.NewDecoder(conn)
						continue
					}
					return
				}
				select {
				case chKV <- kvToLibKV(&kv):
				case <-stopCh:
					return
				}
			}
		}
	}()

	return chKV, nil
}