Skip to content

Commit

Permalink
Merge pull request #11 from trendmicro/fix/singleton_pattern
Browse files Browse the repository at this point in the history
Fix the singleton pattern & release conn not working as expect
  • Loading branch information
t0mz06 authored Jan 16, 2024
2 parents 3e24a12 + 563a330 commit 135617a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
3 changes: 0 additions & 3 deletions src/pytmv1/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
class HTTPConnectionPool(HTTPUrllib):
@typing.no_type_check
def urlopen(self, method, url, **kwargs):
kwargs.pop("preload_content", "")
return super().urlopen(
method,
url,
pool_timeout=5,
release_conn=True,
preload_content=False,
**kwargs,
)

Expand Down
39 changes: 23 additions & 16 deletions src/pytmv1/caller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

import logging
import threading
from functools import lru_cache
from logging import Logger
from typing import Callable, List, Optional, Type, Union
from typing import Any, Callable, List, Optional, Type, Union

from . import utils
from .core import Core
Expand Down Expand Up @@ -66,9 +67,9 @@
from .results import MultiResult, Result

log: Logger = logging.getLogger(__name__)
lock = threading.Lock()


@lru_cache(maxsize=None)
def client(
name: str,
token: str,
Expand All @@ -78,7 +79,7 @@ def client(
connect_timeout: int = 30,
read_timeout: int = 30,
) -> Client:
"""Helper function to initialize a :class:`Client`.
"""Synchronized Helper function to initialize a :class:`Client`.
:param name: Identify the application using this library.
:type name: str
Expand All @@ -96,22 +97,28 @@ def client(
:type connect_timeout: int
:rtype: Client
"""
lock.acquire()
client_ = _client(
appname=name,
token=token,
url=url,
pool_connections=pool_connections,
pool_maxsize=pool_maxsize,
connect_timeout=connect_timeout,
read_timeout=read_timeout,
)
lock.release()
return client_


@lru_cache(maxsize=1)
def _client(**kwargs: Any) -> Client:
log.debug(
"Initializing new client with [Appname=%s, Token=*****, URL=%s]",
name,
url,
)
return Client(
Core(
name,
token,
url,
pool_connections,
pool_maxsize,
connect_timeout,
read_timeout,
)
kwargs["appname"],
kwargs["url"],
)
return Client(Core(**kwargs))


class Client:
Expand Down
14 changes: 9 additions & 5 deletions tests/integration/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import psutil
import pytest

import pytmv1


def test_conn_opened_with_single_call_single_client_is_one(client):
client.get_exception_list()
Expand All @@ -19,18 +21,20 @@ def test_conn_opened_with_multi_call_single_client_is_one(


def test_conn_opened_with_multi_processing_single_client_is_one(client):
threads = thread_list(lambda: client.get_exception_list())
threads = thread_list(lambda: client.add_alert_note("1", "dummy note"))
for t in threads:
t.start()
for t in threads:
t.join()
assert len(list_tcp_conn()) == 1


def test_conn_opened_with_multi_processing_multi_client_is_one(
pytestconfig, client
):
threads = thread_list(lambda: client.get_exception_list())
def test_conn_opened_with_multi_processing_multi_client_is_one(url):
threads = thread_list(
lambda: pytmv1.client(
"appname", "dummyToken", url
).get_exception_list()
)
for t in threads:
t.start()
for t in threads:
Expand Down

0 comments on commit 135617a

Please sign in to comment.