client.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. "io/ioutil"
  18. "log"
  19. "net"
  20. "net/url"
  21. "strings"
  22. "sync"
  23. "time"
  24. "golang.org/x/net/context"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/credentials"
  27. )
  28. var (
  29. ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
  30. )
  31. // Client provides and manages an etcd v3 client session.
  32. type Client struct {
  33. Cluster
  34. KV
  35. Lease
  36. Watcher
  37. Auth
  38. Maintenance
  39. conn *grpc.ClientConn
  40. cfg Config
  41. creds *credentials.TransportAuthenticator
  42. mu sync.RWMutex // protects connection selection and error list
  43. errors []error // errors passed to retryConnection
  44. ctx context.Context
  45. cancel context.CancelFunc
  46. // fields below are managed by connMonitor
  47. // reconnc accepts writes which signal the client should reconnect
  48. reconnc chan error
  49. // newconnc is closed on successful connect and set to a fresh channel
  50. newconnc chan struct{}
  51. lastConnErr error
  52. }
  53. // New creates a new etcdv3 client from a given configuration.
  54. func New(cfg Config) (*Client, error) {
  55. if cfg.RetryDialer == nil {
  56. cfg.RetryDialer = dialEndpointList
  57. }
  58. if len(cfg.Endpoints) == 0 {
  59. return nil, ErrNoAvailableEndpoints
  60. }
  61. return newClient(&cfg)
  62. }
  63. // NewFromURL creates a new etcdv3 client from a URL.
  64. func NewFromURL(url string) (*Client, error) {
  65. return New(Config{Endpoints: []string{url}})
  66. }
  67. // NewFromConfigFile creates a new etcdv3 client from a configuration file.
  68. func NewFromConfigFile(path string) (*Client, error) {
  69. cfg, err := configFromFile(path)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return New(*cfg)
  74. }
  75. // Close shuts down the client's etcd connections.
  76. func (c *Client) Close() error {
  77. c.mu.Lock()
  78. defer c.mu.Unlock()
  79. if c.cancel == nil {
  80. return nil
  81. }
  82. c.cancel()
  83. c.cancel = nil
  84. connc := c.newconnc
  85. c.mu.Unlock()
  86. c.connStartRetry(nil)
  87. c.Watcher.Close()
  88. c.Lease.Close()
  89. <-connc
  90. c.mu.Lock()
  91. if c.lastConnErr != c.ctx.Err() {
  92. return c.lastConnErr
  93. }
  94. return nil
  95. }
  96. // Ctx is a context for "out of band" messages (e.g., for sending
  97. // "clean up" message when another context is canceled). It is
  98. // canceled on client Close().
  99. func (c *Client) Ctx() context.Context { return c.ctx }
  100. // Endpoints lists the registered endpoints for the client.
  101. func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
  102. // Errors returns all errors that have been observed since called last.
  103. func (c *Client) Errors() (errs []error) {
  104. c.mu.Lock()
  105. defer c.mu.Unlock()
  106. errs = c.errors
  107. c.errors = nil
  108. return errs
  109. }
  110. // Dial establishes a connection for a given endpoint using the client's config
  111. func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
  112. opts := []grpc.DialOption{
  113. grpc.WithBlock(),
  114. grpc.WithTimeout(c.cfg.DialTimeout),
  115. }
  116. if c.creds != nil {
  117. opts = append(opts, grpc.WithTransportCredentials(*c.creds))
  118. } else {
  119. opts = append(opts, grpc.WithInsecure())
  120. }
  121. proto := "tcp"
  122. if url, uerr := url.Parse(endpoint); uerr == nil && url.Scheme == "unix" {
  123. proto = "unix"
  124. // strip unix:// prefix so certs work
  125. endpoint = url.Host
  126. }
  127. f := func(a string, t time.Duration) (net.Conn, error) {
  128. select {
  129. case <-c.ctx.Done():
  130. return nil, c.ctx.Err()
  131. default:
  132. }
  133. return net.DialTimeout(proto, a, t)
  134. }
  135. opts = append(opts, grpc.WithDialer(f))
  136. conn, err := grpc.Dial(endpoint, opts...)
  137. if err != nil {
  138. return nil, err
  139. }
  140. return conn, nil
  141. }
  142. func newClient(cfg *Config) (*Client, error) {
  143. if cfg == nil {
  144. cfg = &Config{RetryDialer: dialEndpointList}
  145. }
  146. var creds *credentials.TransportAuthenticator
  147. if cfg.TLS != nil {
  148. c := credentials.NewTLS(cfg.TLS)
  149. creds = &c
  150. }
  151. // use a temporary skeleton client to bootstrap first connection
  152. ctx, cancel := context.WithCancel(context.TODO())
  153. conn, err := cfg.RetryDialer(&Client{cfg: *cfg, creds: creds, ctx: ctx})
  154. if err != nil {
  155. return nil, err
  156. }
  157. client := &Client{
  158. conn: conn,
  159. cfg: *cfg,
  160. creds: creds,
  161. ctx: ctx,
  162. cancel: cancel,
  163. reconnc: make(chan error),
  164. newconnc: make(chan struct{}),
  165. }
  166. go client.connMonitor()
  167. client.Cluster = NewCluster(client)
  168. client.KV = NewKV(client)
  169. client.Lease = NewLease(client)
  170. client.Watcher = NewWatcher(client)
  171. client.Auth = NewAuth(client)
  172. client.Maintenance = NewMaintenance(client)
  173. if cfg.Logger != nil {
  174. logger.Set(cfg.Logger)
  175. } else {
  176. // disable client side grpc by default
  177. logger.Set(log.New(ioutil.Discard, "", 0))
  178. }
  179. return client, nil
  180. }
  181. // ActiveConnection returns the current in-use connection
  182. func (c *Client) ActiveConnection() *grpc.ClientConn {
  183. c.mu.RLock()
  184. defer c.mu.RUnlock()
  185. return c.conn
  186. }
  187. // retryConnection establishes a new connection
  188. func (c *Client) retryConnection(err error) (newConn *grpc.ClientConn, dialErr error) {
  189. c.mu.Lock()
  190. defer c.mu.Unlock()
  191. if err != nil {
  192. c.errors = append(c.errors, err)
  193. }
  194. if c.conn != nil {
  195. c.conn.Close()
  196. if st, _ := c.conn.State(); st != grpc.Shutdown {
  197. // wait so grpc doesn't leak sleeping goroutines
  198. c.conn.WaitForStateChange(context.Background(), st)
  199. }
  200. }
  201. if c.cancel == nil {
  202. // client has called Close() so don't try to dial out
  203. return nil, c.ctx.Err()
  204. }
  205. c.conn, dialErr = c.cfg.RetryDialer(c)
  206. if dialErr != nil {
  207. c.errors = append(c.errors, dialErr)
  208. }
  209. return c.conn, dialErr
  210. }
  211. // connStartRetry schedules a reconnect if one is not already running
  212. func (c *Client) connStartRetry(err error) {
  213. select {
  214. case c.reconnc <- err:
  215. default:
  216. }
  217. }
  218. // connWait waits for a reconnect to be processed
  219. func (c *Client) connWait(ctx context.Context, err error) (*grpc.ClientConn, error) {
  220. c.mu.Lock()
  221. ch := c.newconnc
  222. c.mu.Unlock()
  223. c.connStartRetry(err)
  224. select {
  225. case <-ctx.Done():
  226. return nil, ctx.Err()
  227. case <-ch:
  228. }
  229. c.mu.Lock()
  230. defer c.mu.Unlock()
  231. return c.conn, c.lastConnErr
  232. }
  233. // connMonitor monitors the connection and handles retries
  234. func (c *Client) connMonitor() {
  235. var err error
  236. for {
  237. select {
  238. case err = <-c.reconnc:
  239. case <-c.ctx.Done():
  240. _, err = c.retryConnection(c.ctx.Err())
  241. c.mu.Lock()
  242. c.lastConnErr = err
  243. close(c.newconnc)
  244. c.mu.Unlock()
  245. return
  246. }
  247. conn, connErr := c.retryConnection(err)
  248. c.mu.Lock()
  249. c.lastConnErr = connErr
  250. c.conn = conn
  251. close(c.newconnc)
  252. c.newconnc = make(chan struct{})
  253. c.mu.Unlock()
  254. }
  255. }
  256. // dialEndpointList attempts to connect to each endpoint in order until a
  257. // connection is established.
  258. func dialEndpointList(c *Client) (*grpc.ClientConn, error) {
  259. var err error
  260. for _, ep := range c.Endpoints() {
  261. conn, curErr := c.Dial(ep)
  262. if curErr != nil {
  263. err = curErr
  264. } else {
  265. return conn, nil
  266. }
  267. }
  268. return nil, err
  269. }
  270. // isHaltErr returns true if the given error and context indicate no forward
  271. // progress can be made, even after reconnecting.
  272. func isHaltErr(ctx context.Context, err error) bool {
  273. isRPCError := strings.HasPrefix(grpc.ErrorDesc(err), "etcdserver: ")
  274. return isRPCError || ctx.Err() != nil
  275. }