|
1 | | -from typing import Any, Dict, Type |
| 1 | +from asyncio import Semaphore |
| 2 | +from typing import Any, Dict, Final |
2 | 3 |
|
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 |
6 | 5 |
|
7 | | -_source_reader_instances: Dict[str, Type[SourceReader]] = {} |
| 6 | +from stac_fastapi.indexed.settings import get_settings |
8 | 7 |
|
| 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) |
9 | 13 |
|
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))) |
16 | 14 |
|
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