| 1 |
package pg |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"fmt" |
| 6 |
"net/url" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
_ "github.com/jackc/pgx/v5/stdlib" // PostgreSQL driver |
| 12 |
|
| 13 |
"heckel.io/ntfy/v2/db" |
| 14 |
) |
| 15 |
|
| 16 |
// Advisory lock keys. PostgreSQL advisory locks share one database-wide key space, so every |
| 17 |
// ntfy key is defined here, following the "ntfy"+2586+letter scheme |
| 18 |
const ( |
| 19 |
SchemaLockKey = int64(0x6e7466792586a) // Schema setup serialization (transaction-scoped, see db/schema) |
| 20 |
) |
| 21 |
|
| 22 |
// Open opens a PostgreSQL connection pool for a primary database. It pings the database |
| 23 |
// to verify connectivity before returning. |
| 24 |
func Open(dsn string) (*db.Host, error) { |
| 25 |
d, err := open(dsn) |
| 26 |
if err != nil { |
| 27 |
return nil, fmt.Errorf("failed to open database: %w", err) |
| 28 |
} |
| 29 |
if err := d.DB.Ping(); err != nil { |
| 30 |
return nil, fmt.Errorf("database ping failed on %v: %w", d.Addr, err) |
| 31 |
} |
| 32 |
return d, nil |
| 33 |
} |
| 34 |
|
| 35 |
// OpenReplica opens a PostgreSQL connection pool for a read replica. Unlike Open, it does |
| 36 |
// not ping the database, since replicas are health-checked in the background by db.DB. |
| 37 |
func OpenReplica(dsn string) (*db.Host, error) { |
| 38 |
return open(dsn) |
| 39 |
} |
| 40 |
|
| 41 |
// open opens a PostgreSQL database connection pool from a DSN string. It supports custom |
| 42 |
// query parameters for pool configuration: pool_max_conns (default 10), pool_max_idle_conns, |
| 43 |
// pool_conn_max_lifetime, and pool_conn_max_idle_time. These parameters are stripped from |
| 44 |
// the DSN before passing it to the driver. |
| 45 |
func open(dsn string) (*db.Host, error) { |
| 46 |
u, err := url.Parse(dsn) |
| 47 |
if err != nil { |
| 48 |
return nil, fmt.Errorf("invalid database URL: %w", err) |
| 49 |
} |
| 50 |
switch u.Scheme { |
| 51 |
case "postgres", "postgresql": |
| 52 |
// OK |
| 53 |
default: |
| 54 |
return nil, fmt.Errorf("invalid database URL scheme %q, must be \"postgres\" or \"postgresql\" (URL: %s)", u.Scheme, censorPassword(u)) |
| 55 |
} |
| 56 |
q := u.Query() |
| 57 |
maxOpenConns, err := extractIntParam(q, "pool_max_conns", 10) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
maxIdleConns, err := extractIntParam(q, "pool_max_idle_conns", 0) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
connMaxLifetime, err := extractDurationParam(q, "pool_conn_max_lifetime", 0) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
connMaxIdleTime, err := extractDurationParam(q, "pool_conn_max_idle_time", 0) |
| 70 |
if err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
u.RawQuery = q.Encode() |
| 74 |
d, err := sql.Open("pgx", u.String()) |
| 75 |
if err != nil { |
| 76 |
return nil, err |
| 77 |
} |
| 78 |
d.SetMaxOpenConns(maxOpenConns) |
| 79 |
if maxIdleConns > 0 { |
| 80 |
d.SetMaxIdleConns(maxIdleConns) |
| 81 |
} |
| 82 |
if connMaxLifetime > 0 { |
| 83 |
d.SetConnMaxLifetime(connMaxLifetime) |
| 84 |
} |
| 85 |
if connMaxIdleTime > 0 { |
| 86 |
d.SetConnMaxIdleTime(connMaxIdleTime) |
| 87 |
} |
| 88 |
return &db.Host{ |
| 89 |
Addr: u.Host, |
| 90 |
DB: d, |
| 91 |
}, nil |
| 92 |
} |
| 93 |
|
| 94 |
func extractIntParam(q url.Values, key string, defaultValue int) (int, error) { |
| 95 |
s := q.Get(key) |
| 96 |
if s == "" { |
| 97 |
return defaultValue, nil |
| 98 |
} |
| 99 |
q.Del(key) |
| 100 |
v, err := strconv.Atoi(s) |
| 101 |
if err != nil { |
| 102 |
return 0, fmt.Errorf("invalid %s value %q: %w", key, s, err) |
| 103 |
} |
| 104 |
return v, nil |
| 105 |
} |
| 106 |
|
| 107 |
// censorPassword returns a string representation of the URL with the password replaced by "*****". |
| 108 |
func censorPassword(u *url.URL) string { |
| 109 |
if password, hasPassword := u.User.Password(); hasPassword { |
| 110 |
return strings.Replace(u.String(), ":"+password+"@", ":*****@", 1) |
| 111 |
} |
| 112 |
return u.String() |
| 113 |
} |
| 114 |
|
| 115 |
func extractDurationParam(q url.Values, key string, defaultValue time.Duration) (time.Duration, error) { |
| 116 |
s := q.Get(key) |
| 117 |
if s == "" { |
| 118 |
return defaultValue, nil |
| 119 |
} |
| 120 |
q.Del(key) |
| 121 |
d, err := time.ParseDuration(s) |
| 122 |
if err != nil { |
| 123 |
return 0, fmt.Errorf("invalid %s value %q: %w", key, s, err) |
| 124 |
} |
| 125 |
return d, nil |
| 126 |
} |