Esempio n. 1
0
// DataChange blocks until the data of the watched Zookeeper node changes
// reltive to the returned values of the last invokation of Data or DataChange.
// It returns the new data and stat values.
func (w *Watch) DataChange(sinceStat *zookeeper.Stat, expire time.Duration) (data string, stat *zookeeper.Stat, err error) {
	// Still alive?
	w.lk.Lock()
	z, dwatch := w.zookeeper, w.dwatch
	w.lk.Unlock()
	if z == nil {
		return "", nil, ErrClosed
	}

	// Wait if watch present, otherwise read from Zookeeper
	if dwatch != nil {
		// If we already have a newer revision, return it
		w.lk.Lock()
		if sinceStat == nil || w.dstat.Version() > sinceStat.Version() {
			defer w.lk.Unlock()
			return w.data, w.dstat, nil
		}
		w.lk.Unlock()

		if expire == 0 {
			<-dwatch
		} else {
			select {
			case <-dwatch:
			case <-time.After(expire):
				return "", nil, ErrExpire
			}
		}
	}
	return w.fetchData()
}