cluster_proxy.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. mp := grpcproxy.NewMaintenanceProxy(c)
  50. clp, _ := grpcproxy.NewClusterProxy(c, "", "") // without registering proxy URLs
  51. grpc := grpcAPI{
  52. adapter.ClusterServerToClusterClient(clp),
  53. adapter.KvServerToKvClient(kvp),
  54. adapter.LeaseServerToLeaseClient(lp),
  55. adapter.WatchServerToWatchClient(wp),
  56. adapter.MaintenanceServerToMaintenanceClient(mp),
  57. pb.NewAuthClient(c.ActiveConnection()),
  58. }
  59. proxies[c] = grpcClientProxy{grpc: grpc, wdonec: wpch, kvdonec: kvpch, lpdonec: lpch}
  60. return grpc
  61. }
  62. type proxyCloser struct {
  63. clientv3.Watcher
  64. wdonec <-chan struct{}
  65. kvdonec <-chan struct{}
  66. lpdonec <-chan struct{}
  67. }
  68. func (pc *proxyCloser) Close() error {
  69. // client ctx is canceled before calling close, so kv and lp will close out
  70. <-pc.kvdonec
  71. err := pc.Watcher.Close()
  72. <-pc.wdonec
  73. <-pc.lpdonec
  74. return err
  75. }
  76. func newClientV3(cfg clientv3.Config) (*clientv3.Client, error) {
  77. c, err := clientv3.New(cfg)
  78. if err != nil {
  79. return nil, err
  80. }
  81. rpc := toGRPC(c)
  82. c.KV = clientv3.NewKVFromKVClient(rpc.KV)
  83. pmu.Lock()
  84. c.Lease = clientv3.NewLeaseFromLeaseClient(rpc.Lease, cfg.DialTimeout)
  85. c.Watcher = &proxyCloser{
  86. Watcher: clientv3.NewWatchFromWatchClient(rpc.Watch),
  87. wdonec: proxies[c].wdonec,
  88. kvdonec: proxies[c].kvdonec,
  89. lpdonec: proxies[c].lpdonec,
  90. }
  91. pmu.Unlock()
  92. return c, nil
  93. }