Create a global requests session

This should massively cut down on the number of TCP handshakes
This commit is contained in:
Jake Howard 2023-09-04 21:48:10 +01:00
parent fa065648d7
commit 36749ceac3
Signed by: jake
GPG key ID: 57AFB45680EDD477
5 changed files with 17 additions and 11 deletions

View file

@ -13,6 +13,8 @@ from wagtail.models import get_page_models as get_wagtail_page_models
HEADER_TAGS = ["h2", "h3", "h4", "h5", "h6"]
requests_session = requests.Session()
@dataclass
class TocEntry:
@ -113,6 +115,6 @@ def get_site_title() -> str:
@django_cache_decorator(time=21600)
def get_url_mime_type(url: str) -> Optional[str]:
try:
return requests.head(url).headers.get("Content-Type")
return requests_session.head(url).headers.get("Content-Type")
except requests.exceptions.RequestException:
return None

View file

@ -1,10 +1,11 @@
from functools import cache
from importlib.metadata import version
import requests
import yaml
from django_cache_decorator import django_cache_decorator
from website.common.utils import requests_session
PYGMENTS_VERSION = version("pygments")
PYGMENTS_VERSION_SLUG = PYGMENTS_VERSION.replace(".", "-")
@ -13,7 +14,7 @@ LINGUIST_DATA_URL = "https://raw.githubusercontent.com/github/linguist/master/li
@django_cache_decorator(time=600)
def _get_linguist_colours() -> dict[str, str]:
response = requests.get(LINGUIST_DATA_URL)
response = requests_session.get(LINGUIST_DATA_URL)
response.raise_for_status()

View file

@ -1,9 +1,10 @@
import requests
from django.conf import settings
from website.common.utils import requests_session
def get_unsplash_photo(image_id: str) -> dict:
response = requests.get(
response = requests_session.get(
f"https://api.unsplash.com/photos/{image_id}",
headers={
"Accept-Version": "v1",

View file

@ -1,17 +1,18 @@
import requests
from django.conf import settings
from website.common.utils import requests_session
API_LIMIT = 50
def is_valid_playlist(playlist_id: str) -> bool:
return requests.get(
return requests_session.get(
f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}"
).ok
def get_playlist(playlist_id: str) -> dict:
playlist_response = requests.get(
playlist_response = requests_session.get(
f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}",
params={"fields": "name,external_urls.spotify,tracks.total,description"},
)
@ -20,7 +21,7 @@ def get_playlist(playlist_id: str) -> dict:
tracks = []
for offset in range(0, playlist_data["tracks"]["total"], API_LIMIT):
tracks_response = requests.get(
tracks_response = requests_session.get(
f"https://{settings.SPOTIFY_PROXY_HOST}/v1/playlists/{playlist_id}/tracks",
params={
"offset": str(offset),

View file

@ -1,12 +1,13 @@
import requests
from django.conf import settings
from health_check.backends import BaseHealthCheckBackend
from website.common.utils import requests_session
class SpotifyHealthCheckBackend(BaseHealthCheckBackend):
def check_status(self) -> None:
try:
requests.get(
requests_session.get(
f"https://{settings.SPOTIFY_PROXY_HOST}/.health/"
).raise_for_status()
except Exception as e: