@@ -293,16 +293,18 @@ func (a *authority) fallbackToServer(xc *xdsChannelWithConfig) bool {
293293 // Subscribe to all existing resources from the new management server.
294294 for typ , resources := range a .resources {
295295 for name , state := range resources {
296- if a .logger .V (2 ) {
297- a .logger .Infof ("Resubscribing to resource of type %q and name %q" , typ .TypeName , name )
298- }
299- xc .channel .subscribe (typ , name )
296+ if len (state .watchers ) > 0 {
297+ if a .logger .V (2 ) {
298+ a .logger .Infof ("Resubscribing to resource of type %q and name %q" , typ .TypeName , name )
299+ }
300+ xc .channel .subscribe (typ , name )
300301
301- // Add the new channel to the list of xdsChannels from which this
302- // resource has been requested from. Retain the cached resource and
303- // the set of existing watchers (and other metadata fields) in the
304- // resource state.
305- state .xdsChannelConfigs [xc ] = true
302+ // Add the new channel to the list of xdsChannels from which this
303+ // resource has been requested from. Retain the cached resource and
304+ // the set of existing watchers (and other metadata fields) in the
305+ // resource state.
306+ state .xdsChannelConfigs [xc ] = true
307+ }
306308 }
307309 }
308310 return true
@@ -731,10 +733,6 @@ func (a *authority) unwatchResource(rType ResourceType, resourceName string, wat
731733 // there when the watch was registered.
732734 resources := a .resources [rType ]
733735 state := resources [resourceName ]
734- if state == nil {
735- a .logger .Warningf ("Attempting to unwatch resource %q of type %q which is not currently watched" , resourceName , rType .TypeName )
736- return
737- }
738736
739737 // Delete this particular watcher from the list of watchers, so that its
740738 // callback will not be invoked in the future.
@@ -808,7 +806,7 @@ func (a *authority) closeXDSChannels() {
808806func (a * authority ) watcherExistsForUncachedResource () bool {
809807 for _ , resourceStates := range a .resources {
810808 for _ , state := range resourceStates {
811- if state .md .Status == xdsresource .ServiceStatusRequested {
809+ if len ( state . watchers ) > 0 && state .md .Status == xdsresource .ServiceStatusRequested {
812810 return true
813811 }
814812 }
@@ -840,6 +838,9 @@ func (a *authority) resourceConfig() []*v3statuspb.ClientConfig_GenericXdsConfig
840838 for rType , resourceStates := range a .resources {
841839 typeURL := rType .TypeURL
842840 for name , state := range resourceStates {
841+ if len (state .watchers ) == 0 {
842+ continue
843+ }
843844 var raw * anypb.Any
844845 if state .cache != nil {
845846 raw = & anypb.Any {TypeUrl : typeURL , Value : state .cache .Bytes ()}
@@ -878,6 +879,8 @@ func (a *authority) close() {
878879// after sending a discovery request to ensure that resources that were
879880// unsubscribed (and thus have no watchers) are eventually removed from the
880881// authority's cache.
882+ //
883+ // This method is only executed in the context of a serializer callback.
881884func (a * authority ) removeUnsubscribedCacheEntries (rType ResourceType ) {
882885 resources := a .resources [rType ]
883886 if resources == nil {
@@ -887,7 +890,7 @@ func (a *authority) removeUnsubscribedCacheEntries(rType ResourceType) {
887890 for name , state := range resources {
888891 if len (state .watchers ) == 0 {
889892 if a .logger .V (2 ) {
890- a .logger .Infof ("Removing resource state for %q of type %q as it has no watchers after an update cycle " , name , rType .TypeName )
893+ a .logger .Infof ("Removing resource state for %q of type %q as it has no watchers" , name , rType .TypeName )
891894 }
892895 delete (resources , name )
893896 }
0 commit comments