balancer

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2021 License: MIT Imports: 11 Imported by: 0

README

MySQL Load Balancer

Build Status codecov GoDoc Go Report Card

Usage

package main

import (
    "fmt"
    "log"

    "github.com/StudioSol/balancer"
)

func main() {
    config := balancer.Config{
        // Time in seconds in wich the health of the slaves is going to be checked
        CheckInterval: 3,

        // Wether the balancer should start checking health
        StartCheck: true,

        // Wether the queries executed by the balancer server should be logged
        TraceOn: false,

        // A balancer.Logger interface implementation
        Logger: log,

		// Slave servers' configuration
        ServersSettings: []balancer.ServerSettings{
            balancer.ServerSettings{
			// Name of the MySQL Slave Server
			Name: "slave 1",

			// Connection string of the MySQL user used for reading
			DSN: "user:password@tcp(127.0.0.1:3306)/database",

			// Connection string of the MySQL user used for status. The chosen
			// user must have "REPLICATION STATUS" permission
			ReplicationDSN: "replication_user:password@tcp(127.0.0.1:3306)/",

			// Maximum idle connections
			MaxIdleConns: 0,

			// Maximum open connections
			MaxOpenConns: 10,
		},
		// ...
    }

    db := balancer.New(config)

    server := db.PickServer()
    if server != nil {
        fmt.Println("No Server avaliable", server)
    }

    // Be happy! :)
    server.GetConnection().SelectOne(
    	// ...
    )
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Balancer

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

Balancer MySQL load balancer

func New

func New(config *Config) *Balancer

New creates a new instance of Balancer

func (*Balancer) GetServers

func (b *Balancer) GetServers() Servers

GetServers ...

func (*Balancer) PickServer

func (b *Balancer) PickServer() *Server

PickServer returns the best server at a given point in time

type Config

type Config struct {
	CheckInterval   int64
	StartCheck      bool
	TraceOn         bool
	Logger          Logger
	ServersSettings []ServerSettings
	StartupWait     time.Duration
}

Config configuration options for the balancer

type Logger

type Logger interface {
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Printf(format string, v ...interface{})
}

Logger ...

type Server

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

Server server representation

func (*Server) CheckHealth

func (s *Server) CheckHealth(traceOn bool, logger Logger)

CheckHealth check server's health and set it's state

func (*Server) GetConnection

func (s *Server) GetConnection() *gorp.DbMap

GetConnection returns server's connection

func (*Server) GetHealth

func (s *Server) GetHealth() *ServerHealth

GetHealth returns server's health state

func (*Server) GetName

func (s *Server) GetName() string

GetName returns server's name

type ServerHealth

type ServerHealth struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ServerHealth represents a Server health state

func (*ServerHealth) GetErr

func (h *ServerHealth) GetErr() error

GetErr returns server's last error

func (*ServerHealth) GetOpenConnections

func (h *ServerHealth) GetOpenConnections() *int

GetOpenConnections returns server's open connections

func (*ServerHealth) GetRunningConnections

func (h *ServerHealth) GetRunningConnections() *int

GetRunningConnections returns the number of connections that are not sleeping.

func (*ServerHealth) GetSecondsBehindMaster

func (h *ServerHealth) GetSecondsBehindMaster() *int

GetSecondsBehindMaster returns server's seconds behind master

func (*ServerHealth) IORunning added in v1.1.1

func (h *ServerHealth) IORunning() bool

GetSlaveRunning returns the IO status from slave

func (*ServerHealth) IsUP

func (h *ServerHealth) IsUP() bool

IsUP returns if the server is UP

type ServerSettings

type ServerSettings struct {
	Name             string
	DSN              string
	ReplicationDSN   string
	MaxIdleConns     int
	MaxOpenConns     int
	MaxLifetimeConns time.Duration
}

ServerSettings servers' configuration options

type Servers

type Servers []*Server

Servers - list of servers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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