client.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2016 CoreOS, Inc.
  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
  15. import (
  16. "sync"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/codes"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. )
  21. // Client provides and manages an etcd v3 client session.
  22. type Client struct {
  23. // KV is the keyvalue API for the client's connection.
  24. KV pb.KVClient
  25. // Lease is the lease API for the client's connection.
  26. Lease pb.LeaseClient
  27. // Watch is the watch API for the client's connection.
  28. Watch pb.WatchClient
  29. // Cluster is the cluster API for the client's connection.
  30. Cluster pb.ClusterClient
  31. conn *grpc.ClientConn
  32. cfg Config
  33. mu sync.RWMutex // protects connection selection and error list
  34. errors []error // errors passed to retryConnection
  35. }
  36. // EndpointDialer is a policy for choosing which endpoint to dial next
  37. type EndpointDialer func(*Client) (*grpc.ClientConn, error)
  38. type Config struct {
  39. // Endpoints is a list of URLs
  40. Endpoints []string
  41. // RetryDialer chooses the next endpoint to use
  42. RetryDialer EndpointDialer
  43. // TODO TLS options
  44. }
  45. // New creates a new etcdv3 client from a given configuration.
  46. func New(cfg Config) (*Client, error) {
  47. if cfg.RetryDialer == nil {
  48. cfg.RetryDialer = dialEndpointList
  49. }
  50. // use a temporary skeleton client to bootstrap first connection
  51. conn, err := cfg.RetryDialer(&Client{cfg: cfg})
  52. if err != nil {
  53. return nil, err
  54. }
  55. return newClient(conn, &cfg), nil
  56. }
  57. // NewFromURL creates a new etcdv3 client from a URL.
  58. func NewFromURL(url string) (*Client, error) {
  59. return New(Config{Endpoints: []string{url}})
  60. }
  61. // NewFromConn creates a new etcdv3 client from an established grpc Connection.
  62. func NewFromConn(conn *grpc.ClientConn) *Client { return newClient(conn, nil) }
  63. // Clone creates a copy of client with the old connection and new API clients.
  64. func (c *Client) Clone() *Client { return newClient(c.conn, &c.cfg) }
  65. // Close shuts down the client's etcd connections.
  66. func (c *Client) Close() error {
  67. return c.conn.Close()
  68. }
  69. // Endpoints lists the registered endpoints for the client.
  70. func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
  71. // Errors returns all errors that have been observed since called last.
  72. func (c *Client) Errors() (errs []error) {
  73. c.mu.Lock()
  74. defer c.mu.Unlock()
  75. errs = c.errors
  76. c.errors = nil
  77. return errs
  78. }
  79. // Dial establishes a connection for a given endpoint using the client's config
  80. func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
  81. // TODO: enable grpc.WithTransportCredentials(creds)
  82. return grpc.Dial(endpoint, grpc.WithInsecure())
  83. }
  84. func newClient(conn *grpc.ClientConn, cfg *Config) *Client {
  85. if cfg == nil {
  86. cfg = &Config{RetryDialer: dialEndpointList}
  87. }
  88. return &Client{
  89. KV: pb.NewKVClient(conn),
  90. Lease: pb.NewLeaseClient(conn),
  91. Watch: pb.NewWatchClient(conn),
  92. Cluster: pb.NewClusterClient(conn),
  93. conn: conn,
  94. cfg: *cfg,
  95. }
  96. }
  97. // activeConnection returns the current in-use connection
  98. func (c *Client) activeConnection() *grpc.ClientConn {
  99. c.mu.RLock()
  100. defer c.mu.RUnlock()
  101. return c.conn
  102. }
  103. // refreshConnection establishes a new connection
  104. func (c *Client) retryConnection(oldConn *grpc.ClientConn, err error) (*grpc.ClientConn, error) {
  105. c.mu.Lock()
  106. defer c.mu.Unlock()
  107. if err != nil {
  108. c.errors = append(c.errors, err)
  109. }
  110. if oldConn != c.conn {
  111. // conn has already been updated
  112. return c.conn, nil
  113. }
  114. conn, dialErr := c.cfg.RetryDialer(c)
  115. if dialErr != nil {
  116. c.errors = append(c.errors, dialErr)
  117. return nil, dialErr
  118. }
  119. c.conn = conn
  120. return c.conn, nil
  121. }
  122. // dialEndpoints attempts to connect to each endpoint in order until a
  123. // connection is established.
  124. func dialEndpointList(c *Client) (*grpc.ClientConn, error) {
  125. var err error
  126. for _, ep := range c.Endpoints() {
  127. conn, curErr := c.Dial(ep)
  128. if err != nil {
  129. err = curErr
  130. } else {
  131. return conn, nil
  132. }
  133. }
  134. return nil, err
  135. }
  136. func isRPCError(err error) bool {
  137. return grpc.Code(err) != codes.Unknown
  138. }