From 1e7a96cd5c856f62792d6b0a26b1851ff2dfa713 Mon Sep 17 00:00:00 2001 From: boholder Date: Sun, 17 Mar 2024 14:07:16 +0800 Subject: [PATCH] feat: change usage of nacos-sdk to async style --- src/app/config/__init__.py | 5 ----- src/app/nacos.py | 29 ++++++++++------------------- tests/conftest.py | 4 ++-- tests/test_nacos.py | 12 +++++++----- 4 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/app/config/__init__.py b/src/app/config/__init__.py index 5b4d287..7d0b81e 100644 --- a/src/app/config/__init__.py +++ b/src/app/config/__init__.py @@ -1,5 +1,4 @@ import argparse -import concurrent.futures import sys from pathlib import Path @@ -107,10 +106,6 @@ def parse_nacos_config_if_enabled(cls, value, values): # Make sure to use "config.CONFIG.xxx" to access it, so it can retrieve newly updated values CONFIG: Configs = Configs() -# Global process pool -# for tasks that may block the event loop or hold GIL for long time -PROCESS_EXECUTOR = concurrent.futures.ProcessPoolExecutor() - _ARG_PARSER = argparse.ArgumentParser(description="Web Server") _ARG_PARSER.add_argument("-c", "--config", help="Path of the config file", type=Path) _ARG_PARSER.add_argument("--debug", help="Enable debug mode", action="store_true") diff --git a/src/app/nacos.py b/src/app/nacos.py index a74d922..2635a5d 100644 --- a/src/app/nacos.py +++ b/src/app/nacos.py @@ -1,12 +1,11 @@ -import asyncio import logging -import nacos_sdk_rust_binding_py as nacos +import nacos_sdk_rust_binding_py as nacos_sdk from app import config -_NACOS_CLIENT: nacos.NacosNamingClient -_NACOS_SERVICE_INSTANCE: nacos.NacosServiceInstance +_NACOS_CLIENT: nacos_sdk.AsyncNacosNamingClient +_NACOS_SERVICE_INSTANCE: nacos_sdk.NacosServiceInstance log = logging.getLogger(__name__) @@ -17,22 +16,15 @@ async def initialize_nacos(): # ref: https://github.com/python/cpython/issues/100090 build_nacos_client() build_nacos_service_instance() - await asyncio.get_event_loop().run_in_executor( - config.PROCESS_EXECUTOR, register, _NACOS_CLIENT, _NACOS_SERVICE_INSTANCE - ) - + await register() -def register(client: nacos.NacosNamingClient = None, service_instance: nacos.NacosServiceInstance = None): - if client is None: - client = _NACOS_CLIENT - if service_instance is None: - service_instance = _NACOS_SERVICE_INSTANCE +async def register(): try: - client.register_instance( + await _NACOS_CLIENT.register_instance( service_name=config.CONFIG.app.name, group=config.CONFIG.nacos.group, - service_instance=service_instance, + service_instance=_NACOS_SERVICE_INSTANCE, ) except Exception as e: log.error("Failed to register onto nacos", exc_info=e) @@ -42,8 +34,8 @@ def register(client: nacos.NacosNamingClient = None, service_instance: nacos.Nac def build_nacos_client(): global _NACOS_CLIENT - _NACOS_CLIENT = nacos.NacosNamingClient( - nacos.ClientOptions( + _NACOS_CLIENT = nacos_sdk.AsyncNacosNamingClient( + nacos_sdk.ClientOptions( server_addr=config.CONFIG.nacos.server_addr, namespace=config.CONFIG.nacos.namespace, app_name=config.CONFIG.app.name, @@ -51,11 +43,10 @@ def build_nacos_client(): password=config.CONFIG.nacos.password, ) ) - log.debug(f"Nacos client: {_NACOS_CLIENT}") def build_nacos_service_instance(): global _NACOS_SERVICE_INSTANCE - _NACOS_SERVICE_INSTANCE = nacos.NacosServiceInstance( + _NACOS_SERVICE_INSTANCE = nacos_sdk.NacosServiceInstance( ip=config.CONFIG.app.outer_host, port=config.CONFIG.app.outer_port ) diff --git a/tests/conftest.py b/tests/conftest.py index 69a0e5c..3231241 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -31,12 +31,12 @@ def wrapper(content: dict): @pytest.fixture def mock_nacos_client_with_config(monkeypatch, configure_with): """ - Mock the NacosNamingClient then we don't need to actually connect to the nacos server. + Mock the AsyncNacosNamingClient then we don't need to actually connect to the nacos server. """ mock_client = MagicMock() mock_client.return_value = mock_client - monkeypatch.setattr("nacos_sdk_rust_binding_py.NacosNamingClient", mock_client) + monkeypatch.setattr("nacos_sdk_rust_binding_py.AsyncNacosNamingClient", mock_client) def wrapper(content: dict): """ diff --git a/tests/test_nacos.py b/tests/test_nacos.py index 86ba047..bb251df 100644 --- a/tests/test_nacos.py +++ b/tests/test_nacos.py @@ -1,4 +1,5 @@ -import nacos_sdk_rust_binding_py as nacos_client +import nacos_sdk_rust_binding_py as nacos_sdk +import pytest from app import config, nacos @@ -12,7 +13,7 @@ def test_build_nacos_client(mock_nacos_client_with_config): nacos.build_nacos_client() - actual_options: nacos_client.ClientOptions = mock_client.call_args[0][0] + actual_options: nacos_sdk.ClientOptions = mock_client.call_args[0][0] # Assert initializing with default config values # since they're not set in config file assert actual_options.server_addr == server_addr @@ -29,7 +30,7 @@ def test_build_nacos_client_with_default_username_and_password(mock_nacos_client nacos.build_nacos_client() - actual_options: nacos_client.ClientOptions = mock_client.call_args[0][0] + actual_options: nacos_sdk.ClientOptions = mock_client.call_args[0][0] assert actual_options.username == DEFAULT_NACOS_CONFIG.username assert actual_options.password == DEFAULT_NACOS_CONFIG.password @@ -43,12 +44,13 @@ def test_build_service_instance(configure_with): assert nacos._NACOS_SERVICE_INSTANCE.port == DEFAULT_APP_CONFIG.outer_port -def test_register_onto_nacos(mock_nacos_client_with_config): +@pytest.mark.asyncio +async def test_register_onto_nacos(mock_nacos_client_with_config): mock_client = mock_nacos_client_with_config( {"app": {"enable_nacos": True}, "nacos": {"server_addr": "", "enable_auth": True}} ) - nacos.register() + await nacos.register() actual_kwargs = mock_client.register_instance.call_args.kwargs assert actual_kwargs["service_name"] == DEFAULT_APP_CONFIG.name