pgdfailwrap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2019 License: BSD-2-Clause Imports: 4 Imported by: 0

README

pgdfailwrap GoDoc

pgdfailwrap is a wrapper driver for 'postgres' drivers that adds the capability to detect a node that is in readonly mode and reject such a connection in order to be resilient to database failover from a master to slave node. The typical use-case being database maintenance.

It aims in the direction of implementing 2 features found in libpq 10+: multiple hosts support and RW/RO connection selector but currently implements a poor mans version where connections will only be made to read+write nodes i.e. you can't choose. The author needed this for a production problem and found none of the Go PostgreSQL drivers supported this at the time of writing.

Only tested with lib/pq at the moment. Use at your own risk.

Usage

You'll import a PostgreSQL driver as per normal:

import (
    "database/sql"

    "github.com/hannesdejager/pgdfailwrap"
    _ "github.com/lib/pq"
)

but then do database initialization a bit differently:

func main() {
    var conStrs []string
    // master connection string
    conStrs = append(conStrs, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", masterHost, masterPort, user, password, dbName))
    // slave connection string
    conStrs = append(conStrs, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", slaveHost, slavePort, user, password, dbName))
    InitDriver(&pq.Driver{})
    sqlDb, err := sql.Open("postgres-with-failover", strings.Join(conStrs, ","))
    if err != nil {
        log.Fatal(err)
    }
    ...
}

Documentation

Overview

Package pgdfailwrap is a wrapper driver for 'postgres' drivers that adds the capability to detect a node that is in readonly mode and reject such a connection in order to be resilient to database failover from master to slave. The typical use case here is database maintenance.

Example use:

import (
	"database/sql"

	_ "github.com/lib/pq"
)

func main() {
	var conStrs []string
	conStrs = append(conStrs, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", masterHost, masterPort, user, password, dbName))
	conStrs = append(conStrs, fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", slaveHost, slavePort, user, password, dbName))
	InitDriver(&pq.Driver{})
	sqlDb, err := sql.Open("postgres-with-failover", strings.Join(conStrs, ","))
	if err != nil {
		log.Fatal(err)
	}
	...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitDriver

func InitDriver(delegate driver.Driver)

InitDriver initializes the wrapper driver taking an already initialized postgres driver as argument.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver satisfies the sql/driver/Driver interface

func (*Driver) Open

func (d *Driver) Open(connStr string) (driver.Conn, error)

Open will be called by sql.Open. The connection string is comma seperated pairs of connection strings that is compatible with the delegate driver being used i.e. conStr[,conStr...]

Jump to

Keyboard shortcuts

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