drogonframework / drogonframework/drogon

PostgreSQL reconnect retry interval should be configurable

Open
#2,581 0 comments 1 reaction 0 assignees View on GitHub
good first issue
Dominant language
C++
Stars
14.3k
Forks
1.4k
Avg merge
1d 12h
Merged PRs (30d)
15

Description

## Description

When a PostgreSQL connection is lost, Drogon automatically attempts to
reconnect every 1 second.

This can cause unnecessary CPU usage, connection attempts, and excessive
logs when PostgreSQL is unavailable for an extended period.

I am using Drogon v1.9.13.

## Current behavior

In `orm_lib/src/DbClientImpl.cc`, the reconnect is scheduled with:

```cpp
loop->runAfter(1, [weakPtr, loop, closeConnPtr] {
...
});
```
As a result, when PostgreSQL is down, I see logs like:

```
Pg connection failed
Pg connection failed
Pg connection failed
Pg connection failed
```

approximately every second for each connection in the pool.

Expected behavior

It would be useful to have a configurable reconnect interval, for example:

`"reconnect_interval": 5`

The default could remain 1 second for backward compatibility.

Suggested implementation

Instead of hard-coding:

```cpp
loop->runAfter(1, [weakPtr, loop, closeConnPtr] {
...
});
```

the reconnect delay could be stored in the database client configuration:

```cpp
loop->runAfter(reconnectInterval, [weakPtr, loop, closeConnPtr] {
...
});
```
This would allow applications with unstable or remote PostgreSQL servers to
use a more appropriate retry interval.

Thanks!

Contributor guide

Open the contributing guide

Research direction

Start in orm_lib/src/DbClientImpl.cc at the hard-coded one-second runAfter reconnect scheduling, then trace how database client configuration is stored and read. Make the reconnect interval configurable while preserving a one-second default, and verify that PostgreSQL reconnect attempts use the configured value.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, postgresql
Domain
backend, database
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.