2016-01-27 20:59:47 +03:00
|
|
|
// Copyright 2016 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package clientv3
|
|
|
|
|
|
|
|
import (
|
2016-02-02 22:01:58 +03:00
|
|
|
"errors"
|
2016-01-31 06:14:36 +03:00
|
|
|
"net"
|
|
|
|
"net/url"
|
2016-02-06 02:13:24 +03:00
|
|
|
"strings"
|
2016-01-28 06:23:26 +03:00
|
|
|
"sync"
|
2016-01-28 23:54:03 +03:00
|
|
|
"time"
|
2016-01-28 06:23:26 +03:00
|
|
|
|
2016-01-27 20:59:47 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
|
2016-01-29 03:09:14 +03:00
|
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/credentials"
|
|
|
|
"github.com/coreos/etcd/pkg/transport"
|
2016-01-27 20:59:47 +03:00
|
|
|
)
|
|
|
|
|
2016-02-02 22:01:58 +03:00
|
|
|
var (
|
|
|
|
ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
|
|
|
|
)
|
|
|
|
|
2016-01-28 06:23:26 +03:00
|
|
|
// Client provides and manages an etcd v3 client session.
|
2016-01-27 20:59:47 +03:00
|
|
|
type Client struct {
|
2016-02-25 09:33:28 +03:00
|
|
|
Cluster
|
|
|
|
KV
|
|
|
|
Lease
|
|
|
|
Watcher
|
2016-01-27 20:59:47 +03:00
|
|
|
|
2016-01-28 06:23:26 +03:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
cfg Config
|
2016-01-29 03:09:14 +03:00
|
|
|
creds *credentials.TransportAuthenticator
|
2016-01-28 06:23:26 +03:00
|
|
|
mu sync.RWMutex // protects connection selection and error list
|
|
|
|
errors []error // errors passed to retryConnection
|
2016-01-27 20:59:47 +03:00
|
|
|
}
|
|
|
|
|
2016-01-28 06:23:26 +03:00
|
|
|
// EndpointDialer is a policy for choosing which endpoint to dial next
|
|
|
|
type EndpointDialer func(*Client) (*grpc.ClientConn, error)
|
|
|
|
|
2016-01-27 20:59:47 +03:00
|
|
|
type Config struct {
|
|
|
|
// Endpoints is a list of URLs
|
|
|
|
Endpoints []string
|
|
|
|
|
2016-01-28 06:23:26 +03:00
|
|
|
// RetryDialer chooses the next endpoint to use
|
|
|
|
RetryDialer EndpointDialer
|
|
|
|
|
2016-01-28 23:54:03 +03:00
|
|
|
// DialTimeout is the timeout for failing to establish a connection.
|
|
|
|
DialTimeout time.Duration
|
|
|
|
|
2016-01-29 03:09:14 +03:00
|
|
|
// TLS holds the client secure credentials, if any.
|
|
|
|
TLS *transport.TLSInfo
|
2016-01-27 20:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new etcdv3 client from a given configuration.
|
|
|
|
func New(cfg Config) (*Client, error) {
|
2016-01-28 06:23:26 +03:00
|
|
|
if cfg.RetryDialer == nil {
|
|
|
|
cfg.RetryDialer = dialEndpointList
|
|
|
|
}
|
2016-02-02 22:01:58 +03:00
|
|
|
if len(cfg.Endpoints) == 0 {
|
|
|
|
return nil, ErrNoAvailableEndpoints
|
|
|
|
}
|
|
|
|
|
2016-01-31 06:14:36 +03:00
|
|
|
return newClient(&cfg)
|
2016-01-27 20:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFromURL creates a new etcdv3 client from a URL.
|
|
|
|
func NewFromURL(url string) (*Client, error) {
|
|
|
|
return New(Config{Endpoints: []string{url}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down the client's etcd connections.
|
|
|
|
func (c *Client) Close() error {
|
2016-02-25 09:33:28 +03:00
|
|
|
c.Watcher.Close()
|
|
|
|
c.Lease.Close()
|
2016-01-27 20:59:47 +03:00
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:23:26 +03:00
|
|
|
// Endpoints lists the registered endpoints for the client.
|
|
|
|
func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
|
|
|
|
|
|
|
|
// Errors returns all errors that have been observed since called last.
|
|
|
|
func (c *Client) Errors() (errs []error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
errs = c.errors
|
|
|
|
c.errors = nil
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial establishes a connection for a given endpoint using the client's config
|
|
|
|
func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
|
2016-01-29 03:09:14 +03:00
|
|
|
opts := []grpc.DialOption{
|
2016-01-28 23:54:03 +03:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithTimeout(c.cfg.DialTimeout),
|
2016-01-29 03:09:14 +03:00
|
|
|
}
|
|
|
|
if c.creds != nil {
|
|
|
|
opts = append(opts, grpc.WithTransportCredentials(*c.creds))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, grpc.WithInsecure())
|
|
|
|
}
|
2016-01-31 06:14:36 +03:00
|
|
|
if url, uerr := url.Parse(endpoint); uerr == nil && url.Scheme == "unix" {
|
|
|
|
f := func(a string, t time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", a, t)
|
|
|
|
}
|
|
|
|
// strip unix:// prefix so certs work
|
|
|
|
endpoint = url.Host
|
|
|
|
opts = append(opts, grpc.WithDialer(f))
|
|
|
|
}
|
|
|
|
|
2016-01-29 03:09:14 +03:00
|
|
|
conn, err := grpc.Dial(endpoint, opts...)
|
2016-01-28 23:54:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
2016-01-28 06:23:26 +03:00
|
|
|
}
|
|
|
|
|
2016-01-31 06:14:36 +03:00
|
|
|
func newClient(cfg *Config) (*Client, error) {
|
2016-01-28 06:23:26 +03:00
|
|
|
if cfg == nil {
|
|
|
|
cfg = &Config{RetryDialer: dialEndpointList}
|
|
|
|
}
|
2016-01-29 03:09:14 +03:00
|
|
|
var creds *credentials.TransportAuthenticator
|
|
|
|
if cfg.TLS != nil {
|
|
|
|
tlscfg, err := cfg.TLS.ClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := credentials.NewTLS(tlscfg)
|
|
|
|
creds = &c
|
|
|
|
}
|
2016-01-31 06:14:36 +03:00
|
|
|
// use a temporary skeleton client to bootstrap first connection
|
|
|
|
conn, err := cfg.RetryDialer(&Client{cfg: *cfg, creds: creds})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-25 09:33:28 +03:00
|
|
|
client := &Client{
|
|
|
|
conn: conn,
|
|
|
|
cfg: *cfg,
|
|
|
|
creds: creds,
|
|
|
|
}
|
|
|
|
client.Cluster = NewCluster(client)
|
|
|
|
client.KV = NewKV(client)
|
|
|
|
client.Lease = NewLease(client)
|
|
|
|
client.Watcher = NewWatcher(client)
|
|
|
|
|
|
|
|
return client, nil
|
2016-01-28 06:23:26 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 16:05:03 +03:00
|
|
|
// ActiveConnection returns the current in-use connection
|
2016-01-29 03:09:14 +03:00
|
|
|
func (c *Client) ActiveConnection() *grpc.ClientConn {
|
2016-01-28 06:23:26 +03:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
|
|
|
return c.conn
|
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// retryConnection establishes a new connection
|
2016-01-28 08:13:23 +03:00
|
|
|
func (c *Client) retryConnection(oldConn *grpc.ClientConn, err error) (*grpc.ClientConn, error) {
|
2016-01-28 06:23:26 +03:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
c.errors = append(c.errors, err)
|
|
|
|
}
|
|
|
|
if oldConn != c.conn {
|
|
|
|
// conn has already been updated
|
2016-01-28 08:13:23 +03:00
|
|
|
return c.conn, nil
|
2016-01-28 06:23:26 +03:00
|
|
|
}
|
|
|
|
conn, dialErr := c.cfg.RetryDialer(c)
|
|
|
|
if dialErr != nil {
|
|
|
|
c.errors = append(c.errors, dialErr)
|
2016-01-28 08:13:23 +03:00
|
|
|
return nil, dialErr
|
2016-01-27 20:59:47 +03:00
|
|
|
}
|
2016-01-28 06:23:26 +03:00
|
|
|
c.conn = conn
|
2016-01-28 08:13:23 +03:00
|
|
|
return c.conn, nil
|
2016-01-27 20:59:47 +03:00
|
|
|
}
|
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// dialEndpointList attempts to connect to each endpoint in order until a
|
2016-01-28 06:23:26 +03:00
|
|
|
// connection is established.
|
|
|
|
func dialEndpointList(c *Client) (*grpc.ClientConn, error) {
|
2016-01-27 20:59:47 +03:00
|
|
|
var err error
|
2016-01-28 06:23:26 +03:00
|
|
|
for _, ep := range c.Endpoints() {
|
|
|
|
conn, curErr := c.Dial(ep)
|
2016-01-28 23:54:03 +03:00
|
|
|
if curErr != nil {
|
2016-01-27 20:59:47 +03:00
|
|
|
err = curErr
|
|
|
|
} else {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-28 08:13:23 +03:00
|
|
|
|
|
|
|
func isRPCError(err error) bool {
|
2016-02-06 02:13:24 +03:00
|
|
|
return strings.HasPrefix(grpc.ErrorDesc(err), "etcdserver: ")
|
2016-01-28 08:13:23 +03:00
|
|
|
}
|