Files
keepalive/adapter/adapter.go
T
tiennm99 e3b7ea5011 feat: initial db-keepalive — pluggable adapters for 6 free-tier DBs
Consolidates the *-keepalive family (redis, valkey, postgresql, mysql,
mongodb, couchbase) into a single binary with a runtime DB_TYPE dispatch
and an Adapter interface. Original repos archived with a pointer here.
2026-05-11 20:15:35 +07:00

47 lines
1.1 KiB
Go

// Package adapter defines the contract every database driver implements.
//
// An Adapter connects to a single backing store, increments a counter on every
// keepalive tick (the cheapest write that proves the cluster is live), and
// releases its resources on Close. New databases plug in by adding a file in
// this package and registering a factory in Registry.
package adapter
import (
"context"
"fmt"
)
type Adapter interface {
Connect(ctx context.Context) error
Increment(ctx context.Context) (int64, error)
Close(ctx context.Context) error
}
type Factory func() (Adapter, error)
var Registry = map[string]Factory{}
func New(dbType string) (Adapter, error) {
f, ok := Registry[dbType]
if !ok {
return nil, fmt.Errorf("unknown DB_TYPE %q (known: %v)", dbType, Known())
}
return f()
}
func Known() []string {
out := make([]string, 0, len(Registry))
for k := range Registry {
out = append(out, k)
}
return out
}
func envOrFail(name string) (string, error) {
v, ok := lookupEnv(name)
if !ok || v == "" {
return "", fmt.Errorf("env %s is required", name)
}
return v, nil
}