I struggle with getting etcd
watch
working with combination of clients.
What works: I do Watch
in golang
client and I do put
also there. Then watch register change. If I use etcdctl
in same way i.e. watch/listen both in command line it also work.
What doesnt work: I publish from command line and watch in go or vice versa.
- watch commandline, put golang: doesnt work
- watch commandline, put commandline: works
- watch golang, put commandline: doesnt work
- watch golang, put golang: works
By "doesnt work" I mean watcher is not triggered.
Go code I am playing with (you can run like go run main.go (true|false) (true|false))
:
package main
import (
"github.com/coreos/etcd/clientv3"
"context"
"time"
"fmt"
"os"
)
func main() {
listen := os.Args[1]
submit := os.Args[2]
cfg := clientv3.Config{
Endpoints: []string{"http://10.0.75.1:2379"},
}
c,_ := clientv3.New(cfg)
if listen == "true" {
fmt.Println("listening: on")
ch := c.Watch(context.Background(), "/foo")
go func() {
for s := range ch {
fmt.Println("change: ", string(s.Events[0].Kv.Value))
}
}()
} else {
fmt.Println("listening: off")
}
if submit == "true" {
fmt.Println("submiting: on")
go func() {
for {
c.Put(context.Background(), "/foo", time.Now().String())
time.Sleep(1 * time.Second)
}
}()
} else {
fmt.Println("submiting: off")
}
println("waiting 1h")
time.Sleep(1 * time.Hour)
}
etcdctl
commands I am using to publish/watch:
update:
no_proxy=10.0.75.1 ETCDCTL_API=3 etcdctl.exe --endpoints=http://10.0.75.1:2379 put /foo "$(date)"
watch:
no_proxy=10.0.75.1 ETCDCTL_API=3 etcdctl.exe --endpoints=http://10.0.75.1:2379 watch /foo