cluster_proxy.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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]grpcClientProxy = make(map[*clientv3.Client]grpcClientProxy)
  25. )
  26. type grpcClientProxy struct {
  27. grpc grpcAPI
  28. wdonec <-chan struct{}
  29. kvdonec <-chan struct{}
  30. lpdonec <-chan struct{}
  31. }
  32. func toGRPC(c *clientv3.Client) grpcAPI {
  33. pmu.Lock()
  34. defer pmu.Unlock()
  35. if v, ok := proxies[c]; ok {
  36. return v.grpc
  37. }
  38. kvp, kvpch := grpcproxy.NewKvProxy(c)
  39. wp, wpch := grpcproxy.NewWatchProxy(c)
  40. lp, lpch := grpcproxy.NewLeaseProxy(c)
  41. grpc := grpcAPI{
  42. pb.NewClusterClient(c.ActiveConnection()),
  43. grpcproxy.KvServerToKvClient(kvp),
  44. grpcproxy.LeaseServerToLeaseClient(lp),
  45. grpcproxy.WatchServerToWatchClient(wp),
  46. pb.NewMaintenanceClient(c.ActiveConnection()),
  47. pb.NewAuthClient(c.ActiveConnection()),
  48. }
  49. proxies[c] = grpcClientProxy{grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch}
  50. return grpc
  51. }
  52. type proxyCloser struct {
  53. clientv3.Watcher
  54. wdonec <-chan struct{}
  55. kvdonec <-chan struct{}
  56. lpdonec <-chan struct{}
  57. }
  58. func (pc *proxyCloser) Close() error {
  59. // client ctx is canceled before calling close, so kv and lp will close out
  60. <-pc.kvdonec
  61. err := pc.Watcher.Close()
  62. <-pc.wdonec
  63. <-pc.lpdonec
  64. return err
  65. }
  66. func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
  67. c, err := clientv3.New(cfg)
  68. if err != nil {
  69. return nil, err
  70. }
  71. rpc := toGRPC(c)
  72. c.KV = clientv3.NewKVFromKVClient(rpc.KV)
  73. pmu.Lock()
  74. c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, cfg.DialTimeout)
  75. c.Watcher = &proxyCloser{
  76. Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch),
  77. wdonec: proxies[c].wdonec,
  78. kvdonec: proxies[c].kvdonec,
  79. lpdonec: proxies[c].lpdonec,
  80. }
  81. pmu.Unlock()
  82. return c, nil
  83. }