Skip to content

Commit

Permalink
xds/client: hold authority mutex before making a new authority (#5331)
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed May 3, 2022
1 parent de73b2b commit 78b13f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions xds/internal/xdsclient/authority.go
Expand Up @@ -55,7 +55,7 @@ func (c *clientImpl) findAuthority(n *xdsresource.Name) (_ *authority, unref fun
config = cfg.XDSServer
}

a, err := c.newAuthority(config)
a, err := c.newAuthorityLocked(config)
if err != nil {
return nil, nil, fmt.Errorf("xds: failed to connect to the control plane for authority %q: %v", authority, err)
}
Expand All @@ -73,14 +73,14 @@ func (c *clientImpl) findAuthority(n *xdsresource.Name) (_ *authority, unref fun
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.
//
// The caller must take a reference of the returned authority before using, and
// unref afterwards.
//
// 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
// means this authority is used by other watches (could be the same
// authority name, or a different authority name but the same server
Expand Down
4 changes: 3 additions & 1 deletion xds/internal/xdsclient/loadreport.go
Expand Up @@ -28,7 +28,9 @@ import (
// It returns a Store for the user to report loads, a function to cancel the
// load reporting stream.
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 {
c.logger.Infof("xds: failed to connect to the control plane to do load reporting for authority %q: %v", server, err)
return nil, func() {}
Expand Down

0 comments on commit 78b13f2

Please sign in to comment.