dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/watchers.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 * 20 * Copyright 2020 gRPC authors. 21 * 22 */ 23 24 package client 25 26 import ( 27 "dubbo.apache.org/dubbo-go/v3/xds/client/resource" 28 ) 29 30 // WatchListener uses LDS to discover information about the provided listener. 31 // 32 // Note that during race (e.g. an xDS response is received while the user is 33 // calling cancel()), there's a small window where the callback can be called 34 // after the watcher is canceled. The caller needs to handle this case. 35 func (c *clientImpl) WatchListener(serviceName string, cb func(resource.ListenerUpdate, error)) (cancel func()) { 36 n := resource.ParseName(serviceName) 37 a, unref, err := c.findAuthority(n) 38 if err != nil { 39 cb(resource.ListenerUpdate{}, err) 40 return func() {} 41 } 42 cancelF := a.watchListener(n.String(), cb) 43 return func() { 44 cancelF() 45 unref() 46 } 47 } 48 49 // WatchRouteConfig starts a listener watcher for the service. 50 // 51 // Note that during race (e.g. an xDS response is received while the user is 52 // calling cancel()), there's a small window where the callback can be called 53 // after the watcher is canceled. The caller needs to handle this case. 54 func (c *clientImpl) WatchRouteConfig(routeName string, cb func(resource.RouteConfigUpdate, error)) (cancel func()) { 55 n := resource.ParseName(routeName) 56 a, unref, err := c.findAuthority(n) 57 if err != nil { 58 cb(resource.RouteConfigUpdate{}, err) 59 return func() {} 60 } 61 cancelF := a.watchRouteConfig(n.String(), cb) 62 return func() { 63 cancelF() 64 unref() 65 } 66 } 67 68 // WatchCluster uses CDS to discover information about the provided 69 // clusterName. 70 // 71 // WatchCluster can be called multiple times, with same or different 72 // clusterNames. Each call will start an independent watcher for the resource. 73 // 74 // Note that during race (e.g. an xDS response is received while the user is 75 // calling cancel()), there's a small window where the callback can be called 76 // after the watcher is canceled. The caller needs to handle this case. 77 func (c *clientImpl) WatchCluster(clusterName string, cb func(resource.ClusterUpdate, error)) (cancel func()) { 78 n := resource.ParseName(clusterName) 79 a, unref, err := c.findAuthority(n) 80 if err != nil { 81 cb(resource.ClusterUpdate{}, err) 82 return func() {} 83 } 84 cancelF := a.watchCluster(n.String(), cb) 85 return func() { 86 cancelF() 87 unref() 88 } 89 } 90 91 // WatchEndpoints uses EDS to discover endpoints in the provided clusterName. 92 // 93 // WatchEndpoints can be called multiple times, with same or different 94 // clusterNames. Each call will start an independent watcher for the resource. 95 // 96 // Note that during race (e.g. an xDS response is received while the user is 97 // calling cancel()), there's a small window where the callback can be called 98 // after the watcher is canceled. The caller needs to handle this case. 99 func (c *clientImpl) WatchEndpoints(clusterName string, cb func(resource.EndpointsUpdate, error)) (cancel func()) { 100 n := resource.ParseName(clusterName) 101 a, unref, err := c.findAuthority(n) 102 if err != nil { 103 cb(resource.EndpointsUpdate{}, err) 104 return func() {} 105 } 106 cancelF := a.watchEndpoints(n.String(), cb) 107 return func() { 108 cancelF() 109 unref() 110 } 111 }