client.go 4.3 KB

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