Skip to content

Commit

Permalink
remove caching https connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rteqs committed Aug 21, 2024
1 parent 65dd64f commit b78c188
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions latch_cli/tinyrequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ def __exit__(self, type, value, tb):
self._resp.close()


_cache: Dict[str, HTTPSConnection] = {}


def _req(
method: str,
url: str,
Expand All @@ -92,16 +89,15 @@ def _req(
headers["Content-Type"] = "application/json"

port = parts.port if parts.port is not None else 443
key = f"{parts.hostname}:{port}"

# ayush: this is not threadsafe (as in the connection could be created
# multiple times) but its probably fine
if _cache.get(key) is None:
_cache[key] = HTTPSConnection(parts.hostname, port, timeout=90)

# todo(rteqs): removed caching single connections, implement a connection pool instead.

retries = 3
while True:
conn = _cache[key]
conn = HTTPSConnection(parts.hostname, port, timeout=90)

try:
conn.request(
Expand All @@ -113,8 +109,6 @@ def _req(
resp = conn.getresponse()
break
except ConnectionError as e:
_cache[key] = HTTPSConnection(parts.hostname, port, timeout=90)

retries += 1
if retries > 3:
raise e
Expand All @@ -132,8 +126,7 @@ def request(
stream: bool = False,
num_retries: int = 3,
) -> TinyResponse:
"""
Send HTTP request. Retry on 500s or ConnectionErrors.
"""Send HTTP request. Retry on 500s or ConnectionErrors.
Implements exponential backoff between retries.
"""
err = None
Expand Down

0 comments on commit b78c188

Please sign in to comment.