cluster_proxy.go 2.8 KB

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