0.2.79 - Slow api fixes

This commit is contained in:
ponzischeme89
2026-08-19 18:08:00 +12:00
parent 590e069366
commit 0782545013
41 changed files with 1820 additions and 317 deletions
+32 -1
View File
@@ -294,8 +294,39 @@ type Store struct {
pool *pgxpool.Pool
}
// defaultMaxConns is the floor on the connection pool.
//
// pgxpool's own default is max(4, GOMAXPROCS), which on the NAS this runs on is four —
// and four is fewer than one household's televisions. Every one of them polls
// /v1/status every ten seconds, and that poll alone reads the feature policy, the
// notification preferences, the preference revision, the theme and the request
// allowlist. Queries that each take a millisecond then queue behind each other for
// hundreds, and the waiting is invisible: the query time is honest and the request is
// slow anyway. A deployment that has stated pool_max_conns for itself keeps its own
// answer — this replaces a default nobody chose, not a decision somebody made.
const defaultMaxConns = 16
// minConns keeps a few connections open. Establishing one costs a TCP connection, TLS
// where it is configured and Postgres' own backend fork, which is several milliseconds
// paid by whichever request happened to arrive after an idle period — most often the
// first thing somebody does after switching a television on.
const minConns = 4
func Open(ctx context.Context, databaseURL string) (*Store, error) {
pool, err := pgxpool.New(ctx, databaseURL)
config, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
return nil, fmt.Errorf("store: parse database url: %w", err)
}
if !strings.Contains(databaseURL, "pool_max_conns") && config.MaxConns < defaultMaxConns {
config.MaxConns = defaultMaxConns
}
if !strings.Contains(databaseURL, "pool_min_conns") && config.MinConns < minConns {
config.MinConns = minConns
}
// Every query in the gateway passes through here, which is what makes a slow request
// able to say how much of itself was Postgres.
config.ConnConfig.Tracer = queryTracer{}
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return nil, fmt.Errorf("store: connect: %w", err)
}