Last active
November 6, 2018 20:53
-
-
Save gaffer-93/912abe0d639ef033108de06bfc6d4d7a to your computer and use it in GitHub Desktop.
gRPC's helloworld example modified to demonstrate deadlines and keepalive TCP_USER_TIMEOUT introduced in 1.16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import grpc | |
import time | |
import helloworld_pb2 | |
import helloworld_pb2_grpc | |
def run(): | |
channel_opts = [ | |
( | |
# Interval at which grpc will send keepalive pings | |
'grpc.keepalive_time_ms', | |
5000 | |
), | |
( | |
# Amount of time grpc waits for a keepalive ping to be | |
# acknowledged before deeming the connection unhealthy and closing | |
# this also sets TCP_USER_TIMEOUT for the underlying socket to this value | |
'grpc.keepalive_timeout_ms', | |
1000 | |
) | |
] | |
channel = grpc.insecure_channel("localhost:1234", options=channel_opts) | |
stub = helloworld_pb2_grpc.GreeterStub(channel) | |
while True: | |
# Some large message | |
msg = 'Hello' * 100000 | |
try: | |
response = stub.SayHello(helloworld_pb2.HelloRequest(name=msg), timeout=0.3) | |
except RpcError as e: | |
# Handle timeouts here | |
pass | |
else: | |
# Do something with response | |
pass | |
time.sleep(0.01) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment