cluster_proxy.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // +build cluster_proxy
  15. package integration
  16. import (
  17. "sync"
  18. "github.com/coreos/etcd/clientv3"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/proxy/grpcproxy"
  21. )
  22. var (
  23. pmu sync.Mutex
  24. proxies map[*clientv3.Client]grpcAPI = make(map[*clientv3.Client]grpcAPI)
  25. )
  26. func toGRPC(c *clientv3.Client) grpcAPI {
  27. pmu.Lock()
  28. defer pmu.Unlock()
  29. if v, ok := proxies[c]; ok {
  30. return v
  31. }
  32. api := grpcAPI{
  33. pb.NewClusterClient(c.ActiveConnection()),
  34. grpcproxy.KvServerToKvClient(grpcproxy.NewKvProxy(c)),
  35. pb.NewLeaseClient(c.ActiveConnection()),
  36. grpcproxy.WatchServerToWatchClient(grpcproxy.NewWatchProxy(c)),
  37. pb.NewMaintenanceClient(c.ActiveConnection()),
  38. }
  39. proxies[c] = api
  40. return api
  41. }
  42. func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
  43. c, err := clientv3.New(cfg)
  44. if err != nil {
  45. return nil, err
  46. }
  47. toGRPC(c)
  48. c.KV = clientv3.NewKVFromKVClient(grpcproxy.KvServerToKvClient(grpcproxy.NewKvProxy(c)))
  49. c.Watcher = clientv3.NewWatchFromWatchClient(grpcproxy.WatchServerToWatchClient(grpcproxy.NewWatchProxy(c)))
  50. return c, nil
  51. }