[nominatim-web] Add support for read-only postgres replica



commit 1ee9d27c5da79d69bd7dd6151dc42499f0948e23
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Fri Apr 2 14:50:15 2021 +0200

    Add support for read-only postgres replica

 app/main.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
---
diff --git a/app/main.py b/app/main.py
index 2dac75f..4807d9e 100644
--- a/app/main.py
+++ b/app/main.py
@@ -13,6 +13,7 @@ class Settings(BaseSettings):
     redis_host: str = "localhost"
     redis_port: int = 6379
     postgres_url: str = "dbname=nominatim host=127.0.0.1 user=postgres"
+    postgres_ro_url: str = "dbname=nominatim host=127.0.0.1 user=postgres"
 
 
 settings = Settings()
@@ -21,6 +22,7 @@ redis_conn = redis.Redis(
     host=settings.redis_host, port=settings.redis_port, decode_responses=True
 )
 postgres = psycopg2.connect(settings.postgres_url)
+postgres_ro = psycopg2.connect(settings.postgres_ro_url)
 
 app = FastAPI()
 
@@ -64,7 +66,7 @@ def search(request: Request):
     if resp := redis_conn.get(key):
         return Response(content=resp, media_type="application/json")
 
-    with postgres.cursor() as cur:
+    with postgres_ro.cursor() as cur:
         cur.execute("SELECT * FROM search WHERE key = %s", (key,))
         if resp := cur.fetchone():
             redis_conn.setex(key, 600, resp[2])
@@ -97,7 +99,7 @@ def reverse(request: Request):
     if resp := redis_conn.get(key):
         return Response(content=resp, media_type="application/json")
 
-    with postgres.cursor() as cur:
+    with postgres_ro.cursor() as cur:
         cur.execute("SELECT * FROM reverse WHERE key = %s", (key,))
         if resp := cur.fetchone():
             redis_conn.setex(key, 600, resp[2])


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]