...

Source file src/google.golang.org/grpc/xds/internal/xdsclient/clientimpl_watchers.go

Documentation: google.golang.org/grpc/xds/internal/xdsclient

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package xdsclient
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"sync"
    24  
    25  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
    26  )
    27  
    28  // WatchResource uses xDS to discover the resource associated with the provided
    29  // resource name. The resource type implementation determines how xDS requests
    30  // are sent out and how responses are deserialized and validated. Upon receipt
    31  // of a response from the management server, an appropriate callback on the
    32  // watcher is invoked.
    33  func (c *clientImpl) WatchResource(rType xdsresource.Type, resourceName string, watcher xdsresource.ResourceWatcher) (cancel func()) {
    34  	// Return early if the client is already closed.
    35  	//
    36  	// The client returned from the top-level API is a ref-counted client which
    37  	// contains a pointer to `clientImpl`. When all references are released, the
    38  	// ref-counted client sets its pointer to `nil`. And if any watch APIs are
    39  	// made on such a closed client, we will get here with a `nil` receiver.
    40  	if c == nil || c.done.HasFired() {
    41  		logger.Warningf("Watch registered for name %q of type %q, but client is closed", rType.TypeName(), resourceName)
    42  		return func() {}
    43  	}
    44  
    45  	if err := c.resourceTypes.maybeRegister(rType); err != nil {
    46  		logger.Warningf("Watch registered for name %q of type %q which is already registered", rType.TypeName(), resourceName)
    47  		c.serializer.Schedule(func(context.Context) { watcher.OnError(err) })
    48  		return func() {}
    49  	}
    50  
    51  	// TODO: Make ParseName return an error if parsing fails, and
    52  	// schedule the OnError callback in that case.
    53  	n := xdsresource.ParseName(resourceName)
    54  	a, unref, err := c.findAuthority(n)
    55  	if err != nil {
    56  		logger.Warningf("Watch registered for name %q of type %q, authority %q is not found", rType.TypeName(), resourceName, n.Authority)
    57  		c.serializer.Schedule(func(context.Context) { watcher.OnError(err) })
    58  		return func() {}
    59  	}
    60  	cancelF := a.watchResource(rType, n.String(), watcher)
    61  	return func() {
    62  		cancelF()
    63  		unref()
    64  	}
    65  }
    66  
    67  // A registry of xdsresource.Type implementations indexed by their corresponding
    68  // type URLs. Registration of an xdsresource.Type happens the first time a watch
    69  // for a resource of that type is invoked.
    70  type resourceTypeRegistry struct {
    71  	mu    sync.Mutex
    72  	types map[string]xdsresource.Type
    73  }
    74  
    75  func newResourceTypeRegistry() *resourceTypeRegistry {
    76  	return &resourceTypeRegistry{types: make(map[string]xdsresource.Type)}
    77  }
    78  
    79  func (r *resourceTypeRegistry) get(url string) xdsresource.Type {
    80  	r.mu.Lock()
    81  	defer r.mu.Unlock()
    82  	return r.types[url]
    83  }
    84  
    85  func (r *resourceTypeRegistry) maybeRegister(rType xdsresource.Type) error {
    86  	r.mu.Lock()
    87  	defer r.mu.Unlock()
    88  
    89  	url := rType.TypeURL()
    90  	typ, ok := r.types[url]
    91  	if ok && typ != rType {
    92  		return fmt.Errorf("attempt to re-register a resource type implementation for %v", rType.TypeName())
    93  	}
    94  	r.types[url] = rType
    95  	return nil
    96  }
    97  
    98  func (c *clientImpl) triggerResourceNotFoundForTesting(rType xdsresource.Type, resourceName string) error {
    99  	// Return early if the client is already closed.
   100  	if c == nil || c.done.HasFired() {
   101  		return fmt.Errorf("attempt to trigger resource-not-found-error for resource %q of type %q, but client is closed", rType.TypeName(), resourceName)
   102  	}
   103  
   104  	n := xdsresource.ParseName(resourceName)
   105  	a, unref, err := c.findAuthority(n)
   106  	if err != nil {
   107  		return fmt.Errorf("attempt to trigger resource-not-found-error for resource %q of type %q, but authority %q is not found", rType.TypeName(), resourceName, n.Authority)
   108  	}
   109  	defer unref()
   110  	a.triggerResourceNotFoundForTesting(rType, n.String())
   111  	return nil
   112  }
   113  

View as plain text