Skip to content

Commit

Permalink
Valkey, Update packages, Less binds
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed May 13, 2024
1 parent b7003f5 commit 297d9ae
Show file tree
Hide file tree
Showing 15 changed files with 2,222 additions and 2,161 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
uses: actions/checkout@v4

- name: Install Nix
uses: cachix/install-nix-action@v25
uses: cachix/install-nix-action@v26
with:
nix_path: nixpkgs=channel:nixpkgs-23.11-darwin

Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyproj import Transformer

NAME = 'openaedmap-backend'
VERSION = '2.9.2'
VERSION = '2.10.0'
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'

Expand All @@ -28,7 +28,7 @@

POSTGRES_LOG = os.getenv('POSTGRES_LOG', '0').strip().lower() in ('1', 'true', 'yes')
POSTGRES_URL = 'postgresql+asyncpg://postgres:postgres@/postgres?host=/tmp/openaedmap-postgres'
REDIS_URL = os.getenv('REDIS_URL', 'unix:///tmp/openaedmap-redis.sock?protocol=3')
VALKEY_URL = os.getenv('VALKEY_URL', 'unix:///tmp/openaedmap-valkey.sock?protocol=3')

DEFAULT_CACHE_MAX_AGE = timedelta(minutes=1)
DEFAULT_CACHE_STALE = timedelta(minutes=5)
Expand Down
4 changes: 4 additions & 0 deletions config/postgres.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ unix_socket_directories = '/tmp/openaedmap-postgres'
shared_buffers = 512MB
effective_cache_size = 1GB

# increase statistics target
# reason: more accurate query plans
default_statistics_target = 150

# increase max connections
max_connections = 10000

Expand Down
8 changes: 4 additions & 4 deletions config/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ stopsignal=INT
stdout_logfile=data/supervisor/postgres.log
stderr_logfile=data/supervisor/postgres.log

[program:redis]
command=redis-server config/redis.conf
[program:valkey]
command=valkey-server config/valkey.conf
stopsignal=INT
stdout_logfile=data/supervisor/redis.log
stderr_logfile=data/supervisor/redis.log
stdout_logfile=data/supervisor/valkey.log
stderr_logfile=data/supervisor/valkey.log
4 changes: 2 additions & 2 deletions config/redis.conf → config/valkey.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# listen on socket
# reason: reduce latency
unixsocket /tmp/openaedmap-redis.sock
unixsocket /tmp/openaedmap-valkey.sock
unixsocketperm 700

# single-database mode
Expand All @@ -18,7 +18,7 @@ databases 1
locale-collate C

# disable persistence
# reason: redis is cache only, use postgres for persistence
# reason: valkey is cache only, use postgres for persistence
save ""
appendonly no

Expand Down
6 changes: 3 additions & 3 deletions db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from redis.asyncio import ConnectionPool, Redis
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine

from config import POSTGRES_LOG, POSTGRES_URL, REDIS_URL
from config import POSTGRES_LOG, POSTGRES_URL, VALKEY_URL
from utils import JSON_DECODE, JSON_ENCODE

_db_engine = create_async_engine(
Expand Down Expand Up @@ -45,10 +45,10 @@ async def db_write():
await session.commit()


_redis_pool = ConnectionPool().from_url(REDIS_URL)
_redis_pool = ConnectionPool().from_url(VALKEY_URL)


@asynccontextmanager
async def redis():
async def valkey():
async with Redis(connection_pool=_redis_pool) as r:
yield r
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from anyio import create_task_group
from fastapi import APIRouter, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette_compress import CompressMiddleware

from json_response import CustomJSONResponse
from middlewares.cache_control_middleware import CacheControlMiddleware
from middlewares.cache_response_middleware import CacheResponseMiddleware
from middlewares.compress_middleware import CompressMiddleware
from middlewares.profiler_middleware import ProfilerMiddleware
from middlewares.version_middleware import VersionMiddleware
from services.aed_service import AEDService
Expand Down
6 changes: 3 additions & 3 deletions middlewares/cache_response_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from zstandard import ZstdCompressor, ZstdDecompressor

from db import redis
from db import valkey
from middlewares.cache_control_middleware import make_cache_control, parse_cache_control
from models.cached_response import CachedResponse

Expand Down Expand Up @@ -154,7 +154,7 @@ def satisfy_response_start(self, message: Message) -> None:
async def _get_cached_response(url: URL) -> CachedResponse | None:
key = f'cache:{url.path}:{url.query}'

async with redis() as conn:
async with valkey() as conn:
value: bytes | None = await conn.get(key)

if value is None:
Expand All @@ -172,5 +172,5 @@ async def _set_cached_response(url: URL, cached: CachedResponse) -> None:

logging.debug('Caching response for %r', key)

async with redis() as conn:
async with valkey() as conn:
await conn.set(key, value, ex=ttl)
152 changes: 0 additions & 152 deletions middlewares/compress_middleware.py

This file was deleted.

4 changes: 2 additions & 2 deletions models/db/created_at_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from sqlalchemy import func
from sqlalchemy.dialects.postgresql import TIMESTAMP
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped, MappedAsDataclass, mapped_column


class CreatedAtMixin:
class CreatedAtMixin(MappedAsDataclass):
created_at: Mapped[datetime] = mapped_column(
TIMESTAMP(True),
init=False,
Expand Down
11 changes: 3 additions & 8 deletions openstreetmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import xmltodict
from authlib.integrations.httpx_client import OAuth2Auth
from httpx import HTTPStatusError
from sentry_sdk import trace

from config import CHANGESET_ID_PLACEHOLDER, DEFAULT_CHANGESET_TAGS, OPENSTREETMAP_API_URL
Expand Down Expand Up @@ -51,13 +52,7 @@ async def upload_osm_change(self, osm_change: str) -> str:
{
'osm': {
'changeset': {
'tag': [
{
'@k': k,
'@v': v,
}
for k, v in DEFAULT_CHANGESET_TAGS.items()
]
'tag': [{'@k': k, '@v': v} for k, v in DEFAULT_CHANGESET_TAGS.items()],
}
}
}
Expand Down Expand Up @@ -86,6 +81,6 @@ async def upload_osm_change(self, osm_change: str) -> str:
r.raise_for_status()

if not upload_resp.is_success:
raise Exception(f'Upload failed ({upload_resp.status_code}): {upload_resp.text}')
raise HTTPStatusError(f'Upload failed ({upload_resp.status_code}): {upload_resp.text}')

return changeset_id
Loading

0 comments on commit 297d9ae

Please sign in to comment.