doc.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/coreos/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 ev, ok := status.FromError(err); ok {
  74. // code := ev.Code()
  75. // if code == codes.DeadlineExceeded {
  76. // // server-side context might have timed-out first (due to clock skew)
  77. // // while original client-side context is not timed-out yet
  78. // }
  79. // } else if verr, ok := err.(*v3rpc.ErrEmptyKey); ok {
  80. // // process (verr.Errors)
  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. // if err == context.Canceled {
  90. // // grpc balancer calls 'Get' with an inflight client.Close
  91. // } else if err == grpc.ErrClientConnClosing {
  92. // // grpc balancer calls 'Get' after client.Close.
  93. // }
  94. // }
  95. //
  96. package clientv3