Skip to content

Commit

Permalink
feat: change usage of nacos-sdk to async style
Browse files Browse the repository at this point in the history
  • Loading branch information
boholder committed Mar 17, 2024
1 parent abe2db8 commit 1e7a96c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
5 changes: 0 additions & 5 deletions src/app/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import argparse
import concurrent.futures
import sys
from pathlib import Path

Expand Down Expand Up @@ -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")
Expand Down
29 changes: 10 additions & 19 deletions src/app/nacos.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand All @@ -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)
Expand All @@ -42,20 +34,19 @@ 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,
username=config.CONFIG.nacos.username,
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
)
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
12 changes: 7 additions & 5 deletions tests/test_nacos.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 1e7a96c

Please sign in to comment.