client.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  17. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. )
  19. type Client struct {
  20. // KV is the keyvalue API for the client's connection.
  21. KV pb.KVClient
  22. // Lease is the lease API for the client's connection.
  23. Lease pb.LeaseClient
  24. // Watch is the watch API for the client's connection.
  25. Watch pb.WatchClient
  26. // Cluster is the cluster API for the client's connection.
  27. Cluster pb.ClusterClient
  28. conn *grpc.ClientConn
  29. cfg Config
  30. }
  31. type Config struct {
  32. // Endpoints is a list of URLs
  33. Endpoints []string
  34. // TODO TLS options
  35. }
  36. // New creates a new etcdv3 client from a given configuration.
  37. func New(cfg Config) (*Client, error) {
  38. conn, err := cfg.dialEndpoints()
  39. if err != nil {
  40. return nil, err
  41. }
  42. client := newClient(conn)
  43. client.cfg = cfg
  44. return client, nil
  45. }
  46. // NewFromURL creates a new etcdv3 client from a URL.
  47. func NewFromURL(url string) (*Client, error) {
  48. return New(Config{Endpoints: []string{url}})
  49. }
  50. // NewFromConn creates a new etcdv3 client from an established grpc Connection.
  51. func NewFromConn(conn *grpc.ClientConn) *Client {
  52. return newClient(conn)
  53. }
  54. // Clone creates a copy of client with the old connection and new API clients.
  55. func (c *Client) Clone() *Client {
  56. cl := newClient(c.conn)
  57. cl.cfg = c.cfg
  58. return cl
  59. }
  60. // Close shuts down the client's etcd connections.
  61. func (c *Client) Close() error {
  62. return c.conn.Close()
  63. }
  64. func newClient(conn *grpc.ClientConn) *Client {
  65. return &Client{
  66. KV: pb.NewKVClient(conn),
  67. Lease: pb.NewLeaseClient(conn),
  68. Watch: pb.NewWatchClient(conn),
  69. Cluster: pb.NewClusterClient(conn),
  70. conn: conn,
  71. }
  72. }
  73. func (cfg *Config) dialEndpoints() (*grpc.ClientConn, error) {
  74. var err error
  75. for _, ep := range cfg.Endpoints {
  76. // TODO: enable grpc.WithTransportCredentials(creds)
  77. conn, curErr := grpc.Dial(ep, grpc.WithInsecure())
  78. if err != nil {
  79. err = curErr
  80. } else {
  81. return conn, nil
  82. }
  83. }
  84. return nil, err
  85. }