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

Allow DB hosts to be a list for PostgreSQL connections. #335

Merged
merged 1 commit into from
May 4, 2018
Merged
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
9 changes: 9 additions & 0 deletions tilequeue/query/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import psycopg2
import threading
import ujson
import random


class ConnectionsContextManager(object):
Expand Down Expand Up @@ -38,6 +39,14 @@ def __init__(self, dbnames, conn_info, readonly=True):
self.readonly = readonly

def _make_conn(self, conn_info):
# if multiple hosts are provided, select one at random as a kind of
# simple load balancing.
host = conn_info.get('host')
if host and isinstance(host, list):
host = random.choice(host)
conn_info = conn_info.copy()
conn_info['host'] = host

conn = psycopg2.connect(**conn_info)
conn.set_session(readonly=self.readonly, autocommit=True)
register_hstore(conn)
Expand Down