Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Limiting postserve PG connection count #229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bin/postserve
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Usage:
[--pghost=<host>] [--pgport=<port>] [--dbname=<db>]
[--user=<user>] [--password=<password>]
[--test-geometry] [--verbose]
[--max-pg-conn <count>]
postserve --help
postserve --version

Expand All @@ -24,6 +25,7 @@ Options:
--no-feature-ids Disable feature ID generation, e.g. from osm_id.
Feature IDS are automatically disabled with PostGIS before v3
-g --test-geometry Validate all geometries produced by ST_AsMvtGeom(), and warn.
-c --max-pg-conn <c> Maximum number of PostgreSQL connections. Defaults to CPU count.
-v --verbose Print additional debugging information
--help Show this screen.
--version Show version.
Expand Down Expand Up @@ -66,6 +68,7 @@ def main(args):
disable_feature_ids=args['--no-feature-ids'],
test_geometry=args['--test-geometry'],
verbose=args.get('--verbose'),
max_pg_connections=args.get('--max-pg-conn'),
).serve()


Expand Down
15 changes: 12 additions & 3 deletions openmaptiles/postserve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
from functools import partial
from typing import Union, List, Any, Dict

Expand Down Expand Up @@ -137,7 +138,7 @@ class Postserve:

def __init__(self, url, port, pghost, pgport, dbname, user, password,
layers, tileset_path, sql_file, key_column, disable_feature_ids,
gzip, verbose, exclude_layers, test_geometry):
gzip, verbose, exclude_layers, test_geometry, max_pg_connections):
self.url = url
self.port = port
self.pghost = pghost
Expand All @@ -155,6 +156,10 @@ def __init__(self, url, port, pghost, pgport, dbname, user, password,
self.test_geometry = test_geometry
self.verbose = verbose

if max_pg_connections is None:
max_pg_connections = os.cpu_count() or 1
self.max_pg_connections = max_pg_connections

self.tileset = Tileset.parse(self.tileset_path)

def create_metadata(self,
Expand Down Expand Up @@ -254,12 +259,16 @@ def serve(self):
access_log.setLevel(logging.INFO if self.verbose else logging.ERROR)

print(f'Connecting to PostgreSQL at {self.pghost}:{self.pgport}, '
f'db={self.dbname}, user={self.user}...')
f'db={self.dbname}, user={self.user}, '
f'up to {self.max_pg_connections} simultaneous connections...')
io_loop = IOLoop.current()
self.pool = io_loop.run_sync(partial(
create_pool,
dsn=f"postgresql://{self.user}:{self.password}@"
f"{self.pghost}:{self.pgport}/{self.dbname}"))
f"{self.pghost}:{self.pgport}/{self.dbname}",
min_size=min(2, self.max_pg_connections),
max_size=self.max_pg_connections,
))
io_loop.run_sync(partial(self.init_connection))

if self.sql_file:
Expand Down