rotator

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2021 License: GPL-3.0 Imports: 3 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const (
	// FetcherDefaultMaxRetry defined the least retry number
	FetcherDefaultMaxRetry int = 2
)

Variables

This section is empty.

Functions

func RegisterRotationDriver

func RegisterRotationDriver(opt Opt)

RegisterRotationDriver registers the custom driver with the given opt. RegisterRotationDriver may trigger panic if the given opt is invalid

Types

type Fetcher

type Fetcher interface {
	// Fetch should returns the database datasource name / error
	Fetch(ctx context.Context) (dsn string, err error)
}

Fetcher interface helps you to fetch the database datasource name fetcher

type FetcherFunc

type FetcherFunc func(ctx context.Context) (dsn string, err error)

FetcherFunc is a single function form of Fetcher

func (FetcherFunc) Fetch

func (f FetcherFunc) Fetch(ctx context.Context) (dsn string, err error)

type Opt

type Opt struct {
	// MaxRetry is used for defining how many times the fetcher will be retried
	// if there something wrong when fetching / re-open the connection.
	// If MaxRetry is less than FetcherDefaultMaxRetry, Opt will use FetcherDefaultMaxRetry instead.
	MaxRetry int

	// DriverName is used for identifying the custom driver
	DriverName string

	// DriverBase is used for opening the database connection
	DriverBase driver.Driver

	// Fetcher is used for fetching the database datasource name
	Fetcher Fetcher
}

Opt is used for registering a new driver

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL