client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "errors"
  17. "net"
  18. "net/url"
  19. "strings"
  20. "sync"
  21. "time"
  22. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/credentials"
  25. "github.com/coreos/etcd/pkg/transport"
  26. )
  27. var (
  28. ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
  29. )
  30. // Client provides and manages an etcd v3 client session.
  31. type Client struct {
  32. Cluster
  33. KV
  34. Lease
  35. Watcher
  36. Auth
  37. conn *grpc.ClientConn
  38. cfg Config
  39. creds *credentials.TransportAuthenticator
  40. mu sync.RWMutex // protects connection selection and error list
  41. errors []error // errors passed to retryConnection
  42. }
  43. // EndpointDialer is a policy for choosing which endpoint to dial next
  44. type EndpointDialer func(*Client) (*grpc.ClientConn, error)
  45. type Config struct {
  46. // Endpoints is a list of URLs
  47. Endpoints []string
  48. // RetryDialer chooses the next endpoint to use
  49. RetryDialer EndpointDialer
  50. // DialTimeout is the timeout for failing to establish a connection.
  51. DialTimeout time.Duration
  52. // TLS holds the client secure credentials, if any.
  53. TLS *transport.TLSInfo
  54. }
  55. // New creates a new etcdv3 client from a given configuration.
  56. func New(cfg Config) (*Client, error) {
  57. if cfg.RetryDialer == nil {
  58. cfg.RetryDialer = dialEndpointList
  59. }
  60. if len(cfg.Endpoints) == 0 {
  61. return nil, ErrNoAvailableEndpoints
  62. }
  63. return newClient(&cfg)
  64. }
  65. // NewFromURL creates a new etcdv3 client from a URL.
  66. func NewFromURL(url string) (*Client, error) {
  67. return New(Config{Endpoints: []string{url}})
  68. }
  69. // Close shuts down the client's etcd connections.
  70. func (c *Client) Close() error {
  71. c.Watcher.Close()
  72. c.Lease.Close()
  73. return c.conn.Close()
  74. }
  75. // Endpoints lists the registered endpoints for the client.
  76. func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
  77. // Errors returns all errors that have been observed since called last.
  78. func (c *Client) Errors() (errs []error) {
  79. c.mu.Lock()
  80. defer c.mu.Unlock()
  81. errs = c.errors
  82. c.errors = nil
  83. return errs
  84. }
  85. // Dial establishes a connection for a given endpoint using the client's config
  86. func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
  87. opts := []grpc.DialOption{
  88. grpc.WithBlock(),
  89. grpc.WithTimeout(c.cfg.DialTimeout),
  90. }
  91. if c.creds != nil {
  92. opts = append(opts, grpc.WithTransportCredentials(*c.creds))
  93. } else {
  94. opts = append(opts, grpc.WithInsecure())
  95. }
  96. if url, uerr := url.Parse(endpoint); uerr == nil && url.Scheme == "unix" {
  97. f := func(a string, t time.Duration) (net.Conn, error) {
  98. return net.DialTimeout("unix", a, t)
  99. }
  100. // strip unix:// prefix so certs work
  101. endpoint = url.Host
  102. opts = append(opts, grpc.WithDialer(f))
  103. }
  104. conn, err := grpc.Dial(endpoint, opts...)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return conn, nil
  109. }
  110. func newClient(cfg *Config) (*Client, error) {
  111. if cfg == nil {
  112. cfg = &Config{RetryDialer: dialEndpointList}
  113. }
  114. var creds *credentials.TransportAuthenticator
  115. if cfg.TLS != nil {
  116. tlscfg, err := cfg.TLS.ClientConfig()
  117. if err != nil {
  118. return nil, err
  119. }
  120. c := credentials.NewTLS(tlscfg)
  121. creds = &c
  122. }
  123. // use a temporary skeleton client to bootstrap first connection
  124. conn, err := cfg.RetryDialer(&Client{cfg: *cfg, creds: creds})
  125. if err != nil {
  126. return nil, err
  127. }
  128. client := &Client{
  129. conn: conn,
  130. cfg: *cfg,
  131. creds: creds,
  132. }
  133. client.Cluster = NewCluster(client)
  134. client.KV = NewKV(client)
  135. client.Lease = NewLease(client)
  136. client.Watcher = NewWatcher(client)
  137. client.Auth = NewAuth(client)
  138. return client, nil
  139. }
  140. // ActiveConnection returns the current in-use connection
  141. func (c *Client) ActiveConnection() *grpc.ClientConn {
  142. c.mu.RLock()
  143. defer c.mu.RUnlock()
  144. return c.conn
  145. }
  146. // retryConnection establishes a new connection
  147. func (c *Client) retryConnection(oldConn *grpc.ClientConn, err error) (*grpc.ClientConn, error) {
  148. c.mu.Lock()
  149. defer c.mu.Unlock()
  150. if err != nil {
  151. c.errors = append(c.errors, err)
  152. }
  153. if oldConn != c.conn {
  154. // conn has already been updated
  155. return c.conn, nil
  156. }
  157. conn, dialErr := c.cfg.RetryDialer(c)
  158. if dialErr != nil {
  159. c.errors = append(c.errors, dialErr)
  160. return nil, dialErr
  161. }
  162. c.conn = conn
  163. return c.conn, nil
  164. }
  165. // dialEndpointList attempts to connect to each endpoint in order until a
  166. // connection is established.
  167. func dialEndpointList(c *Client) (*grpc.ClientConn, error) {
  168. var err error
  169. for _, ep := range c.Endpoints() {
  170. conn, curErr := c.Dial(ep)
  171. if curErr != nil {
  172. err = curErr
  173. } else {
  174. return conn, nil
  175. }
  176. }
  177. return nil, err
  178. }
  179. // isHalted returns true if the given error and context indicate no forward
  180. // progress can be made, even after reconnecting.
  181. func isHalted(ctx context.Context, err error) bool {
  182. isRPCError := strings.HasPrefix(grpc.ErrorDesc(err), "etcdserver: ")
  183. return isRPCError || ctx.Err() != nil
  184. }