grpclb_util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpc
  19. import (
  20. "google.golang.org/grpc/balancer"
  21. "google.golang.org/grpc/resolver"
  22. )
  23. // The parent ClientConn should re-resolve when grpclb loses connection to the
  24. // remote balancer. When the ClientConn inside grpclb gets a TransientFailure,
  25. // it calls lbManualResolver.ResolveNow(), which calls parent ClientConn's
  26. // ResolveNow, and eventually results in re-resolve happening in parent
  27. // ClientConn's resolver (DNS for example).
  28. //
  29. // parent
  30. // ClientConn
  31. // +-----------------------------------------------------------------+
  32. // | parent +---------------------------------+ |
  33. // | DNS ClientConn | grpclb | |
  34. // | resolver balancerWrapper | | |
  35. // | + + | grpclb grpclb | |
  36. // | | | | ManualResolver ClientConn | |
  37. // | | | | + + | |
  38. // | | | | | | Transient | |
  39. // | | | | | | Failure | |
  40. // | | | | | <--------- | | |
  41. // | | | <--------------- | ResolveNow | | |
  42. // | | <--------- | ResolveNow | | | | |
  43. // | | ResolveNow | | | | | |
  44. // | | | | | | | |
  45. // | + + | + + | |
  46. // | +---------------------------------+ |
  47. // +-----------------------------------------------------------------+
  48. // lbManualResolver is used by the ClientConn inside grpclb. It's a manual
  49. // resolver with a special ResolveNow() function.
  50. //
  51. // When ResolveNow() is called, it calls ResolveNow() on the parent ClientConn,
  52. // so when grpclb client lose contact with remote balancers, the parent
  53. // ClientConn's resolver will re-resolve.
  54. type lbManualResolver struct {
  55. scheme string
  56. ccr resolver.ClientConn
  57. ccb balancer.ClientConn
  58. }
  59. func (r *lbManualResolver) Build(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOption) (resolver.Resolver, error) {
  60. r.ccr = cc
  61. return r, nil
  62. }
  63. func (r *lbManualResolver) Scheme() string {
  64. return r.scheme
  65. }
  66. // ResolveNow calls resolveNow on the parent ClientConn.
  67. func (r *lbManualResolver) ResolveNow(o resolver.ResolveNowOption) {
  68. r.ccb.ResolveNow(o)
  69. }
  70. // Close is a noop for Resolver.
  71. func (*lbManualResolver) Close() {}
  72. // NewAddress calls cc.NewAddress.
  73. func (r *lbManualResolver) NewAddress(addrs []resolver.Address) {
  74. r.ccr.NewAddress(addrs)
  75. }
  76. // NewServiceConfig calls cc.NewServiceConfig.
  77. func (r *lbManualResolver) NewServiceConfig(sc string) {
  78. r.ccr.NewServiceConfig(sc)
  79. }