client.go 8.0 KB

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