Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/picterra/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,28 @@ def _download_to_file(url: str, filename: str):


def _upload_file_to_blobstore(upload_url: str, filename: str):
"""Uploads a file to a blobstore URL

Note that since the common upload pattern is:
1. get upload URL, POST
2. upload file to URL (this function), PUT
3. commit upload, POST
4. wait for operation to complete, GET
and we retry if throttled only on GETs, we add two pauses before and after this
function so that we avoid incurring in the rate limit in the POST-PUT-POST sequence.

Args:
upload_url: The pre-signed URL to upload the file to.
filename: The local path to the file to upload.

Raises:
ValueError: If the provided filename does not exist or is not a file.
APIError: If the upload to the blobstore fails.

"""
if not (os.path.exists(filename) and os.path.isfile(filename)):
raise ValueError("Invalid file: " + filename)
time.sleep(2)
with open(
filename, "rb"
) as f: # binary recommended by requests stream upload (see link below)
Expand All @@ -109,6 +129,7 @@ def _upload_file_to_blobstore(upload_url: str, filename: str):
if not resp.ok:
logger.error("Error when uploading to blobstore %s" % upload_url)
raise APIError(resp.text)
time.sleep(2)

@julienr julienr Jul 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something feels weird about this fix. This adds an unconditional sleep to the upload unless I'm missing something, which to me is a sign we should fix our throttling instead of the code here.

Like adding a sleep in a retry is fine, but we shouldn't need sleep in the happy path - this just means our throttling limit is too low.

(Maybe I'm misunderstanding the fix / this isn't ready yet)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry I didn't notice I assigned to you (I guess GH did it on his own), indeed the issue is on the server side and I'm fixing it there

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no all good, you didn't assign it but I still get notif (I'm repo owner or something I guess). So all good if its just wip/debug stuff :)



def multipolygon_to_polygon_feature_collection(mp):
Expand Down
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import asyncio
import time

import pytest


@pytest.fixture(autouse=True)
def fast_sleep(monkeypatch):
"""Replace real sleeps with fast no-ops to keep tests quick and deterministic.

This fixture is autouse so it applies to all tests. If a specific test
requires real sleeping behavior, it can explicitly restore the original
functions.
"""

# Patch time.sleep to a no-op
monkeypatch.setattr(time, "sleep", lambda s: None)

# Patch asyncio.sleep to an async no-op
async def _async_noop(_):
return None

monkeypatch.setattr(asyncio, "sleep", _async_noop)

yield
Loading