repositorypackage
1.0.6
Repository: https://github.com/clavinjune/rotator.git
Documentation: pkg.go.dev
# README
rotator 
Helper database driver for databases that use credential rotation (e.g. Vault / Heroku). By using rotator, your choice of database driver will be wrapped, so the database datasource name will be dynamically rotated depends on how you want it to be rotated.
Usage
go get -u github.com/ClavinJune/rotator@latest
Example
package main
import (
"context"
"database/sql"
"log"
"time"
"github.com/ClavinJune/rotator"
"github.com/lib/pq"
)
func main() {
// Register your wrapper driver
rotator.RegisterRotationDriver(rotator.Opt{
// set Max Retry if there's something wrong when fetching / reopening the connection
MaxRetry: 3,
// set your uniq custom DriverName
DriverName: "example",
// set your choice of database driver
DriverBase: &pq.Driver{},
// set your way to fetch the DSN
Fetcher: rotator.FetcherFunc(func(ctx context.Context) (dsn string, err error) {
// fetch and return your datasource name here
return "postgres://user:password@localhost:5432/dbname", nil
}),
})
db, err := sql.Open("example", "this section will automatically filled by Fetcher func")
if err != nil {
panic(err)
}
defer db.Close()
db.SetMaxIdleConns(3)
db.SetMaxOpenConns(3)
db.SetConnMaxLifetime(3 * time.Second)
for i := 0; i < 10; i++ {
if err := db.Ping(); err != nil {
panic(err)
}
log.Println("connected")
time.Sleep(time.Second)
}
}