Skip to content

Helper database driver for databases that use credential rotation

License

Notifications You must be signed in to change notification settings

clavinjune/rotator

Repository files navigation

rotator Go Reference

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)
	}
}

About

Helper database driver for databases that use credential rotation

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published