mrsql

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2020 License: MPL-2.0 Imports: 12 Imported by: 0

README

MRSQL (Master/Replica SQL Driver)

Like https://github.com/tsenart/nap/ but converted into the Golang SQL "middleware" driver.

Known Limitations

Work in progress

Still work in progress! You may suggest pull requests but keep in mind it is still being activelly worked on..

Retries are untested

Retries are experimental and not tested well!

SetConnMaxLifetime, SetMaxIdleConns, SetMaxOpenConns on sql.DB doesn't work

Package sql.DB creates multiple calls to mrsql's Open but receives the same "virtual" connection every time. So you need to set these restrictions on the mrsql.Driver instance returned from Register() instead.

You can set sql.DB idle conns to the number of cpus or something like that and keep MaxConns unlimited:

b, err := sql.Open(drv.DriverName(), strings.Join(DSNs, ";"))
if err != nil {
    log.Fatalf("Unable to connect to DB: %v\n", err)
}

// configure
db.SetMaxIdleConns(runtime.NumCPU())

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNetwork network error
	ErrNetwork = errors.New("Network error or connection refused")

	// ErrNoConnection there is no no connection in the pool and no way to create a new one
	ErrNoConnection = errors.New("No connection available")
)
View Source
var (
	DebugLogEnabled = false
)
View Source
var (
	// DefaultHealthCheckPeriod is default period to do health checks on failed dbs
	DefaultHealthCheckPeriod = 40 * time.Millisecond
)

Functions

func RequireMasterCtx

func RequireMasterCtx(parent context.Context) context.Context

RequireMasterCtx creates a new context instructing the path to use masters only

func RequireReplicaCtx

func RequireReplicaCtx(parent context.Context) context.Context

RequireReplicaCtx creates a new context instructing the path to use masters only

Types

type Driver

type Driver interface {
	DriverName() string

	SetMaxOpenConns(n int)
	GetMaxOpenConns() int

	SetMaxIdleConns(n int)
	GetMaxIdleConns() int

	SetMaxConnLifetime(d time.Duration)
	GetMaxConnLifetime() time.Duration

	SetMaxRetryCount(retry int)
	GetMaxRetryCount() int
}

func Register

func Register(driverName string) (Driver, error)

Register initializes and registers our wrapped database driver. On success it returns the driver which can be further configured and whose DriverName() method returns the name to be used as sql.Open argument instead of the original driverName.

type PoolStats added in v0.2.1

type PoolStats struct {
	Opened, RealOpened, Idle, Closed int64
	TotalOpenWaitDuration            int64
	AvgQueryTime                     time.Duration
}

type Stmt

type Stmt interface {
	Close() error
	Exec(...interface{}) (sql.Result, error)
	Query(...interface{}) (*sql.Rows, error)
	QueryRow(...interface{}) *sql.Row
}

Stmt is an aggregate prepared statement. It holds a prepared statement for each underlying physical db.

Jump to

Keyboard shortcuts

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