We are sending multiple requests to a gRPC server.
But every once in a while we come across "Connection reset by peer"
error with UNAVAILABLE
status.
GRPC server: NestJS
Client: Python
Python version: 3.8
gRPCio version: 1.50.0
Code:
# Connect to server from client:
def connect_to_user_manager_server() -> AuthorizationControllerStub:
channel = grpc.insecure_channel(envs.USER_MANAGER_GRPC_URL, options=(
('grpc.keepalive_time_ms', 120000),
('grpc.keepalive_permit_without_calls', True),
))
stub = AuthorizationControllerStub(channel)
return stub
client = connect_to_user_manager_server()
user_response = client.CheckAuthorization(authorizationData(authorization=token, requiredRoles=roles))
You can add a retry logic in your client code using a library such as retrying or by implementing it manually.
For instance, you can implement retry logic using the retrying library:
This will retry the
connect_to_user_manager_server
function up to 3 times, with a 1 second delay between each retry.You can also implement it manually using a loop and try-catch block, like this:
This will also retry the connection to the server up to 3 times, with a 1 second delay between each retry.
You can adjust the number of retries and the delay time to fit your needs.