From f8b5474ac910bc8a32d33ee8ed20be828940ec02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C=E8=A7=82=E5=AF=9F=E6=97=A5=E5=BF=97?= Date: Tue, 26 Aug 2025 04:57:13 +0800 Subject: [PATCH] fix: add retry for grpc connection --- src/grpc/manager.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/grpc/manager.py b/src/grpc/manager.py index 47039f0..33375ab 100644 --- a/src/grpc/manager.py +++ b/src/grpc/manager.py @@ -1,5 +1,5 @@ import asyncio -import logging +import json from typing import Awaitable, Callable, Type from async_lru import alru_cache @@ -32,11 +32,27 @@ class WrapperManager: self._decrypt_queue = asyncio.Queue() async def init(self, url: str, secure: bool): + service_config_json = json.dumps( + { + "methodConfig": [ + { + "name": [{}], + "retryPolicy": { + "maxAttempts": 5, + "initialBackoff": "0.1s", + "maxBackoff": "1s", + "backoffMultiplier": 2, + "retryableStatusCodes": ["UNAVAILABLE", "INTERNAL"], + }, + } + ] + } + ) + options = ((ChannelOptions.SingleThreadedUnaryStream, 1), ("grpc.service_config", service_config_json)) if secure: - self._channel = secure_channel(url, credentials=ssl_channel_credentials(), - options=((ChannelOptions.SingleThreadedUnaryStream, 1),)) + self._channel = secure_channel(url, credentials=ssl_channel_credentials(), options=options) else: - self._channel = insecure_channel(url, options=((ChannelOptions.SingleThreadedUnaryStream, 1),)) + self._channel = insecure_channel(url, options=options) self._stub = WrapperManagerServiceStub(self._channel) return self @@ -132,7 +148,7 @@ class WrapperManager: return @retry(retry=((retry_if_exception_type(WrapperManagerException)) & ( - retry_if_not_exception_message('no available instance')) & (retry_if_not_exception_message('no available lyrics'))), + retry_if_not_exception_message('no available instance'))), wait=wait_random_exponential(multiplier=1, max=60), stop=stop_after_attempt(32), before_sleep=before_sleep_log(it(GlobalLogger).logger, "WARNING")) async def lyrics(self, adam_id: str, language: str, region: str) -> str: