Skip to content

Commit 9b88f99

Browse files
committed
use the existing manual resolver
1 parent 69ecafe commit 9b88f99

File tree

2 files changed

+34
-44
lines changed

2 files changed

+34
-44
lines changed

balancer/grpclb/grpclb.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ func (b *lbBuilder) Name() string {
132132
}
133133

134134
func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
135-
// This generates a manual resolver builder with a fixed scheme. This
136-
// scheme will be used to dial to remote LB, so we can send filtered
137-
// address updates to remote LB ClientConn using this manual resolver.
138-
r := &lbManualResolver{scheme: "grpclb-internal", ccb: cc}
139-
140135
lb := &lbBalancer{
141136
cc: newLBCacheClientConn(cc),
142137
dialTarget: opt.Target.Endpoint(),
@@ -145,7 +140,10 @@ func (b *lbBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) bal
145140
fallbackTimeout: b.fallbackTimeout,
146141
doneCh: make(chan struct{}),
147142

148-
manualResolver: r,
143+
// This generates a manual resolver builder with a fixed scheme. This
144+
// scheme will be used to dial to remote LB, so we can send filtered
145+
// address updates to remote LB ClientConn using this manual resolver.
146+
manualResolver: newManualResolver("grpclb-internal", cc),
149147
subConns: make(map[resolver.Address]balancer.SubConn),
150148
scStates: make(map[balancer.SubConn]connectivity.State),
151149
picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),

balancer/grpclb/grpclb_util.go

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"google.golang.org/grpc/balancer"
2727
"google.golang.org/grpc/resolver"
28+
"google.golang.org/grpc/resolver/manual"
2829
)
2930

3031
// The parent ClientConn should re-resolve when grpclb loses connection to the
@@ -60,50 +61,41 @@ import (
6061
// so when grpclb client lose contact with remote balancers, the parent
6162
// ClientConn's resolver will re-resolve.
6263
type lbManualResolver struct {
63-
scheme string
64-
ccr resolver.ClientConn
65-
ccb balancer.ClientConn
66-
67-
// Cache the most recently received state update via UpdateState(), and push
68-
// the same when Build() is invoked. This ensures that this manual resolver
69-
// can handle restarts when channel idleness comes into the picture.
70-
stateMu sync.Mutex
71-
lastState resolver.State
72-
stateUpdateRecv bool
73-
}
74-
75-
func (r *lbManualResolver) Build(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
76-
r.ccr = cc
77-
78-
r.stateMu.Lock()
79-
if r.stateUpdateRecv {
80-
r.ccr.UpdateState(r.lastState)
64+
// These fields are setup at creation time and are read-only after that, and
65+
// therefore need not be protected with a mutex.
66+
resolver.Builder // The underlying manual resolver builder.
67+
resolver.Resolver // The underlying manual resolver.
68+
ccb balancer.ClientConn // ClientConn passed to the parent channel.
69+
70+
// The resolver.ClientConn is updated everytime the resolver on the channel
71+
// is restarted, and this happens when the channel exits IDLE.
72+
mu sync.Mutex
73+
ccr resolver.ClientConn
74+
}
75+
76+
func newManualResolver(scheme string, ccb balancer.ClientConn) *lbManualResolver {
77+
mr := manual.NewBuilderWithScheme(scheme)
78+
r := &lbManualResolver{
79+
Builder: mr,
80+
Resolver: mr,
81+
ccb: ccb,
8182
}
82-
r.stateMu.Unlock()
8383

84-
return r, nil
85-
}
86-
87-
func (r *lbManualResolver) Scheme() string {
88-
return r.scheme
89-
}
90-
91-
// ResolveNow calls resolveNow on the parent ClientConn.
92-
func (r *lbManualResolver) ResolveNow(o resolver.ResolveNowOptions) {
93-
r.ccb.ResolveNow(o)
84+
mr.BuildCallback = func(_ resolver.Target, ccr resolver.ClientConn, _ resolver.BuildOptions) {
85+
r.mu.Lock()
86+
r.ccr = ccr
87+
r.mu.Unlock()
88+
}
89+
mr.ResolveNowCallback = func(o resolver.ResolveNowOptions) {
90+
r.ccb.ResolveNow(o)
91+
}
92+
return r
9493
}
9594

96-
// Close is a noop for Resolver.
97-
func (*lbManualResolver) Close() {}
98-
99-
// UpdateState calls cc.UpdateState.
10095
func (r *lbManualResolver) UpdateState(s resolver.State) {
101-
r.stateMu.Lock()
102-
r.lastState = s
103-
r.stateUpdateRecv = true
104-
r.stateMu.Unlock()
105-
96+
r.mu.Lock()
10697
r.ccr.UpdateState(s)
98+
r.mu.Unlock()
10799
}
108100

109101
const subConnCacheTime = time.Second * 10

0 commit comments

Comments
 (0)