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

Fix connection timeout #456

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion src/pg_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas import DataFrame, Series
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy import text

from src.logger import set_log
from src.utils.query_file import open_query
Expand All @@ -21,8 +22,19 @@ class MultiInstanceDBFetcher:
"""

def __init__(self, db_urls: list[str]):
log.info("Initializing MultiInstanceDBFetcher")
self.connections = [
create_engine(f"postgresql+psycopg2://{url}") for url in db_urls
create_engine(
f"postgresql+psycopg2://{url}",
pool_pre_ping=True,
connect_args={
"keepalives": 1,
"keepalives_idle": 30,
"keepalives_interval": 10,
"keepalives_count": 5,
},
)
for url in db_urls
]

@classmethod
Expand Down Expand Up @@ -58,10 +70,14 @@ def get_solver_rewards(

# Here, we use the convention that we run the prod query for the first connection
# and the barn query to all other connections
log.info("Setting tcp_keepalives_idle to 900 for prod connection")
self.connections[0].execute(text("SET tcp_keepalives_idle = 900;"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably introduce a constant for this value.

log.info("Running prod query for first connection (in get_solver_rewards)")
results.append(
self.exec_query(query=batch_reward_query_prod, engine=self.connections[0])
)
for engine in self.connections[1:]:
log.info("Running barn query on other connections (in get_solver_rewards")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the long term plan here? It seems that not setting the time out for barn is fine. But it also makes it a bit more difficult to understand what is happening here.

results.append(
self.exec_query(query=batch_reward_query_barn, engine=engine)
)
Expand Down
Loading