func watch(conn *doozer.Conn, events chan doozer.Event, revs chan int64) { for { rev := <-revs event, err := conn.Wait("/*", rev) if err != nil { panic("error waiting for event, bailing") } revs <- event.Rev + 1 events <- event } }
func waitFor(cl *doozer.Conn, path string) { var rev int64 for { ev, err := cl.Wait(path, rev) if err != nil { panic(err) } if ev.IsSet() && len(ev.Body) > 0 { break } rev = ev.Rev + 1 } }
func follow(st *store.Store, cl *doozer.Conn, rev int64, stop chan bool) { for { ev, err := cl.Wait("/**", rev) if err != nil { panic(err) } // store.Clobber is okay here because the event // has already passed through another store mut := store.MustEncodeSet(ev.Path, string(ev.Body), store.Clobber) st.Ops <- store.Op{ev.Rev, mut} rev = ev.Rev + 1 select { case <-stop: return default: } } }
// wait waits on a changes for the fiven file starting at the given // revision from the given doozer connection. It sends updated peer // lists on the returned channel. func wait(d *doozer.Conn, file string, rev *int64) chan []string { c := make(chan []string, 1) cur := *rev go func() { for { // Wait for the change. e, err := d.Wait(file, cur+1) if err != nil { log.Println("waiting failed (no longer watching):", err) close(c) return } // Update the revision and send the change on the channel. atomic.CompareAndSwapInt64(rev, cur, e.Rev) cur = e.Rev c <- strings.Split(string(e.Body), " ") } }() return c }