Skip to content

Instantly share code, notes, and snippets.

@tcotav
Created September 17, 2015 04:55
Show Gist options
  • Save tcotav/7df72a406a2f82b6357d to your computer and use it in GitHub Desktop.
Save tcotav/7df72a406a2f82b6357d to your computer and use it in GitHub Desktop.
Using watch with new etcd 2.2 golang library
package main
import (
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"log"
"time"
)
func main() {
cfg := client.Config{
Endpoints: []string{"http://127.0.0.1:4001"},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
watcherOpts := client.WatcherOptions{AfterIndex: 0, Recursive: true}
w := kapi.Watcher("/base", &watcherOpts)
for {
r,err := w.Next(context.Background())
if err != nil {
log.Fatal("Error occurred", err)
}
action := r.Action
// do something with Response r here
log.Printf("action %s", action)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment