codefloe/forgejo-codefloe
Watch
4
12
Fork
You've already forked forgejo-codefloe
0

feat: add connect_timeout and pgx multi-host failover for database resilience #24

Merged
pat-s merged 1 commit from feat/db-connect-timeout-failover into v14.0/forgejo-codefloe 2026-03-12 10:52:30 +00:00
Owner

Summary

  • Add a CONNECT_TIMEOUT database setting (default 10s) injected into PostgreSQL connection strings as pgx's connect_timeout parameter. This ensures connections to unreachable hosts fail fast instead of waiting for the OS TCP timeout (~127s).
  • Change HOST_REPLICA handling to combine all replica hosts into a single pgx multi-host connection string instead of creating separate xorm Engines per host. Previously, if any listed replica host was unreachable, xorm's load balancer could route queries to the dead Engine, causing hangs. Now pgx handles failover transparently.
  • HOST_PRIMARY already supported pgx multi-host; the connect_timeout now ensures fast failover there too.

Context

When a database node goes down, Forgejo's connection attempts hang for ~127 seconds (OS TCP SYN retry timeout) per unreachable host. With multiple HAProxy endpoints listed for primary/replica connections, a single dead node blocks startup and causes query failures.

With these changes, worst-case failover takes ~10 seconds per unreachable host instead of ~127 seconds, and HAProxy handles load balancing across actual PostgreSQL nodes.

## Summary - Add a `CONNECT_TIMEOUT` database setting (default 10s) injected into PostgreSQL connection strings as pgx's `connect_timeout` parameter. This ensures connections to unreachable hosts fail fast instead of waiting for the OS TCP timeout (~127s). - Change `HOST_REPLICA` handling to combine all replica hosts into a single pgx multi-host connection string instead of creating separate xorm Engines per host. Previously, if any listed replica host was unreachable, xorm's load balancer could route queries to the dead Engine, causing hangs. Now pgx handles failover transparently. - `HOST_PRIMARY` already supported pgx multi-host; the `connect_timeout` now ensures fast failover there too. ## Context When a database node goes down, Forgejo's connection attempts hang for ~127 seconds (OS TCP SYN retry timeout) per unreachable host. With multiple HAProxy endpoints listed for primary/replica connections, a single dead node blocks startup and causes query failures. With these changes, worst-case failover takes ~10 seconds per unreachable host instead of ~127 seconds, and HAProxy handles load balancing across actual PostgreSQL nodes.
feat: add connect_timeout and pgx multi-host failover for database resilience
All checks were successful
testing / frontend-checks (pull_request) Successful in 2m2s
testing / test-remote-cacher (redis) (pull_request) Successful in 5m22s
testing / test-remote-cacher (valkey) (pull_request) Successful in 5m46s
testing / backend-checks (pull_request) Successful in 15m0s
testing / test-unit (pull_request) Successful in 11m25s
testing / test-remote-cacher (garnet) (pull_request) Successful in 5m46s
testing / test-e2e (pull_request) Successful in 31m58s
testing / test-mysql (pull_request) Successful in 34m30s
testing / test-pgsql (pull_request) Successful in 42m36s
testing / test-sqlite (pull_request) Successful in 42m7s
testing / test-remote-cacher (redict) (pull_request) Successful in 1m29s
testing / security-check (pull_request) Successful in 3m32s
milestone / set (pull_request_target) Has been skipped
issue-labels / backporting (pull_request_target) Has been skipped
b136f8f6ef
Add a CONNECT_TIMEOUT setting (default 10s) that is injected into PostgreSQL connection strings as pgx's connect_timeout parameter.
This ensures that connections to unreachable hosts fail fast instead of waiting for the OS TCP timeout (~127s with tcp_syn_retries=6).

Change HOST_REPLICA handling to combine all replica hosts into a single pgx multi-host connection string instead of creating separate xorm Engines per host.
Previously, if any listed replica host was unreachable, xorm's load balancer could route queries to the dead Engine, causing hangs.
Now pgx handles failover transparently — skipping unreachable hosts and connecting to the next available one.

HOST_PRIMARY already supported pgx multi-host via comma-separated hosts; the connect_timeout now ensures fast failover there too.
pat-s merged commit fbedbf92b7 into v14.0/forgejo-codefloe 2026-03-12 10:52:30 +00:00
pat-s deleted branch feat/db-connect-timeout-failover 2026-03-12 10:52:30 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
codefloe/forgejo-codefloe!24
No description provided.