doc.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 implements the official Go etcd client for v3.
  15. //
  16. // Create client using `clientv3.New`:
  17. //
  18. // // expect dial time-out on ipv4 blackhole
  19. // _, err := clientv3.New(clientv3.Config{
  20. // Endpoints: []string{"http://254.0.0.1:12345"},
  21. // DialTimeout: 2 * time.Second,
  22. // })
  23. //
  24. // // etcd clientv3 >= v3.2.10, grpc/grpc-go >= v1.7.3
  25. // if err == context.DeadlineExceeded {
  26. // // handle errors
  27. // }
  28. //
  29. // // etcd clientv3 <= v3.2.9, grpc/grpc-go <= v1.2.1
  30. // if err == grpc.ErrClientConnTimeout {
  31. // // handle errors
  32. // }
  33. //
  34. // cli, err := clientv3.New(clientv3.Config{
  35. // Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
  36. // DialTimeout: 5 * time.Second,
  37. // })
  38. // if err != nil {
  39. // // handle error!
  40. // }
  41. // defer cli.Close()
  42. //
  43. // Make sure to close the client after using it. If the client is not closed, the
  44. // connection will have leaky goroutines.
  45. //
  46. // To specify a client request timeout, wrap the context with context.WithTimeout:
  47. //
  48. // ctx, cancel := context.WithTimeout(context.Background(), timeout)
  49. // resp, err := kvc.Put(ctx, "sample_key", "sample_value")
  50. // cancel()
  51. // if err != nil {
  52. // // handle error!
  53. // }
  54. // // use the response
  55. //
  56. // The Client has internal state (watchers and leases), so Clients should be reused instead of created as needed.
  57. // Clients are safe for concurrent use by multiple goroutines.
  58. //
  59. // etcd client returns 3 types of errors:
  60. //
  61. // 1. context error: canceled or deadline exceeded.
  62. // 2. gRPC status error: e.g. when clock drifts in server-side before client's context deadline exceeded.
  63. // 3. gRPC error: see https://github.com/etcd-io/etcd/blob/master/etcdserver/api/v3rpc/rpctypes/error.go
  64. //
  65. // Here is the example code to handle client errors:
  66. //
  67. // resp, err := kvc.Put(ctx, "", "")
  68. // if err != nil {
  69. // if err == context.Canceled {
  70. // // ctx is canceled by another routine
  71. // } else if err == context.DeadlineExceeded {
  72. // // ctx is attached with a deadline and it exceeded
  73. // } else if err == rpctypes.ErrEmptyKey {
  74. // // client-side error: key is not provided
  75. // } else if ev, ok := status.FromError(err); ok {
  76. // code := ev.Code()
  77. // if code == codes.DeadlineExceeded {
  78. // // server-side context might have timed-out first (due to clock skew)
  79. // // while original client-side context is not timed-out yet
  80. // }
  81. // } else {
  82. // // bad cluster endpoints, which are not etcd servers
  83. // }
  84. // }
  85. //
  86. // go func() { cli.Close() }()
  87. // _, err := kvc.Get(ctx, "a")
  88. // if err != nil {
  89. // // with etcd clientv3 <= v3.3
  90. // if err == context.Canceled {
  91. // // grpc balancer calls 'Get' with an inflight client.Close
  92. // } else if err == grpc.ErrClientConnClosing { // <= gRCP v1.7.x
  93. // // grpc balancer calls 'Get' after client.Close.
  94. // }
  95. // // with etcd clientv3 >= v3.4
  96. // if clientv3.IsConnCanceled(err) {
  97. // // gRPC client connection is closed
  98. // }
  99. // }
  100. //
  101. // The grpc load balancer is registered statically and is shared across etcd clients.
  102. // To enable detailed load balancer logging, set the ETCD_CLIENT_DEBUG environment
  103. // variable. E.g. "ETCD_CLIENT_DEBUG=1".
  104. //
  105. package clientv3