Skip to content

Commit

Permalink
Use a session object for requests
Browse files Browse the repository at this point in the history
By using a session object we can reuse the tcp connection.
We use one session per domain.

There is some uncertainty about the thread safety of Session
(psf/requests#2766), but it seems to work well
when not making requests to many different domains (1 in our case).
  • Loading branch information
Amund211 committed Dec 22, 2022
1 parent 397bcfc commit 9b34681
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
7 changes: 5 additions & 2 deletions src/prism/minecraft.py
@@ -1,14 +1,17 @@
import threading
from json import JSONDecodeError

import requests
from requests.exceptions import RequestException

from prism.ratelimiting import RateLimiter
from prism.requests import make_prism_requests_session

USERPROFILES_ENDPOINT = "https://api.mojang.com/users/profiles/minecraft"
REQUEST_LIMIT, REQUEST_WINDOW = 100, 60 # Max requests per time window

# Use a connection pool for the requests
SESSION = make_prism_requests_session()


class MojangAPIError(ValueError):
"""Exception raised when we receive an error from the Mojang api"""
Expand Down Expand Up @@ -37,7 +40,7 @@ def get_uuid(username: str) -> str | None: # pragma: nocover
try:
# Uphold our prescribed rate-limits
with limiter:
response = requests.get(f"{USERPROFILES_ENDPOINT}/{username}")
response = SESSION.get(f"{USERPROFILES_ENDPOINT}/{username}")
except RequestException as e:
raise MojangAPIError(
f"Request to Mojang API failed due to a connection error {e}"
Expand Down
11 changes: 7 additions & 4 deletions src/prism/overlay/antisniper_api.py
Expand Up @@ -4,12 +4,12 @@
from json import JSONDecodeError
from typing import Any

import requests
from cachetools import TTLCache
from requests.exceptions import RequestException

from prism.overlay.player import MISSING_WINSTREAKS, GamemodeName, Winstreaks
from prism.ratelimiting import RateLimiter
from prism.requests import make_prism_requests_session

logger = logging.getLogger(__name__)

Expand All @@ -28,6 +28,9 @@ class Flag(Enum):
LOWERCASE_DENICK_CACHE = TTLCache[str, str | None](maxsize=1024, ttl=60 * 60)
DENICK_MUTEX = threading.Lock()

# Use a connection pool for the requests
SESSION = make_prism_requests_session()


def set_denick_cache(nick: str, uuid: str | None) -> str | None:
"""Set the cache entry for nick, and return the uuid"""
Expand Down Expand Up @@ -66,7 +69,7 @@ def denick(
try:
# Uphold our prescribed rate-limits
with key_holder.limiter:
response = requests.get(
response = SESSION.get(
f"{DENICK_ENDPOINT}?key={key_holder.key}&nick={nick}"
)
except RequestException as e:
Expand Down Expand Up @@ -126,7 +129,7 @@ def get_estimated_winstreaks(
try:
# Uphold our prescribed rate-limits
with key_holder.limiter:
response = requests.get(
response = SESSION.get(
f"{WINSTREAK_ENDPOINT}?key={key_holder.key}&uuid={uuid}"
)
except RequestException as e:
Expand Down Expand Up @@ -228,7 +231,7 @@ def queue_data(
try:
# Uphold our prescribed rate-limits
with key_holder.limiter:
response = requests.get(
response = SESSION.get(
f"{ANTISNIPER_ENDPOINT}?key={key_holder.key}&name={name}"
)
except RequestException as e:
Expand Down
7 changes: 5 additions & 2 deletions src/prism/playerdata.py
@@ -1,14 +1,17 @@
from json import JSONDecodeError
from typing import Any

import requests
from requests.exceptions import RequestException

from prism.ratelimiting import RateLimiter
from prism.requests import make_prism_requests_session

PLAYER_ENDPOINT = "https://api.hypixel.net/player"
REQUEST_LIMIT, REQUEST_WINDOW = 100, 60 # Max requests per time window

# Use a connection pool for the requests
SESSION = make_prism_requests_session()


class HypixelAPIKeyHolder:
"""Class associating an api key with a RateLimiter instance"""
Expand Down Expand Up @@ -46,7 +49,7 @@ def get_player_data(
try:
# Uphold our prescribed rate-limits
with key_holder.limiter:
response = requests.get(
response = SESSION.get(
f"{PLAYER_ENDPOINT}?key={key_holder.key}&uuid={uuid}"
)
except RequestException as e:
Expand Down

0 comments on commit 9b34681

Please sign in to comment.