|
| 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 | + |
| 19 | +package grpc |
| 20 | + |
| 21 | +import ( |
| 22 | + "google.golang.org/grpc/balancer" |
| 23 | + "google.golang.org/grpc/resolver" |
| 24 | +) |
| 25 | + |
| 26 | +// The parent ClientConn should re-resolve when grpclb loses connection to the |
| 27 | +// remote balancer. When the ClientConn inside grpclb gets a TransientFailure, |
| 28 | +// it calls lbManualResolver.ResolveNow(), which calls parent ClientConn's |
| 29 | +// ResolveNow, and eventually results in re-resolve happening in parent |
| 30 | +// ClientConn's resolver (DNS for example). |
| 31 | +// |
| 32 | +// parent |
| 33 | +// ClientConn |
| 34 | +// +-----------------------------------------------------------------+ |
| 35 | +// | parent +---------------------------------+ | |
| 36 | +// | DNS ClientConn | grpclb | | |
| 37 | +// | resolver balancerWrapper | | | |
| 38 | +// | + + | grpclb grpclb | | |
| 39 | +// | | | | ManualResolver ClientConn | | |
| 40 | +// | | | | + + | | |
| 41 | +// | | | | | | Transient | | |
| 42 | +// | | | | | | Failure | | |
| 43 | +// | | | | | <--------- | | | |
| 44 | +// | | | <--------------- | ResolveNow | | | |
| 45 | +// | | <--------- | ResolveNow | | | | | |
| 46 | +// | | ResolveNow | | | | | | |
| 47 | +// | | | | | | | | |
| 48 | +// | + + | + + | | |
| 49 | +// | +---------------------------------+ | |
| 50 | +// +-----------------------------------------------------------------+ |
| 51 | + |
| 52 | +// lbManualResolver is used by the ClientConn inside grpclb. It's a manual |
| 53 | +// resolver with a special ResolveNow() function. |
| 54 | +// |
| 55 | +// When ResolveNow() is called, it calls ResolveNow() on the parent ClientConn, |
| 56 | +// so when grpclb client lose contact with remote balancers, the parent |
| 57 | +// ClientConn's resolver will re-resolve. |
| 58 | +type lbManualResolver struct { |
| 59 | + scheme string |
| 60 | + ccr resolver.ClientConn |
| 61 | + |
| 62 | + ccb balancer.ClientConn |
| 63 | +} |
| 64 | + |
| 65 | +func (r *lbManualResolver) Build(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOption) (resolver.Resolver, error) { |
| 66 | + r.ccr = cc |
| 67 | + return r, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (r *lbManualResolver) Scheme() string { |
| 71 | + return r.scheme |
| 72 | +} |
| 73 | + |
| 74 | +// ResolveNow calls resolveNow on the parent ClientConn. |
| 75 | +func (r *lbManualResolver) ResolveNow(o resolver.ResolveNowOption) { |
| 76 | + r.ccb.ResolveNow(o) |
| 77 | +} |
| 78 | + |
| 79 | +// Close is a noop for Resolver. |
| 80 | +func (*lbManualResolver) Close() {} |
| 81 | + |
| 82 | +// NewAddress calls cc.NewAddress. |
| 83 | +func (r *lbManualResolver) NewAddress(addrs []resolver.Address) { |
| 84 | + r.ccr.NewAddress(addrs) |
| 85 | +} |
| 86 | + |
| 87 | +// NewServiceConfig calls cc.NewServiceConfig. |
| 88 | +func (r *lbManualResolver) NewServiceConfig(sc string) { |
| 89 | + r.ccr.NewServiceConfig(sc) |
| 90 | +} |
0 commit comments