• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Example gRPC client to depict flow control in gRPC"""
15
16import logging
17
18import grpc
19import helloworld_pb2
20import helloworld_pb2_grpc
21import helpers
22
23
24def get_iter_data():
25    max_iter = 100
26    data_size = 2000
27    test_request_data = bytes("1" * data_size, "utf-8")
28
29    for i in range(1, (max_iter + 1)):
30        if (i % 10) == 0:
31            print(
32                f"\n{helpers.get_current_time()}   "
33                f"Request {i}: Sent {(data_size*i)} bytes in total"
34            )
35
36        yield helloworld_pb2.HelloRequest(name=test_request_data)
37
38
39def run():
40    with grpc.insecure_channel(
41        "localhost:50051",
42        # Setting server options to minimal, to allow low number of maximum
43        # bytes in the window
44        # `bdp_probe` is set to 0(false) to disable resizing of window
45        options=[
46            ("grpc.http2.max_frame_size", 16384),
47            ("grpc.http2.bdp_probe", 0),
48        ],
49    ) as channel:
50        stub = helloworld_pb2_grpc.GreeterStub(channel)
51
52        responses = stub.SayHelloBidiStream(get_iter_data())
53        for i, _ in enumerate(responses, start=1):
54            if (i % 10) == 0:
55                print(
56                    f"{helpers.get_current_time()}   "
57                    f"Received {i} responses\n"
58                )
59
60
61if __name__ == "__main__":
62    logging.basicConfig()
63    run()
64