github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/watchers.go (about)

     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  	"github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/xdsresource"
    22  )
    23  
    24  // WatchListener uses LDS to discover information about the provided listener.
    25  //
    26  // Note that during race (e.g. an xDS response is received while the user is
    27  // calling cancel()), there's a small window where the callback can be called
    28  // after the watcher is canceled. The caller needs to handle this case.
    29  func (c *clientImpl) WatchListener(serviceName string, cb func(xdsresource.ListenerUpdate, error)) (cancel func()) {
    30  	n := xdsresource.ParseName(serviceName)
    31  	a, unref, err := c.findAuthority(n)
    32  	if err != nil {
    33  		cb(xdsresource.ListenerUpdate{}, err)
    34  		return func() {}
    35  	}
    36  	cancelF := a.watchListener(n.String(), cb)
    37  	return func() {
    38  		cancelF()
    39  		unref()
    40  	}
    41  }
    42  
    43  // WatchRouteConfig starts a listener watcher for the service.
    44  //
    45  // Note that during race (e.g. an xDS response is received while the user is
    46  // calling cancel()), there's a small window where the callback can be called
    47  // after the watcher is canceled. The caller needs to handle this case.
    48  func (c *clientImpl) WatchRouteConfig(routeName string, cb func(xdsresource.RouteConfigUpdate, error)) (cancel func()) {
    49  	n := xdsresource.ParseName(routeName)
    50  	a, unref, err := c.findAuthority(n)
    51  	if err != nil {
    52  		cb(xdsresource.RouteConfigUpdate{}, err)
    53  		return func() {}
    54  	}
    55  	cancelF := a.watchRouteConfig(n.String(), cb)
    56  	return func() {
    57  		cancelF()
    58  		unref()
    59  	}
    60  }
    61  
    62  // WatchCluster uses CDS to discover information about the provided
    63  // clusterName.
    64  //
    65  // WatchCluster can be called multiple times, with same or different
    66  // clusterNames. Each call will start an independent watcher for the resource.
    67  //
    68  // Note that during race (e.g. an xDS response is received while the user is
    69  // calling cancel()), there's a small window where the callback can be called
    70  // after the watcher is canceled. The caller needs to handle this case.
    71  func (c *clientImpl) WatchCluster(clusterName string, cb func(xdsresource.ClusterUpdate, error)) (cancel func()) {
    72  	n := xdsresource.ParseName(clusterName)
    73  	a, unref, err := c.findAuthority(n)
    74  	if err != nil {
    75  		cb(xdsresource.ClusterUpdate{}, err)
    76  		return func() {}
    77  	}
    78  	cancelF := a.watchCluster(n.String(), cb)
    79  	return func() {
    80  		cancelF()
    81  		unref()
    82  	}
    83  }
    84  
    85  // WatchEndpoints uses EDS to discover endpoints in the provided clusterName.
    86  //
    87  // WatchEndpoints can be called multiple times, with same or different
    88  // clusterNames. Each call will start an independent watcher for the resource.
    89  //
    90  // Note that during race (e.g. an xDS response is received while the user is
    91  // calling cancel()), there's a small window where the callback can be called
    92  // after the watcher is canceled. The caller needs to handle this case.
    93  func (c *clientImpl) WatchEndpoints(clusterName string, cb func(xdsresource.EndpointsUpdate, error)) (cancel func()) {
    94  	n := xdsresource.ParseName(clusterName)
    95  	a, unref, err := c.findAuthority(n)
    96  	if err != nil {
    97  		cb(xdsresource.EndpointsUpdate{}, err)
    98  		return func() {}
    99  	}
   100  	cancelF := a.watchEndpoints(n.String(), cb)
   101  	return func() {
   102  		cancelF()
   103  		unref()
   104  	}
   105  }