Home
Softono
gorb

gorb

Open source MIT Go
14
Stars
4
Forks
0
Issues
1
Watchers
7 years
Last Commit

About gorb

Gorb allows load balancing in a round robin style between master and slave databases.

Platforms

Web Self-hosted

Languages

Go

Links

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.