sqlxentrypoint

package module
v0.0.0-...-d5c4bc7 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2016 License: MIT Imports: 4 Imported by: 0

README

Motivation

Read from multiple slaves and write to single master while working with sql database cluster. Inspired by http://github.com/tsenart/nap

Install

$ go get github.com/datalinkE/sqlxentrypoint

Usage

package main

import (
  "log"

  "github.com/datalinkE/sqlx-entrypoint"
  _ "github.com/go-sql-driver/mysql" // Any sql.DB works
)

func main() {
  // The first DSN is assumed to be the master and all
  // other to be slaves
  dsns := "tcp://user:password@master/dbname;"
  dsns += "tcp://user:password@slave01/dbname;"
  dsns += "tcp://user:password@slave02/dbname"

  db, err := sqlx-entrypoint.Open("mysql", dsns)
  if err != nil {
    log.Fatal(err)
  }

  if err := db.Ping(); err != nil {
    log.Fatalf("Some physical database is unreachable: %s", err)
  }

  // Read queries are directed to slaves with Query and QueryRow.
  // Load distribution is round-robin.
  var count int
  err = db.Slave().QueryRow("SELECT COUNT(*) FROM sometable").Scan(&count)
  if err != nil {
    log.Fatal(err)
  }

  // Write queries are directed to the master with Exec.
  err = db.Master().Exec("UPDATE sometable SET something = 1")
  if err != nil {
    log.Fatal(err)
  }

  // Transactions always use the master
  tx, err := db.Master().Begin()
  if err != nil {
    log.Fatal(err)
  }
  // Do something transactional ...
  if err = tx.Commit(); err != nil {
    log.Fatal(err)
  }

}

Todo

  • Support other slave load balancing algorithms.

License

The MIT License (MIT)

Copyright (c) 2013 Tomás Senart http://github.com/tsenart/nap

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB is a entry point for logical database with multiple underlying physical databases forming a single master multiple slaves topology.

func Open

func Open(driverName, dataSourceNames string) (*DB, error)

Open concurrently opens each underlying 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 slaves.

func OpenEx

func OpenEx(driverName, dataSourceNames string, dataSourceOpener DataSourceOpener) (*DB, error)

OpenEx is a version of Open that uses DataSourceOpener in conjunction with sqlx.NewDB

func (*DB) Close

func (db *DB) Close() error

Close closes all physical databases concurrently, releasing any open resources.

func (*DB) Master

func (db *DB) Master() *sqlx.DB

Master returns the master physical database

func (*DB) Ping

func (db *DB) Ping() error

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

func (*DB) SetMaxIdleConns

func (db *DB) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool for each underlying physical db. 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 (*DB) SetMaxOpenConns

func (db *DB) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to each physical database. 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 (*DB) Slave

func (db *DB) Slave() *sqlx.DB

Slave returns one of the physical databases which is a slave

type DataSourceOpener

type DataSourceOpener interface {
	Open(driverName, dataSource string) (*sql.DB, error)
}

DataSourceOpener provides a ready-to-use *sql.DB from outside of sqlx

Jump to

Keyboard shortcuts

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