Skip to content

Commit 45ce7d7

Browse files
author
mingsing
committed
use aiohttp
Signed-off-by: mingsing <[email protected]>
1 parent 453afd1 commit 45ce7d7

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

dapr/aio/clients/health.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.
1414
"""
15+
import aiohttp
1516
import asyncio
16-
import urllib.request
17-
import urllib.error
1817
import time
1918

2019
from dapr.clients.http.conf import DAPR_API_TOKEN_HEADER, USER_AGENT_HEADER, DAPR_USER_AGENT
@@ -32,21 +31,24 @@ async def wait_until_ready():
3231
timeout = float(settings.DAPR_HEALTH_TIMEOUT)
3332

3433
start = time.time()
35-
while True:
36-
try:
37-
req = urllib.request.Request(health_url, headers=headers)
38-
with urllib.request.urlopen(req, context=DaprHealth.get_ssl_context()) as response:
39-
if 200 <= response.status < 300:
40-
break
41-
except urllib.error.URLError as e:
42-
print(f'Health check on {health_url} failed: {e.reason}')
43-
except Exception as e:
44-
print(f'Unexpected error during health check: {e}')
45-
46-
remaining = (start + timeout) - time.time()
47-
if remaining <= 0:
48-
raise TimeoutError(f'Dapr health check timed out, after {timeout}.')
49-
await asyncio.sleep(min(1.0, remaining))
34+
ssl_context = DaprHealth.get_ssl_context()
35+
36+
connector = aiohttp.TCPConnector(ssl=ssl_context)
37+
async with aiohttp.ClientSession(connector=connector) as session:
38+
while True:
39+
try:
40+
async with session.get(health_url, headers=headers) as response:
41+
if 200 <= response.status < 300:
42+
break
43+
except aiohttp.ClientError as e:
44+
print(f'Health check on {health_url} failed: {e}')
45+
except Exception as e:
46+
print(f'Unexpected error during health check: {e}')
47+
48+
remaining = (start + timeout) - time.time()
49+
if remaining <= 0:
50+
raise TimeoutError(f'Dapr health check timed out, after {timeout}.')
51+
await asyncio.sleep(min(1.0, remaining))
5052

5153
@staticmethod
5254
def get_ssl_context():

0 commit comments

Comments
 (0)