doc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // if err == grpc.ErrClientConnTimeout {
  24. // // handle errors
  25. // }
  26. //
  27. // cli, err := clientv3.New(clientv3.Config{
  28. // Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
  29. // DialTimeout: 5 * time.Second,
  30. // })
  31. // if err != nil {
  32. // // handle error!
  33. // }
  34. // defer cli.Close()
  35. //
  36. // Make sure to close the client after using it. If the client is not closed, the
  37. // connection will have leaky goroutines.
  38. //
  39. // To specify a client request timeout, wrap the context with context.WithTimeout:
  40. //
  41. // ctx, cancel := context.WithTimeout(context.Background(), timeout)
  42. // resp, err := kvc.Put(ctx, "sample_key", "sample_value")
  43. // cancel()
  44. // if err != nil {
  45. // // handle error!
  46. // }
  47. // // use the response
  48. //
  49. // The Client has internal state (watchers and leases), so Clients should be reused instead of created as needed.
  50. // Clients are safe for concurrent use by multiple goroutines.
  51. //
  52. // etcd client returns 3 types of errors:
  53. //
  54. // 1. context error: canceled or deadline exceeded.
  55. // 2. gRPC status error: e.g. when clock drifts in server-side before client's context deadline exceeded.
  56. // 3. gRPC error: see https://github.com/coreos/etcd/blob/master/etcdserver/api/v3rpc/rpctypes/error.go
  57. //
  58. // Here is the example code to handle client errors:
  59. //
  60. // resp, err := kvc.Put(ctx, "", "")
  61. // if err != nil {
  62. // if err == context.Canceled {
  63. // // ctx is canceled by another routine
  64. // } else if err == context.DeadlineExceeded {
  65. // // ctx is attached with a deadline and it exceeded
  66. // } else if ev, ok := status.FromError(err); ok {
  67. // code := ev.Code()
  68. // if code == codes.DeadlineExceeded {
  69. // // server-side context might have timed-out first (due to clock skew)
  70. // // while original client-side context is not timed-out yet
  71. // }
  72. // } else if verr, ok := err.(*v3rpc.ErrEmptyKey); ok {
  73. // // process (verr.Errors)
  74. // } else {
  75. // // bad cluster endpoints, which are not etcd servers
  76. // }
  77. // }
  78. //
  79. // go func() { cli.Close() }()
  80. // _, err := kvc.Get(ctx, "a")
  81. // if err != nil {
  82. // if err == context.Canceled {
  83. // // grpc balancer calls 'Get' with an inflight client.Close
  84. // } else if err == grpc.ErrClientConnClosing {
  85. // // grpc balancer calls 'Get' after client.Close.
  86. // }
  87. // }
  88. //
  89. package clientv3