mirror of
https://github.com/tiennm99/keepalive.git
synced 2026-10-11 03:13:31 +00:00
- Each connect attempt, including initialization, times out after 1 minute, and PostgreSQL DSNs without connect_timeout get connect_timeout=30, since lib/pq honours the context only while dialing. - A service increments right after connecting, so a restart writes even when the interval outlasts the process. - A config path that is a directory (Docker's stand-in for a missing bind-mount source) fails with a clear message. - Generated service names take the first free suffix, and suffixed names are reserved so an explicit name cannot silently collide with them. - counter_key inside a service's config map is rejected instead of being overwritten. - Unknown keys in the config file log a warning without blocking start. - A .dockerignore keeps local configs and .env out of the build context.
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
func init() {
|
|
Registry["postgresql"] = func(cfg Config) (Adapter, error) {
|
|
url, err := cfg.Required("url")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &postgresAdapter{
|
|
url: withDefaultConnectTimeout(url),
|
|
key: cfg.Optional("counter_key", "counter"),
|
|
}, nil
|
|
}
|
|
Registry["postgres"] = Registry["postgresql"]
|
|
}
|
|
|
|
type postgresAdapter struct {
|
|
db *sql.DB
|
|
url string
|
|
key string
|
|
}
|
|
|
|
func (a *postgresAdapter) Connect(ctx context.Context) error {
|
|
db, err := sql.Open("postgres", a.url)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
a.db = db
|
|
if err := a.db.PingContext(ctx); err != nil {
|
|
a.db.Close()
|
|
return err
|
|
}
|
|
if err := a.ensureInitialized(ctx); err != nil {
|
|
a.db.Close()
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *postgresAdapter) ensureInitialized(ctx context.Context) error {
|
|
if _, err := a.db.ExecContext(ctx, `
|
|
CREATE TABLE IF NOT EXISTS keepalive (
|
|
key TEXT PRIMARY KEY,
|
|
value BIGINT NOT NULL DEFAULT 0
|
|
)`); err != nil {
|
|
return err
|
|
}
|
|
_, err := a.db.ExecContext(ctx,
|
|
`INSERT INTO keepalive (key, value) VALUES ($1, 0) ON CONFLICT (key) DO NOTHING`,
|
|
a.key,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (a *postgresAdapter) Increment(ctx context.Context) (int64, error) {
|
|
tx, err := a.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
var value int64
|
|
if err := tx.QueryRowContext(ctx,
|
|
`UPDATE keepalive SET value = value + 1 WHERE key = $1 RETURNING value`,
|
|
a.key,
|
|
).Scan(&value); err != nil {
|
|
tx.Rollback()
|
|
return 0, err
|
|
}
|
|
return value, tx.Commit()
|
|
}
|
|
|
|
func (a *postgresAdapter) Close(_ context.Context) error {
|
|
if a.db == nil {
|
|
return nil
|
|
}
|
|
return a.db.Close()
|
|
}
|
|
|
|
// withDefaultConnectTimeout adds connect_timeout when the DSN has none:
|
|
// lib/pq honours the context only while dialing, so a server that accepts the
|
|
// connection but never answers the startup handshake would hang forever.
|
|
func withDefaultConnectTimeout(dsn string) string {
|
|
if strings.Contains(dsn, "connect_timeout") {
|
|
return dsn
|
|
}
|
|
switch {
|
|
case !strings.Contains(dsn, "://"):
|
|
return dsn + " connect_timeout=30"
|
|
case strings.Contains(dsn, "?"):
|
|
return dsn + "&connect_timeout=30"
|
|
default:
|
|
return dsn + "?connect_timeout=30"
|
|
}
|
|
}
|