gorb

package module
v0.0.0-...-89f05fd Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2018 License: MIT Imports: 8 Imported by: 0

README

gorb

Gorb is a package built upon Gorp and strongly inspired by Nap. It allows load balancing in a round robin style between master and replica databases.

Read queries are executed by replica.
Write queries are executed by the master. Use .MasterCanRead(true) to perform WRITE queries with the master and READ queries with the master and replica.

Gorp documentation

Usage

func main() {
	dsns := "root:root@tcp(master:3306)/masterpwd;"
	dsns += "root:root@tcp(replica:3306)/replicapwd"
	b, err := gorb.NewBalancer("mysql", gorp.MySQLDialect{"InnoDB", "utf8mb4"}	, dsns)
	if err != nil{
	  panic(err)
	}
	err = b.Ping()
	if err != nil{
	  panic(err)
	}
	
	// Master is used only for write queries, this is the default value
	b.MasterCanRead(false) 

	// Master is used for write and read queries
	b.MasterCanRead(true)
	
	count, err := b.SelectInt("SELECT COUNT(*) FROM mytable")
	if err != nil{
	  panic(err)
	}
	fmt.Println(count)
}

License

Gorb is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

type Balancer struct {
	*gorp.DbMap // master
	// contains filtered or unexported fields
}

Balancer embeds multiple connections to physical db and automatically distributes queries with a round-robin scheduling around a master/replica replication. Write queries are executed by the Master. Read queries(SELECTs) are executed by the replicas.

func NewBalancer

func NewBalancer(driverName string, dialect gorp.Dialect, sources string) (*Balancer, error)

NewBalancer opens a connection to each physical db. dataSourceNames must be a semi-comma separated list of DSNs with the first one being used as the master and the rest as replicas.

func (*Balancer) Close

func (b *Balancer) Close() error

Close closes all physical databases

func (*Balancer) Get

func (b *Balancer) Get(i interface{}, keys ...interface{}) (interface{}, error)

Get https://godoc.org/gopkg.in/gorp.v2#DbMap.Get

func (*Balancer) GetAllDbs

func (b *Balancer) GetAllDbs() []*gorp.DbMap

GetAllDbs returns each underlying physical database, the first one is the master

func (*Balancer) Master

func (b *Balancer) Master() *gorp.DbMap

Master returns the master database

func (*Balancer) MasterCanRead

func (b *Balancer) MasterCanRead(read bool)

MasterCanRead adds the master physical database to the replicas list if read==true so that the master can perform WRITE queries AND READ queries .

func (*Balancer) Ping

func (b *Balancer) Ping() error

Ping verifies if a connection to each physical database is still alive, establishing a connection if necessary.

func (*Balancer) Prepare

func (b *Balancer) Prepare(query string) (Stmt, error)

Prepare creates a prepared statement for later queries or executions on each physical database. Multiple queries or executions may be run concurrently from the returned statement. This is equivalent to running: Prepare() using database/sql

https://godoc.org/gopkg.in/gorp.v2#DbMap.Prepare

func (*Balancer) Replica

func (b *Balancer) Replica() *gorp.DbMap

Replica returns one of the replicas databases

func (*Balancer) Select

func (b *Balancer) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error)

Select https://godoc.org/gopkg.in/gorp.v2#DbMap.Select

func (*Balancer) SelectFloat

func (b *Balancer) SelectFloat(query string, args ...interface{}) (float64, error)

SelectFloat https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectFloat

func (*Balancer) SelectInt

func (b *Balancer) SelectInt(query string, args ...interface{}) (int64, error)

SelectInt https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectInt

func (*Balancer) SelectNullFloat

func (b *Balancer) SelectNullFloat(query string, args ...interface{}) (sql.NullFloat64, error)

SelectNullFloat https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullFloat

func (*Balancer) SelectNullInt

func (b *Balancer) SelectNullInt(query string, args ...interface{}) (sql.NullInt64, error)

SelectNullInt https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullInt

func (*Balancer) SelectNullStr

func (b *Balancer) SelectNullStr(query string, args ...interface{}) (sql.NullString, error)

SelectNullStr https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectNullStr

func (*Balancer) SelectOne

func (b *Balancer) SelectOne(holder interface{}, query string, args ...interface{}) error

SelectOne https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectOne

func (*Balancer) SelectStr

func (b *Balancer) SelectStr(query string, args ...interface{}) (string, error)

SelectStr https://godoc.org/gopkg.in/gorp.v2#DbMap.SelectStr

func (*Balancer) SetConnMaxLifetime

func (b *Balancer) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused. Expired connections may be closed lazily before reuse. If d <= 0, connections are reused forever.

func (*Balancer) SetMaxIdleConns

func (b *Balancer) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections If MaxOpenConns is greater than 0 but less than the new MaxIdleConns then the new MaxIdleConns will be reduced to match the MaxOpenConns limit If n <= 0, no idle connections are retained.

func (*Balancer) SetMaxOpenConns

func (b *Balancer) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

func (*Balancer) TraceOff

func (b *Balancer) TraceOff()

TraceOff https://godoc.org/gopkg.in/gorp.v2#DbMap.TraceOff

func (*Balancer) TraceOn

func (b *Balancer) TraceOn(prefix string, logger gorp.GorpLogger)

TraceOn https://godoc.org/gopkg.in/gorp.v2#DbMap.TraceOn

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