-
Notifications
You must be signed in to change notification settings - Fork 4.5k
balancer: add StateListener to NewSubConnOptions for SubConn state updates #6481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,8 +200,8 @@ func (gsb *Balancer) ExitIdle() { | |
} | ||
} | ||
|
||
// UpdateSubConnState forwards the update to the appropriate child. | ||
func (gsb *Balancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { | ||
// updateSubConnState forwards the update to the appropriate child. | ||
func (gsb *Balancer) updateSubConnState(sc balancer.SubConn, state balancer.SubConnState, cb func(balancer.SubConnState)) { | ||
gsb.currentMu.Lock() | ||
defer gsb.currentMu.Unlock() | ||
gsb.mu.Lock() | ||
|
@@ -214,13 +214,26 @@ func (gsb *Balancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubC | |
} else if gsb.balancerPending != nil && gsb.balancerPending.subconns[sc] { | ||
balToUpdate = gsb.balancerPending | ||
} | ||
gsb.mu.Unlock() | ||
if balToUpdate == nil { | ||
// SubConn belonged to a stale lb policy that has not yet fully closed, | ||
// or the balancer was already closed. | ||
gsb.mu.Unlock() | ||
return | ||
} | ||
balToUpdate.UpdateSubConnState(sc, state) | ||
if state.ConnectivityState == connectivity.Shutdown { | ||
delete(balToUpdate.subconns, sc) | ||
} | ||
gsb.mu.Unlock() | ||
if cb != nil { | ||
cb(state) | ||
easwars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
balToUpdate.UpdateSubConnState(sc, state) | ||
} | ||
easwars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// UpdateSubConnState forwards the update to the appropriate child. | ||
func (gsb *Balancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { | ||
gsb.updateSubConnState(sc, state, nil) | ||
Comment on lines
+234
to
+236
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, from tests... Loads of tests call these methods. I cleaned up some, but there are more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have a TODO or an issue to track that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will need to be done as part of #6472. Once this PR lands and I start migrating more things, I'll make |
||
} | ||
|
||
// Close closes any active child balancers. | ||
|
@@ -254,18 +267,6 @@ type balancerWrapper struct { | |
subconns map[balancer.SubConn]bool // subconns created by this balancer | ||
} | ||
|
||
func (bw *balancerWrapper) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) { | ||
if state.ConnectivityState == connectivity.Shutdown { | ||
bw.gsb.mu.Lock() | ||
delete(bw.subconns, sc) | ||
bw.gsb.mu.Unlock() | ||
} | ||
// There is no need to protect this read with a mutex, as the write to the | ||
// Balancer field happens in SwitchTo, which completes before this can be | ||
// called. | ||
bw.Balancer.UpdateSubConnState(sc, state) | ||
} | ||
|
||
// Close closes the underlying LB policy and removes the subconns it created. bw | ||
// must not be referenced via balancerCurrent or balancerPending in gsb when | ||
// called. gsb.mu must not be held. Does not panic with a nil receiver. | ||
|
@@ -335,6 +336,9 @@ func (bw *balancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.Ne | |
} | ||
bw.gsb.mu.Unlock() | ||
|
||
var sc balancer.SubConn | ||
oldListener := opts.StateListener | ||
easwars marked this conversation as resolved.
Show resolved
Hide resolved
|
||
opts.StateListener = func(state balancer.SubConnState) { bw.gsb.updateSubConnState(sc, state, oldListener) } | ||
sc, err := bw.gsb.cc.NewSubConn(addrs, opts) | ||
if err != nil { | ||
return nil, err | ||
|
Uh oh!
There was an error while loading. Please reload this page.