mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
clientv3: support unix endpoints
This commit is contained in:
parent
eb8ab3ace4
commit
b74a42b286
@ -15,6 +15,8 @@
|
|||||||
package clientv3
|
package clientv3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -65,12 +67,7 @@ func New(cfg Config) (*Client, error) {
|
|||||||
if cfg.RetryDialer == nil {
|
if cfg.RetryDialer == nil {
|
||||||
cfg.RetryDialer = dialEndpointList
|
cfg.RetryDialer = dialEndpointList
|
||||||
}
|
}
|
||||||
// use a temporary skeleton client to bootstrap first connection
|
return newClient(&cfg)
|
||||||
conn, err := cfg.RetryDialer(&Client{cfg: cfg})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return newClient(conn, &cfg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFromURL creates a new etcdv3 client from a URL.
|
// NewFromURL creates a new etcdv3 client from a URL.
|
||||||
@ -78,12 +75,6 @@ func NewFromURL(url string) (*Client, error) {
|
|||||||
return New(Config{Endpoints: []string{url}})
|
return New(Config{Endpoints: []string{url}})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFromConn creates a new etcdv3 client from an established grpc Connection.
|
|
||||||
func NewFromConn(conn *grpc.ClientConn) *Client { return mustNewClient(conn, nil) }
|
|
||||||
|
|
||||||
// Clone creates a copy of client with the old connection and new API clients.
|
|
||||||
func (c *Client) Clone() *Client { return mustNewClient(c.conn, &c.cfg) }
|
|
||||||
|
|
||||||
// Close shuts down the client's etcd connections.
|
// Close shuts down the client's etcd connections.
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
@ -112,6 +103,15 @@ func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
|
|||||||
} else {
|
} else {
|
||||||
opts = append(opts, grpc.WithInsecure())
|
opts = append(opts, grpc.WithInsecure())
|
||||||
}
|
}
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
conn, err := grpc.Dial(endpoint, opts...)
|
conn, err := grpc.Dial(endpoint, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -119,15 +119,7 @@ func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustNewClient(conn *grpc.ClientConn, cfg *Config) *Client {
|
func newClient(cfg *Config) (*Client, error) {
|
||||||
c, err := newClient(conn, cfg)
|
|
||||||
if err != nil {
|
|
||||||
panic("expected no error")
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func newClient(conn *grpc.ClientConn, cfg *Config) (*Client, error) {
|
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
cfg = &Config{RetryDialer: dialEndpointList}
|
cfg = &Config{RetryDialer: dialEndpointList}
|
||||||
}
|
}
|
||||||
@ -140,6 +132,11 @@ func newClient(conn *grpc.ClientConn, cfg *Config) (*Client, error) {
|
|||||||
c := credentials.NewTLS(tlscfg)
|
c := credentials.NewTLS(tlscfg)
|
||||||
creds = &c
|
creds = &c
|
||||||
}
|
}
|
||||||
|
// use a temporary skeleton client to bootstrap first connection
|
||||||
|
conn, err := cfg.RetryDialer(&Client{cfg: *cfg, creds: creds})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Client{
|
return &Client{
|
||||||
KV: pb.NewKVClient(conn),
|
KV: pb.NewKVClient(conn),
|
||||||
Lease: pb.NewLeaseClient(conn),
|
Lease: pb.NewLeaseClient(conn),
|
||||||
|
@ -31,7 +31,6 @@ import (
|
|||||||
|
|
||||||
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
||||||
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
|
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
|
||||||
"github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/credentials"
|
|
||||||
|
|
||||||
"github.com/coreos/etcd/client"
|
"github.com/coreos/etcd/client"
|
||||||
"github.com/coreos/etcd/clientv3"
|
"github.com/coreos/etcd/clientv3"
|
||||||
@ -445,6 +444,7 @@ func (m *member) listenGRPC() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("listen failed on grpc socket %s (%v)", m.grpcAddr, err)
|
return fmt.Errorf("listen failed on grpc socket %s (%v)", m.grpcAddr, err)
|
||||||
}
|
}
|
||||||
|
m.grpcAddr = "unix://" + m.grpcAddr
|
||||||
m.grpcListener = l
|
m.grpcListener = l
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -454,29 +454,12 @@ func NewClientV3(m *member) (*clientv3.Client, error) {
|
|||||||
if m.grpcAddr == "" {
|
if m.grpcAddr == "" {
|
||||||
return nil, fmt.Errorf("member not configured for grpc")
|
return nil, fmt.Errorf("member not configured for grpc")
|
||||||
}
|
}
|
||||||
f := func(a string, t time.Duration) (net.Conn, error) {
|
cfg := clientv3.Config{
|
||||||
return net.Dial("unix", a)
|
Endpoints: []string{m.grpcAddr},
|
||||||
|
DialTimeout: 5 * time.Second,
|
||||||
|
TLS: m.ClientTLSInfo,
|
||||||
}
|
}
|
||||||
unixdialer := grpc.WithDialer(f)
|
return clientv3.New(cfg)
|
||||||
opts := []grpc.DialOption{
|
|
||||||
unixdialer,
|
|
||||||
grpc.WithBlock(),
|
|
||||||
grpc.WithTimeout(5 * time.Second)}
|
|
||||||
if m.ClientTLSInfo != nil {
|
|
||||||
tlscfg, err := m.ClientTLSInfo.ClientConfig()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
creds := credentials.NewTLS(tlscfg)
|
|
||||||
opts = append(opts, grpc.WithTransportCredentials(creds))
|
|
||||||
} else {
|
|
||||||
opts = append(opts, grpc.WithInsecure())
|
|
||||||
}
|
|
||||||
conn, err := grpc.Dial(m.grpcAddr, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return clientv3.NewFromConn(conn), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone returns a member with the same server configuration. The returned
|
// Clone returns a member with the same server configuration. The returned
|
||||||
|
@ -58,7 +58,7 @@ func mustCreateClients(totalClients, totalConns uint) []*clientv3.Client {
|
|||||||
|
|
||||||
clients := make([]*clientv3.Client, totalClients)
|
clients := make([]*clientv3.Client, totalClients)
|
||||||
for i := range clients {
|
for i := range clients {
|
||||||
clients[i] = conns[i%int(totalConns)].Clone()
|
clients[i] = conns[i%int(totalConns)]
|
||||||
}
|
}
|
||||||
return clients
|
return clients
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user