google.golang.org/grpc@v1.72.2/xds/internal/resolver/watch_service.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  
    19  package resolver
    20  
    21  import (
    22  	"context"
    23  
    24  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
    25  )
    26  
    27  type listenerWatcher struct {
    28  	resourceName string
    29  	cancel       func()
    30  	parent       *xdsResolver
    31  }
    32  
    33  func newListenerWatcher(resourceName string, parent *xdsResolver) *listenerWatcher {
    34  	lw := &listenerWatcher{resourceName: resourceName, parent: parent}
    35  	lw.cancel = xdsresource.WatchListener(parent.xdsClient, resourceName, lw)
    36  	return lw
    37  }
    38  
    39  func (l *listenerWatcher) OnUpdate(update *xdsresource.ListenerResourceData, onDone xdsresource.OnDoneFunc) {
    40  	handleUpdate := func(context.Context) { l.parent.onListenerResourceUpdate(update.Resource); onDone() }
    41  	l.parent.serializer.ScheduleOr(handleUpdate, onDone)
    42  }
    43  
    44  func (l *listenerWatcher) OnError(err error, onDone xdsresource.OnDoneFunc) {
    45  	handleError := func(context.Context) { l.parent.onListenerResourceError(err); onDone() }
    46  	l.parent.serializer.ScheduleOr(handleError, onDone)
    47  }
    48  
    49  func (l *listenerWatcher) OnResourceDoesNotExist(onDone xdsresource.OnDoneFunc) {
    50  	handleNotFound := func(context.Context) { l.parent.onListenerResourceNotFound(); onDone() }
    51  	l.parent.serializer.ScheduleOr(handleNotFound, onDone)
    52  }
    53  
    54  func (l *listenerWatcher) stop() {
    55  	l.cancel()
    56  	l.parent.logger.Infof("Canceling watch on Listener resource %q", l.resourceName)
    57  }
    58  
    59  type routeConfigWatcher struct {
    60  	resourceName string
    61  	cancel       func()
    62  	parent       *xdsResolver
    63  }
    64  
    65  func newRouteConfigWatcher(resourceName string, parent *xdsResolver) *routeConfigWatcher {
    66  	rw := &routeConfigWatcher{resourceName: resourceName, parent: parent}
    67  	rw.cancel = xdsresource.WatchRouteConfig(parent.xdsClient, resourceName, rw)
    68  	return rw
    69  }
    70  
    71  func (r *routeConfigWatcher) OnUpdate(u *xdsresource.RouteConfigResourceData, onDone xdsresource.OnDoneFunc) {
    72  	handleUpdate := func(context.Context) {
    73  		r.parent.onRouteConfigResourceUpdate(r.resourceName, u.Resource)
    74  		onDone()
    75  	}
    76  	r.parent.serializer.ScheduleOr(handleUpdate, onDone)
    77  }
    78  
    79  func (r *routeConfigWatcher) OnError(err error, onDone xdsresource.OnDoneFunc) {
    80  	handleError := func(context.Context) { r.parent.onRouteConfigResourceError(r.resourceName, err); onDone() }
    81  	r.parent.serializer.ScheduleOr(handleError, onDone)
    82  }
    83  
    84  func (r *routeConfigWatcher) OnResourceDoesNotExist(onDone xdsresource.OnDoneFunc) {
    85  	handleNotFound := func(context.Context) { r.parent.onRouteConfigResourceNotFound(r.resourceName); onDone() }
    86  	r.parent.serializer.ScheduleOr(handleNotFound, onDone)
    87  }
    88  
    89  func (r *routeConfigWatcher) stop() {
    90  	r.cancel()
    91  	r.parent.logger.Infof("Canceling watch on RouteConfiguration resource %q", r.resourceName)
    92  }