google.golang.org/grpc@v1.72.2/xds/internal/balancer/cdsbalancer/cluster_watcher.go (about)

     1  /*
     2   * Copyright 2023 gRPC authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cdsbalancer
    18  
    19  import (
    20  	"context"
    21  
    22  	"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
    23  )
    24  
    25  // clusterWatcher implements the xdsresource.ClusterWatcher interface, and is
    26  // passed to the xDS client as part of the WatchResource() API.
    27  //
    28  // It watches a single cluster and handles callbacks from the xDS client by
    29  // scheduling them on the parent LB policy's serializer.
    30  type clusterWatcher struct {
    31  	name   string
    32  	parent *cdsBalancer
    33  }
    34  
    35  func (cw *clusterWatcher) OnUpdate(u *xdsresource.ClusterResourceData, onDone xdsresource.OnDoneFunc) {
    36  	handleUpdate := func(context.Context) { cw.parent.onClusterUpdate(cw.name, u.Resource); onDone() }
    37  	cw.parent.serializer.ScheduleOr(handleUpdate, onDone)
    38  }
    39  
    40  func (cw *clusterWatcher) OnError(err error, onDone xdsresource.OnDoneFunc) {
    41  	handleError := func(context.Context) { cw.parent.onClusterError(cw.name, err); onDone() }
    42  	cw.parent.serializer.ScheduleOr(handleError, onDone)
    43  }
    44  
    45  func (cw *clusterWatcher) OnResourceDoesNotExist(onDone xdsresource.OnDoneFunc) {
    46  	handleNotFound := func(context.Context) { cw.parent.onClusterResourceNotFound(cw.name); onDone() }
    47  	cw.parent.serializer.ScheduleOr(handleNotFound, onDone)
    48  }
    49  
    50  // watcherState groups the state associated with a clusterWatcher.
    51  type watcherState struct {
    52  	watcher     *clusterWatcher            // The underlying watcher.
    53  	cancelWatch func()                     // Cancel func to cancel the watch.
    54  	lastUpdate  *xdsresource.ClusterUpdate // Most recent update received for this cluster.
    55  }