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) { 36 cw.parent.serializer.Schedule(func(context.Context) { 37 cw.parent.onClusterUpdate(cw.name, u.Resource) 38 }) 39 } 40 41 func (cw *clusterWatcher) OnError(err error) { 42 cw.parent.serializer.Schedule(func(context.Context) { 43 cw.parent.onClusterError(cw.name, err) 44 }) 45 } 46 47 func (cw *clusterWatcher) OnResourceDoesNotExist() { 48 cw.parent.serializer.Schedule(func(context.Context) { 49 cw.parent.onClusterResourceNotFound(cw.name) 50 }) 51 } 52 53 // watcherState groups the state associated with a clusterWatcher. 54 type watcherState struct { 55 watcher *clusterWatcher // The underlying watcher. 56 cancelWatch func() // Cancel func to cancel the watch. 57 lastUpdate *xdsresource.ClusterUpdate // Most recent update received for this cluster. 58 } 59