fix: UDP-logged QSOs fall back to the offline outbox on DB failure (were silently lost); speed up MySQL connect (batch migration checks into 1 query, connect direct-to-DB first) — was ~40s on a high-latency link
This commit is contained in:
+20
-5
@@ -137,10 +137,6 @@ func OpenMySQL(c MySQLConfig) (*sql.DB, error) {
|
||||
if !validDBIdent(name) {
|
||||
return nil, fmt.Errorf("invalid database name %q (letters, digits, underscore only)", name)
|
||||
}
|
||||
// Ensure the database exists (connect server-level first).
|
||||
if err := PingMySQL(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := sql.Open("mysql", c.dsn())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open mysql: %w", err)
|
||||
@@ -157,9 +153,28 @@ func OpenMySQL(c MySQLConfig) (*sql.DB, error) {
|
||||
// (surfacing as "Unknown database"). Idle connections are still recycled
|
||||
// after 90s, and the driver retries stale pooled connections.
|
||||
conn.SetConnMaxLifetime(0)
|
||||
// Connect DIRECTLY to the database first. Only if that fails — the DB doesn't
|
||||
// exist yet (first-time setup) or the server is unreachable — do we pay for the
|
||||
// server-level connect + CREATE DATABASE (two more handshakes). On a normal
|
||||
// startup against an existing DB this halves the connection round-trips, which
|
||||
// matters a lot on a high-latency remote MySQL (each handshake is several RTTs).
|
||||
if err := conn.Ping(); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("connect to %s: %w", name, err)
|
||||
if perr := PingMySQL(c); perr != nil {
|
||||
return nil, perr // creates the DB (or returns a clear "cannot create" error)
|
||||
}
|
||||
conn, err = sql.Open("mysql", c.dsn())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open mysql: %w", err)
|
||||
}
|
||||
conn.SetMaxOpenConns(50)
|
||||
conn.SetMaxIdleConns(10)
|
||||
conn.SetConnMaxIdleTime(90 * time.Second)
|
||||
conn.SetConnMaxLifetime(0)
|
||||
if err := conn.Ping(); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, fmt.Errorf("connect to %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
// Set the dialect before migrating so the runner takes the MySQL path
|
||||
// (per-statement, idempotent) rather than the SQLite transaction path.
|
||||
|
||||
Reference in New Issue
Block a user