|
| 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 | + |
| 18 | +// https://github.com/grpc/proposal/blob/master/A62-pick-first.md#pick_first-via-xds-1 |
| 19 | + |
| 20 | +import { LoadBalancingConfig } from "@grpc/grpc-js"; |
| 21 | +import { LoadBalancingPolicy__Output } from "../generated/envoy/config/cluster/v3/LoadBalancingPolicy"; |
| 22 | +import { TypedExtensionConfig__Output } from "../generated/envoy/config/core/v3/TypedExtensionConfig"; |
| 23 | +import { loadProtosWithOptionsSync } from "@grpc/proto-loader/build/src/util"; |
| 24 | +import { Any__Output } from "../generated/google/protobuf/Any"; |
| 25 | +import { PickFirst__Output } from "../generated/envoy/extensions/load_balancing_policies/pick_first/v3/PickFirst"; |
| 26 | +import { EXPERIMENTAL_PICK_FIRST } from "../environment"; |
| 27 | +import { registerLbPolicy } from "../lb-policy-registry"; |
| 28 | + |
| 29 | +const PICK_FIRST_TYPE_URL = 'type.googleapis.com/envoy.extensions.load_balancing_policies.pick_first.v3.PickFirst'; |
| 30 | + |
| 31 | +const resourceRoot = loadProtosWithOptionsSync([ |
| 32 | + 'envoy/extensions/load_balancing_policies/pick_first/v3/pick_first.proto'], { |
| 33 | + keepCase: true, |
| 34 | + includeDirs: [ |
| 35 | + // Paths are relative to src/build/lb-policy-registry |
| 36 | + __dirname + '/../../../deps/envoy-api/', |
| 37 | + __dirname + '/../../../deps/xds/', |
| 38 | + __dirname + '/../../../deps/protoc-gen-validate' |
| 39 | + ], |
| 40 | + } |
| 41 | +); |
| 42 | + |
| 43 | +const toObjectOptions = { |
| 44 | + longs: String, |
| 45 | + enums: String, |
| 46 | + defaults: true, |
| 47 | + oneofs: true |
| 48 | +} |
| 49 | + |
| 50 | +function decodePickFirstConfig(message: Any__Output): PickFirst__Output { |
| 51 | + const name = message.type_url.substring(message.type_url.lastIndexOf('/') + 1); |
| 52 | + const type = resourceRoot.lookup(name); |
| 53 | + if (type) { |
| 54 | + const decodedMessage = (type as any).decode(message.value); |
| 55 | + return decodedMessage.$type.toObject(decodedMessage, toObjectOptions) as PickFirst__Output; |
| 56 | + } else { |
| 57 | + throw new Error(`TypedStruct parsing error: unexpected type URL ${message.type_url}`); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function convertToLoadBalancingPolicy(protoPolicy: TypedExtensionConfig__Output, selectChildPolicy: (childPolicy: LoadBalancingPolicy__Output) => LoadBalancingConfig): LoadBalancingConfig | null { |
| 62 | + if (protoPolicy.typed_config?.type_url !== PICK_FIRST_TYPE_URL) { |
| 63 | + throw new Error(`Pick first LB policy parsing error: unexpected type URL ${protoPolicy.typed_config?.type_url}`); |
| 64 | + } |
| 65 | + const pickFirstMessage = decodePickFirstConfig(protoPolicy.typed_config); |
| 66 | + return { |
| 67 | + pick_first: { |
| 68 | + shuffleAddressList: pickFirstMessage.shuffle_address_list |
| 69 | + } |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +export function setup() { |
| 74 | + if (EXPERIMENTAL_PICK_FIRST) { |
| 75 | + registerLbPolicy(PICK_FIRST_TYPE_URL, convertToLoadBalancingPolicy); |
| 76 | + } |
| 77 | +} |
0 commit comments