Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting the session pool size in niquests #190

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion grafana_client/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import niquests.auth
from urllib3.exceptions import InsecureRequestWarning

from .client import DEFAULT_TIMEOUT, AsyncGrafanaClient, GrafanaClient
from .client import DEFAULT_SESSION_POOL_SIZE, DEFAULT_TIMEOUT, AsyncGrafanaClient, GrafanaClient
from .elements import (
Admin,
Alerting,
Expand Down Expand Up @@ -72,6 +72,7 @@ def __init__(
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
session_pool_size=DEFAULT_SESSION_POOL_SIZE,
):
self.client = GrafanaClient(
auth,
Expand All @@ -83,6 +84,7 @@ def __init__(
timeout=timeout,
user_agent=user_agent,
organization_id=organization_id,
session_pool_size=session_pool_size,
)
self.url = None
self.admin = Admin(self.client)
Expand Down
9 changes: 7 additions & 2 deletions grafana_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from niquests import HTTPError, Timeout

DEFAULT_TIMEOUT: float = 5.0
DEFAULT_SESSION_POOL_SIZE: int = 10


class GrafanaException(Exception):
Expand Down Expand Up @@ -87,6 +88,7 @@ def __init__(
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
session_pool_size=DEFAULT_SESSION_POOL_SIZE,
):
self.auth = auth
self.verify = verify
Expand All @@ -95,6 +97,7 @@ def __init__(
self.url_port = port
self.url_path_prefix = url_path_prefix
self.url_protocol = protocol
self.session_pool_size = session_pool_size

def construct_api_url():
params = {
Expand All @@ -117,7 +120,7 @@ def construct_api_url():

self.user_agent = user_agent or f"{__appname__}/{__version__}"

self.s = niquests.Session()
self.s = niquests.Session(pool_maxsize=session_pool_size)
self.s.headers["User-Agent"] = self.user_agent

self.organization_id = organization_id
Expand Down Expand Up @@ -229,6 +232,7 @@ def __init__(
timeout=DEFAULT_TIMEOUT,
user_agent: str = None,
organization_id: int = None,
session_pool_size=DEFAULT_SESSION_POOL_SIZE,
):
super().__init__(
auth,
Expand All @@ -240,8 +244,9 @@ def __init__(
timeout=timeout,
user_agent=user_agent,
organization_id=organization_id,
session_pool_size=session_pool_size,
)
self.s = niquests.AsyncSession()
self.s = niquests.AsyncSession(pool_maxsize=session_pool_size)
self.s.headers.setdefault("Connection", "keep-alive")

def __getattr__(self, item):
Expand Down