cluster_proxy.go 2.5 KB

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