mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-26 11:08:13 +00:00
add event handler for default connection settings
This commit is contained in:
@@ -49,7 +49,7 @@ For running Nominatim:
|
|||||||
* [psutil](https://github.com/giampaolo/psutil)
|
* [psutil](https://github.com/giampaolo/psutil)
|
||||||
* [Jinja2](https://palletsprojects.com/p/jinja/)
|
* [Jinja2](https://palletsprojects.com/p/jinja/)
|
||||||
* [SQLAlchemy](https://www.sqlalchemy.org/) (1.4+ with greenlet support)
|
* [SQLAlchemy](https://www.sqlalchemy.org/) (1.4+ with greenlet support)
|
||||||
* [asyncpg](https://magicstack.github.io/asyncpg)
|
* [asyncpg](https://magicstack.github.io/asyncpg) (0.8+)
|
||||||
* [PyICU](https://pypi.org/project/PyICU/)
|
* [PyICU](https://pypi.org/project/PyICU/)
|
||||||
* [PyYaml](https://pyyaml.org/) (5.1+)
|
* [PyYaml](https://pyyaml.org/) (5.1+)
|
||||||
* [datrie](https://github.com/pytries/datrie)
|
* [datrie](https://github.com/pytries/datrie)
|
||||||
|
|||||||
@@ -7,12 +7,14 @@
|
|||||||
"""
|
"""
|
||||||
Implementation of classes for API access via libraries.
|
Implementation of classes for API access via libraries.
|
||||||
"""
|
"""
|
||||||
from typing import Mapping, Optional
|
from typing import Mapping, Optional, cast, Any
|
||||||
import asyncio
|
import asyncio
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from sqlalchemy import text, event
|
||||||
from sqlalchemy.engine.url import URL
|
from sqlalchemy.engine.url import URL
|
||||||
from sqlalchemy.ext.asyncio import create_async_engine
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||||||
|
import asyncpg
|
||||||
|
|
||||||
from nominatim.config import Configuration
|
from nominatim.config import Configuration
|
||||||
from nominatim.apicmd.status import get_status, StatusResult
|
from nominatim.apicmd.status import get_status, StatusResult
|
||||||
@@ -33,10 +35,29 @@ class NominatimAPIAsync:
|
|||||||
host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
|
host=dsn.get('host'), port=int(dsn['port']) if 'port' in dsn else None,
|
||||||
query={k: v for k, v in dsn.items()
|
query={k: v for k, v in dsn.items()
|
||||||
if k not in ('user', 'password', 'dbname', 'host', 'port')})
|
if k not in ('user', 'password', 'dbname', 'host', 'port')})
|
||||||
self.engine = create_async_engine(dburl,
|
self.engine = create_async_engine(
|
||||||
connect_args={"server_settings": {"jit": "off"}},
|
dburl, future=True,
|
||||||
future=True)
|
connect_args={'server_settings': {
|
||||||
|
'DateStyle': 'sql,european',
|
||||||
|
'max_parallel_workers_per_gather': '0'
|
||||||
|
}})
|
||||||
|
asyncio.get_event_loop().run_until_complete(self._query_server_version())
|
||||||
|
asyncio.get_event_loop().run_until_complete(self.close())
|
||||||
|
|
||||||
|
if self.server_version >= 110000:
|
||||||
|
@event.listens_for(self.engine.sync_engine, "connect") # type: ignore[misc]
|
||||||
|
def _on_connect(dbapi_con: Any, _: Any) -> None:
|
||||||
|
cursor = dbapi_con.cursor()
|
||||||
|
cursor.execute("SET jit_above_cost TO '-1'")
|
||||||
|
|
||||||
|
|
||||||
|
async def _query_server_version(self) -> None:
|
||||||
|
try:
|
||||||
|
async with self.engine.begin() as conn:
|
||||||
|
result = await conn.scalar(text('SHOW server_version_num'))
|
||||||
|
self.server_version = int(cast(str, result))
|
||||||
|
except asyncpg.PostgresError:
|
||||||
|
self.server_version = 0
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
""" Close all active connections to the database. The NominatimAPIAsync
|
""" Close all active connections to the database. The NominatimAPIAsync
|
||||||
|
|||||||
Reference in New Issue
Block a user