Skip to content

Commit 2468ade

Browse files
feat(#148): added semaphore to API fetcher
1 parent 0cc11fa commit 2468ade

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/stac_fastapi/indexed/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class _Settings(ApiSettings):
1717
True # container images set this to false after installing extensions in build
1818
)
1919
create_empty_index_if_missing: bool = False
20+
max_concurrency: int = 10
2021

2122

2223
@lru_cache(maxsize=1)
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
from typing import Any, Dict, Type
1+
from asyncio import Semaphore
2+
from typing import Any, Dict, Final
23

3-
from stac_index.io.readers import SourceReader, get_reader_for_uri
4-
from stac_pydantic.collection import Collection
5-
from stac_pydantic.item import Item
4+
from stac_index.io.readers import get_reader_for_uri
65

7-
_source_reader_instances: Dict[str, Type[SourceReader]] = {}
6+
from stac_fastapi.indexed.settings import get_settings
87

8+
# If we ever support multi-catalog indexes, or if we see catalogs
9+
# whose STAC items come from different domains, it might be worth
10+
# maintaining one semaphore per domain as a single semaphore may
11+
# unnecessarily limit concurrency when hosts could handle more.
12+
_semaphore: Final[Semaphore] = Semaphore(value=get_settings().max_concurrency)
913

10-
async def fetch_dict(uri: str) -> Dict[str, Any]:
11-
return await get_reader_for_uri(uri=uri).load_json_from_uri(uri)
12-
13-
14-
async def fetch_item(uri: str) -> Item:
15-
return Item(**(await fetch_dict(uri)))
1614

17-
18-
async def fetch_collection(uri: str) -> Collection:
19-
return Collection(**(await fetch_dict(uri)))
15+
async def fetch_dict(uri: str) -> Dict[str, Any]:
16+
async with _semaphore:
17+
return await get_reader_for_uri(uri=uri).load_json_from_uri(uri)

0 commit comments

Comments
 (0)