Skip to content

Commit 2da4ba0

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
dummy
1 parent 7d6b741 commit 2da4ba0

File tree

5 files changed

+177
-4
lines changed

5 files changed

+177
-4
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ docker-shell-os:
6363

6464
.PHONY: test-elasticsearch
6565
test-elasticsearch:
66-
-$(run_es) /bin/bash -c 'export && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest'
66+
-$(run_es) /bin/bash -c 'pip install redis==6.4.0 export && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest'
6767
docker compose down
6868

6969
.PHONY: test-opensearch
7070
test-opensearch:
71-
-$(run_os) /bin/bash -c 'export && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest'
71+
-$(run_os) /bin/bash -c 'pip install redis==6.4.0 export && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest'
7272
docker compose down
7373

7474
.PHONY: test-datetime-filtering-es
7575
test-datetime-filtering-es:
76-
-$(run_es) /bin/bash -c 'export ENABLE_DATETIME_INDEX_FILTERING=true && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest -s --cov=stac_fastapi --cov-report=term-missing -m datetime_filtering'
76+
-$(run_es) /bin/bash -c 'pip install redis==6.4.0 && export ENABLE_DATETIME_INDEX_FILTERING=true && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest -s --cov=stac_fastapi --cov-report=term-missing -m datetime_filtering'
7777
docker compose down
7878

7979
.PHONY: test-datetime-filtering-os
8080
test-datetime-filtering-os:
81-
-$(run_os) /bin/bash -c 'export ENABLE_DATETIME_INDEX_FILTERING=true && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest -s --cov=stac_fastapi --cov-report=term-missing -m datetime_filtering'
81+
-$(run_os) /bin/bash -c 'pip install redis==6.4.0 && export ENABLE_DATETIME_INDEX_FILTERING=true && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest -s --cov=stac_fastapi --cov-report=term-missing -m datetime_filtering'
8282
docker compose down
8383

8484
.PHONY: test

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
[mypy-redis.*]
3+
ignore_missing_imports = True

stac_fastapi/core/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"pygeofilter~=0.3.1",
2020
"jsonschema~=4.0.0",
2121
"slowapi~=0.1.9",
22+
"redis==6.4.0",
2223
]
2324

