|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2025 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 | + |
| 19 | +const grpc = require('@grpc/grpc-js'); |
| 20 | +const protoLoader = require('@grpc/proto-loader'); |
| 21 | + |
| 22 | +const PROTO_PATH = __dirname + '/../protos/echo.proto'; |
| 23 | + |
| 24 | +const packageDefinition = protoLoader.loadSync( |
| 25 | + PROTO_PATH, |
| 26 | + {keepCase: true, |
| 27 | + longs: String, |
| 28 | + enums: String, |
| 29 | + defaults: true, |
| 30 | + oneofs: true |
| 31 | + }); |
| 32 | +const echoProto = grpc.loadPackageDefinition(packageDefinition).grpc.examples.echo; |
| 33 | + |
| 34 | +const addressString = 'ipv4:///127.0.0.1:50051,127.0.0.1:50052'; |
| 35 | + |
| 36 | +function callUnaryEcho(client, message) { |
| 37 | + return new Promise((resolve, reject) => { |
| 38 | + const deadline = new Date(); |
| 39 | + deadline.setSeconds(deadline.getSeconds() + 1); |
| 40 | + client.unaryEcho({message}, {deadline}, (error, response) => { |
| 41 | + if (error) { |
| 42 | + reject(error); |
| 43 | + } else { |
| 44 | + console.log(response.message); |
| 45 | + resolve(response); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +async function makeRPCs(client, count) { |
| 52 | + for (let i = 0; i < count; i++) { |
| 53 | + await callUnaryEcho(client, "this is examples/load_balancing"); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function main() { |
| 58 | + // "pick_first" is the default, so there's no need to set the load balancing policy. |
| 59 | + const pickFirstClient = new echoProto.Echo(addressString, grpc.credentials.createInsecure()); |
| 60 | + console.log("--- calling helloworld.Greeter/SayHello with pick_first ---"); |
| 61 | + await makeRPCs(pickFirstClient, 10); |
| 62 | + console.log(); |
| 63 | + |
| 64 | + const roundRobinServiceConfig = { |
| 65 | + methodConfig: [], |
| 66 | + loadBalancingConfig: [{ round_robin: {} }] |
| 67 | + }; |
| 68 | + const roundRobinClient = new echoProto.Echo(addressString, grpc.credentials.createInsecure(), {'grpc.service_config': JSON.stringify(roundRobinServiceConfig)}); |
| 69 | + console.log("--- calling helloworld.Greeter/SayHello with round_robin ---"); |
| 70 | + await makeRPCs(roundRobinClient, 10); |
| 71 | + pickFirstClient.close(); |
| 72 | + roundRobinClient.close(); |
| 73 | +} |
| 74 | + |
| 75 | +main(); |
0 commit comments