xds/client: hold authority mutex before making a new authority (#5331)

This commit is contained in:
Menghan Li 2022-05-03 13:39:18 -07:00 committed by GitHub
parent de73b2b645
commit 78b13f27de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -55,7 +55,7 @@ func (c *clientImpl) findAuthority(n *xdsresource.Name) (_ *authority, unref fun
config = cfg.XDSServer config = cfg.XDSServer
} }
a, err := c.newAuthority(config) a, err := c.newAuthorityLocked(config)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("xds: failed to connect to the control plane for authority %q: %v", authority, err) return nil, nil, fmt.Errorf("xds: failed to connect to the control plane for authority %q: %v", authority, err)
} }
@ -73,14 +73,14 @@ func (c *clientImpl) findAuthority(n *xdsresource.Name) (_ *authority, unref fun
return a, func() { c.unrefAuthority(a) }, nil return a, func() { c.unrefAuthority(a) }, nil
} }
// newAuthority creates a new authority for the config. But before that, it // newAuthorityLocked creates a new authority for the config. But before that, it
// checks the cache to see if an authority for this config already exists. // checks the cache to see if an authority for this config already exists.
// //
// The caller must take a reference of the returned authority before using, and // The caller must take a reference of the returned authority before using, and
// unref afterwards. // unref afterwards.
// //
// caller must hold c.authorityMu // caller must hold c.authorityMu
func (c *clientImpl) newAuthority(config *bootstrap.ServerConfig) (_ *authority, retErr error) { func (c *clientImpl) newAuthorityLocked(config *bootstrap.ServerConfig) (_ *authority, retErr error) {
// First check if there's already an authority for this config. If found, it // First check if there's already an authority for this config. If found, it
// means this authority is used by other watches (could be the same // means this authority is used by other watches (could be the same
// authority name, or a different authority name but the same server // authority name, or a different authority name but the same server

View File

@ -28,7 +28,9 @@ import (
// It returns a Store for the user to report loads, a function to cancel the // It returns a Store for the user to report loads, a function to cancel the
// load reporting stream. // load reporting stream.
func (c *clientImpl) ReportLoad(server *bootstrap.ServerConfig) (*load.Store, func()) { func (c *clientImpl) ReportLoad(server *bootstrap.ServerConfig) (*load.Store, func()) {
a, err := c.newAuthority(server) c.authorityMu.Lock()
a, err := c.newAuthorityLocked(server)
c.authorityMu.Unlock()
if err != nil { if err != nil {
c.logger.Infof("xds: failed to connect to the control plane to do load reporting for authority %q: %v", server, err) c.logger.Infof("xds: failed to connect to the control plane to do load reporting for authority %q: %v", server, err)
return nil, func() {} return nil, func() {}