2425
setup(

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
from stac_fastapi.core.base_settings import ApiBaseSettings
2525
from stac_fastapi.core.datetime_utils import format_datetime_range
2626
from stac_fastapi.core.models.links import PagingLinks
27+
from stac_fastapi.core.redis_utils import (
28+
connect_redis,
29+
get_prev_link,
30+
save_self_link,
31+
)
2732
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2833
from stac_fastapi.core.session import Session
2934
from stac_fastapi.core.utilities import filter_fields
@@ -255,6 +260,13 @@ async def all_collections(
255260
if parsed_sort:
256261
sort = parsed_sort
257262

263+
current_url = str(request.url)
264+
redis = None
265+
try:
266+
redis = await connect_redis()
267+
except Exception:
268+
redis = None
269+
258270
collections, next_token = await self.database.get_all_collections(
259271
token=token, limit=limit, request=request, sort=sort
260272
)
@@ -269,6 +281,22 @@ async def all_collections(
269281
},
270282
]
271283

284+
if redis:
285+
if next_token:
286+
await save_self_link(redis, next_token, current_url)
287+
288+
prev_link = await get_prev_link(redis, token)
289+
if prev_link:
290+
links.insert(
291+
0,
292+
{
293+
"rel": "prev",
294+
"type": "application/json",
295+
"method": "GET",
296+
"href": prev_link,
297+
},
298+
)
299+
272300
if next_token:
273301
next_link = PagingLinks(next=next_token, request=request).link_next()
274302
links.append(next_link)
@@ -499,6 +527,10 @@ async def post_search(
499527
HTTPException: If there is an error with the cql2_json filter.
500528
"""
501529
base_url = str(request.base_url)
530+
try:
531+
redis = await connect_redis()
532+
except Exception:
533+
redis = None
502534

503535
search = self.database.make_search()
504536

@@ -609,6 +641,22 @@ async def post_search(
609641
]
610642
links = await PagingLinks(request=request, next=next_token).get_links()
611643

644+
if redis:
645+
self_link = str(request.url)
646+
await save_self_link(redis, next_token, self_link)
647+
648+
prev_link = await get_prev_link(redis, token_param)
649+
if prev_link:
650+
links.insert(
651+
0,
652+
{
653+
"rel": "prev",
654+
"type": "application/json",
655+
"method": "GET",
656+
"href": prev_link,
657+
},
658+
)
659+
612660
return stac_types.ItemCollection(
613661
type="FeatureCollection",
614662
features=items,
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Utilities for connecting to and managing Redis connections."""
2+
3+
from typing import Optional
4+
5+
from pydantic_settings import BaseSettings
6+
from redis import asyncio as aioredis
7+
from redis.asyncio.sentinel import Sentinel
8+
9+
redis_pool: Optional[aioredis.Redis] = None
10+
11+
12+
class RedisSentinelSettings(BaseSettings):
13+
"""Configuration for connecting to Redis Sentinel."""
14+
15+
REDIS_SENTINEL_HOSTS: str = ""
16+
REDIS_SENTINEL_PORTS: str = "26379"
17+
REDIS_SENTINEL_MASTER_NAME: str = "master"
18+
REDIS_DB: int = 15
19+
20+
REDIS_MAX_CONNECTIONS: int = 10
21+
REDIS_RETRY_TIMEOUT: bool = True
22+
REDIS_DECODE_RESPONSES: bool = True
23+
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
24+
REDIS_HEALTH_CHECK_INTERVAL: int = 30
25+
26+
27+
class RedisSettings(BaseSettings):
28+
"""Configuration for connecting Redis Sentinel."""
29+
30+
REDIS_HOST: str = ""
31+
REDIS_PORT: int = 6379
32+
REDIS_DB: int = 0
33+
34+
REDIS_MAX_CONNECTIONS: int = 10
35+
REDIS_RETRY_TIMEOUT: bool = True
36+
REDIS_DECODE_RESPONSES: bool = True
37+
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
38+
REDIS_HEALTH_CHECK_INTERVAL: int = 30
39+
40+
41+
# Select the Redis or Redis Sentinel configuration
42+
redis_settings: BaseSettings = RedisSettings()
43+
44+
45+
async def connect_redis(settings: Optional[RedisSettings] = None) -> aioredis.Redis:
46+
"""Return a Redis connection."""
47+
global redis_pool
48+
settings = settings or redis_settings
49+
50+
if redis_pool is None:
51+
pool = aioredis.ConnectionPool(
52+
host=settings.REDIS_HOST,
53+
port=settings.REDIS_PORT,
54+
db=settings.REDIS_DB,
55+
max_connections=settings.REDIS_MAX_CONNECTIONS,
56+
decode_responses=settings.REDIS_DECODE_RESPONSES,
57+
retry_on_timeout=settings.REDIS_RETRY_TIMEOUT,
58+
health_check_interval=settings.REDIS_HEALTH_CHECK_INTERVAL,
59+
)
60+
redis_pool = aioredis.Redis(
61+
connection_pool=pool, client_name=settings.REDIS_CLIENT_NAME
62+
)
63+
return redis_pool
64+
65+
66+
async def connect_redis_sentinel(
67+
settings: Optional[RedisSentinelSettings] = None,
68+
) -> Optional[aioredis.Redis]:
69+
"""Return a Redis Sentinel connection."""
70+
global redis_pool
71+
72+
settings = settings or redis_settings
73+
74+
if (
75+
not settings.REDIS_SENTINEL_HOSTS
76+
or not settings.REDIS_SENTINEL_PORTS
77+
or not settings.REDIS_SENTINEL_MASTER_NAME
78+
):
79+
return None
80+
81+
hosts = [h.strip() for h in settings.REDIS_SENTINEL_HOSTS.split(",") if h.strip()]
82+
ports = [
83+
int(p.strip()) for p in settings.REDIS_SENTINEL_PORTS.split(",") if p.strip()
84+
]
85+
86+
if redis_pool is None:
87+
try:
88+
sentinel = Sentinel(
89+
[(h, p) for h, p in zip(hosts, ports)],
90+
decode_responses=settings.REDIS_DECODE_RESPONSES,
91+
)
92+
master = sentinel.master_for(
93+
service_name=settings.REDIS_SENTINEL_MASTER_NAME,
94+
db=settings.REDIS_DB,
95+
decode_responses=settings.REDIS_DECODE_RESPONSES,
96+
retry_on_timeout=settings.REDIS_RETRY_TIMEOUT,
97+
client_name=settings.REDIS_CLIENT_NAME,
98+
max_connections=settings.REDIS_MAX_CONNECTIONS,
99+
health_check_interval=settings.REDIS_HEALTH_CHECK_INTERVAL,
100+
)
101+
redis_pool = master
102+
103+
except Exception:
104+
return None
105+
106+
return redis_pool
107+
108+
109+
async def save_self_link(
110+
redis: aioredis.Redis, token: Optional[str], self_href: str
111+
) -> None:
112+
"""Save the self link for the current token with 30 min TTL."""
113+
if token:
114+
await redis.setex(f"nav:self:{token}", 1800, self_href)
115+
116+
117+
async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional[str]:
118+
"""Get the previous page link for the current token (if exists)."""
119+
if not token:
120+
return None
121+
return await redis.get(f"nav:self:{token}")

0 commit comments

Comments
 (0)