example_watch_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. )
  21. func ExampleWatcher_watch() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. rch := cli.Watch(context.Background(), "foo")
  31. for wresp := range rch {
  32. for _, ev := range wresp.Events {
  33. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  34. }
  35. }
  36. // PUT "foo" : "bar"
  37. }
  38. func ExampleWatcher_watchWithPrefix() {
  39. cli, err := clientv3.New(clientv3.Config{
  40. Endpoints: endpoints,
  41. DialTimeout: dialTimeout,
  42. })
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. defer cli.Close()
  47. rch := cli.Watch(context.Background(), "foo", clientv3.WithPrefix())
  48. for wresp := range rch {
  49. for _, ev := range wresp.Events {
  50. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  51. }
  52. }
  53. // PUT "foo1" : "bar"
  54. }
  55. func ExampleWatcher_watchWithRange() {
  56. cli, err := clientv3.New(clientv3.Config{
  57. Endpoints: endpoints,
  58. DialTimeout: dialTimeout,
  59. })
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. defer cli.Close()
  64. // watches within ['foo1', 'foo4'), in lexicographical order
  65. rch := cli.Watch(context.Background(), "foo1", clientv3.WithRange("foo4"))
  66. for wresp := range rch {
  67. for _, ev := range wresp.Events {
  68. fmt.Printf("%s %q : %q\n", ev.Type, ev.Kv.Key, ev.Kv.Value)
  69. }
  70. }
  71. // PUT "foo1" : "bar"
  72. // PUT "foo2" : "bar"
  73. // PUT "foo3" : "bar"
  74. }
  75. func ExampleWatcher_watchWithProgressNotify() {
  76. cli, err := clientv3.New(clientv3.Config{
  77. Endpoints: endpoints,
  78. DialTimeout: dialTimeout,
  79. })
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. rch := cli.Watch(context.Background(), "foo", clientv3.WithProgressNotify())
  84. wresp := <-rch
  85. fmt.Printf("wresp.Header.Revision: %d\n", wresp.Header.Revision)
  86. fmt.Println("wresp.IsProgressNotify:", wresp.IsProgressNotify())
  87. // wresp.Header.Revision: 0
  88. // wresp.IsProgressNotify: true
  89. }