gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/xdsclient/controller/version/version.go (about) 1 /* 2 * 3 * Copyright 2021 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 version defines APIs to deal with different versions of xDS. 19 package version 20 21 import ( 22 "context" 23 "time" 24 25 grpc "gitee.com/ks-custle/core-gm/grpc" 26 "gitee.com/ks-custle/core-gm/grpc/internal/grpclog" 27 "gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/load" 28 "gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource" 29 "gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource/version" 30 "github.com/golang/protobuf/proto" 31 "google.golang.org/protobuf/types/known/anypb" 32 ) 33 34 var ( 35 m = make(map[version.TransportAPI]func(opts BuildOptions) (VersionedClient, error)) 36 ) 37 38 // RegisterAPIClientBuilder registers a client builder for xDS transport protocol 39 // version specified by b.Version(). 40 // 41 // NOTE: this function must only be called during initialization time (i.e. in 42 // an init() function), and is not thread-safe. If multiple builders are 43 // registered for the same version, the one registered last will take effect. 44 func RegisterAPIClientBuilder(v version.TransportAPI, f func(opts BuildOptions) (VersionedClient, error)) { 45 m[v] = f 46 } 47 48 // GetAPIClientBuilder returns the client builder registered for the provided 49 // xDS transport API version. 50 func GetAPIClientBuilder(version version.TransportAPI) func(opts BuildOptions) (VersionedClient, error) { 51 if f, ok := m[version]; ok { 52 return f 53 } 54 return nil 55 } 56 57 // BuildOptions contains options to be passed to client builders. 58 type BuildOptions struct { 59 // NodeProto contains the Node proto to be used in xDS requests. The actual 60 // type depends on the transport protocol version used. 61 NodeProto proto.Message 62 // // Backoff returns the amount of time to backoff before retrying broken 63 // // streams. 64 // Backoff func(int) time.Duration 65 // Logger provides enhanced logging capabilities. 66 Logger *grpclog.PrefixLogger 67 } 68 69 // LoadReportingOptions contains configuration knobs for reporting load data. 70 type LoadReportingOptions struct { 71 LoadStore *load.Store 72 } 73 74 // ErrResourceTypeUnsupported is an error used to indicate an unsupported xDS 75 // resource type. The wrapped ErrStr contains the details. 76 type ErrResourceTypeUnsupported struct { 77 ErrStr string 78 } 79 80 // Error helps implements the error interface. 81 func (e ErrResourceTypeUnsupported) Error() string { 82 return e.ErrStr 83 } 84 85 // VersionedClient is the interface to version specific operations of the 86 // client. 87 // 88 // It mainly deals with the type assertion from proto.Message to the real v2/v3 89 // types, and grpc.Stream to the versioned stream types. 90 type VersionedClient interface { 91 // NewStream returns a new xDS client stream specific to the underlying 92 // transport protocol version. 93 NewStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error) 94 // SendRequest constructs and sends out a DiscoveryRequest message specific 95 // to the underlying transport protocol version. 96 SendRequest(s grpc.ClientStream, resourceNames []string, rType xdsresource.ResourceType, version, nonce, errMsg string) error 97 // RecvResponse uses the provided stream to receive a response specific to 98 // the underlying transport protocol version. 99 RecvResponse(s grpc.ClientStream) (proto.Message, error) 100 // ParseResponse type asserts message to the versioned response, and 101 // retrieves the fields. 102 ParseResponse(r proto.Message) (xdsresource.ResourceType, []*anypb.Any, string, string, error) 103 104 // The following are LRS methods. 105 106 // NewLoadStatsStream returns a new LRS client stream specific to the 107 // underlying transport protocol version. 108 NewLoadStatsStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error) 109 // SendFirstLoadStatsRequest constructs and sends the first request on the 110 // LRS stream. 111 SendFirstLoadStatsRequest(s grpc.ClientStream) error 112 // HandleLoadStatsResponse receives the first response from the server which 113 // contains the load reporting interval and the clusters for which the 114 // server asks the client to report load for. 115 // 116 // If the response sets SendAllClusters to true, the returned clusters is 117 // nil. 118 HandleLoadStatsResponse(s grpc.ClientStream) (clusters []string, _ time.Duration, _ error) 119 // SendLoadStatsRequest will be invoked at regular intervals to send load 120 // report with load data reported since the last time this method was 121 // invoked. 122 SendLoadStatsRequest(s grpc.ClientStream, loads []*load.Data) error 123 }