proxysql

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

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

Go to latest
Published: May 26, 2019 License: MIT Imports: 7 Imported by: 0

README

proxysql-go Build Status codecov Documentation Go Report Card

A thread safe package for building ProxySQL sidecars in go. Modify ProxySQL's configuration and routing rules on the fly, and easily write tests for these changes.

About

Proxysql-go is a package designed to help you build your own service discovery for ProxySQL. You can update ProxySQL's configuration on the fly. While you could send SQL queries to it yourself, those are hard to mock and test. This package allows you to update it with functions, and then mock and test your code that uses these functions.

Use

Example located here

Install the package
go get github.com/kirinrastogi/proxysql-go
Import the package
import (
  . "github.com/kirinrastogi/proxysql-go" // optional "."
)
Create an instance of the client
conn, err := NewProxySQL("/")

The string should be in the DSN format

Modify ProxySQL's configuration
err := conn.AddHost(Hostname("some-hostname"), HostgroupID(1))
if err != nil {...}
err = conn.PersistChanges()
if err != nil {...}
// ProxySQL is now using your configuration!

Running Tests

You must have docker installed with privileged access.

To install code dependencies type

dep ensure -v

Then, run the tests with

go test

Alternatively, you can run tests that don't require docker with

go test -short

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrConfigBadTable             = errors.New("Bad table value, must be one of 'mysql_servers', 'runtime_mysql_servers'")
	ErrConfigBadHostgroupID       = errors.New("Bad hostgroup value, must be in [0, 2147483648]")
	ErrConfigBadPort              = errors.New("Bad port value, must be in [0, 65535]")
	ErrConfigBadMaxConnections    = errors.New("Bad max_connections value, must be > 0")
	ErrConfigBadStatus            = errors.New("Bad status value, must be one of 'ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD'")
	ErrConfigBadWeight            = errors.New("Bad weight value, must be > 0")
	ErrConfigBadCompression       = errors.New("Bad compression value, must be in [0, 102400]")
	ErrConfigBadMaxReplicationLag = errors.New("Bad max_replication_lag value, must be in [0, 126144000]")
	ErrConfigBadUseSSL            = errors.New("Bad use_ssl value, must be one of 0, 1")
	ErrConfigBadMaxLatencyMS      = errors.New("Bad max_latency_ms value, must be > 0")
	ErrConfigDuplicateSpec        = errors.New("Bad function call, a value was specified twice")
	ErrConfigNoHostname           = errors.New("Bad hostname, must not be empty")
)

Functions

This section is empty.

Types

type Host

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

Host represents a row in ProxySQL's mysql_servers config table

func DefaultHost

func DefaultHost() *Host

DefaultHost returns a default host (in terms of the mysql_servers table). Note that hostname is left empty

func (*Host) Comment

func (h *Host) Comment() string

func (*Host) Compression

func (h *Host) Compression() int

func (*Host) HostgroupID

func (h *Host) HostgroupID() int

func (*Host) Hostname

func (h *Host) Hostname() string

func (*Host) MaxConnections

func (h *Host) MaxConnections() int

func (*Host) MaxLatencyMS

func (h *Host) MaxLatencyMS() int

func (*Host) MaxReplicationLag

func (h *Host) MaxReplicationLag() int

func (*Host) Port

func (h *Host) Port() int

func (*Host) SetComment

func (h *Host) SetComment(c string) *Host

func (*Host) SetCompression

func (h *Host) SetCompression(c int) *Host

func (*Host) SetHostgroupID

func (h *Host) SetHostgroupID(hg int) *Host

func (*Host) SetHostname

func (h *Host) SetHostname(hn string) *Host

func (*Host) SetMaxConnections

func (h *Host) SetMaxConnections(m int) *Host

func (*Host) SetMaxLatencyMS

func (h *Host) SetMaxLatencyMS(m int) *Host

func (*Host) SetMaxReplicationLag

func (h *Host) SetMaxReplicationLag(m int) *Host

func (*Host) SetPort

func (h *Host) SetPort(p int) *Host

func (*Host) SetStatus

func (h *Host) SetStatus(s string) *Host

func (*Host) SetUseSSL

func (h *Host) SetUseSSL(u int) *Host

func (*Host) SetWeight

func (h *Host) SetWeight(w int) *Host

func (*Host) Status

func (h *Host) Status() string

func (*Host) UseSSL

func (h *Host) UseSSL() int

func (*Host) Valid

func (h *Host) Valid() error

func (*Host) Weight

func (h *Host) Weight() int

type HostOpts

type HostOpts func(*hostQuery) *hostQuery

HostOpts is a type of function that is called with a hostQuery struct to specify a value in a query

func Comment

func Comment(c string) HostOpts

Comment sets the 'comment' in a query

func Compression

func Compression(c int) HostOpts

Compression sets the 'compression' in a query

func HostgroupID

func HostgroupID(h int) HostOpts

HostgroupID sets the 'hostgroup_id' in a query

func Hostname

func Hostname(h string) HostOpts

Hostname sets the 'hostname' in a query

func MaxConnections

func MaxConnections(c int) HostOpts

MaxConnections sets the 'max_connections' in a query

func MaxLatencyMS

func MaxLatencyMS(m int) HostOpts

MaxLatencyMS sets the 'max_latency_ms' in a query

func MaxReplicationLag

func MaxReplicationLag(m int) HostOpts

MaxReplicationLag sets the 'max_replication_lag' in a query

func Port

func Port(p int) HostOpts

Port sets the 'port' in a query

func Status

func Status(s string) HostOpts

Status sets the 'status' in a query

func Table

func Table(t string) HostOpts

Table sets the table in a query One of 'runtime_mysql_servers' or 'mysql_servers'

func UseSSL

func UseSSL(u int) HostOpts

UseSSL sets the 'use_ssl' in a query

func Weight

func Weight(w int) HostOpts

Weight sets the 'weight' in a query

type ProxySQL

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

func NewProxySQL

func NewProxySQL(dsn string) (*ProxySQL, error)

NewProxySQL will create & return a pointer to a ProxySQL struct. It will fail and return an error if the call to `sql.Open` fails. This will really only fail if there is no memory left to create a connection struct

func (*ProxySQL) AddHost

func (p *ProxySQL) AddHost(opts ...HostOpts) error

AddHost takes the configuration provided and inserts a host into ProxySQL with that configuration. This will return an error when a validation error of the configuration you specified occurs. This will propagate errors from sql.Exec as well

func (*ProxySQL) AddHosts

func (p *ProxySQL) AddHosts(hosts ...*Host) error

AddHosts will insert each of the hosts into mysql_servers this will error if any of the hosts are not valid this will propagate error from sql.Exec

func (*ProxySQL) All

func (p *ProxySQL) All(opts ...HostOpts) ([]*Host, error)

All returns the state of the table that you specify This will error if configuration validation fails, you should only call this with All(Table("runtime_mysql_servers")) or just All() for "mysql_servers" This will also propagate error from sql.Query, sql.Rows.Scan, sql.Rows.Err

func (*ProxySQL) Clear

func (p *ProxySQL) Clear() error

Clear is a convenience function to clear configuration

func (*ProxySQL) Close

func (p *ProxySQL) Close()

Close is a convenience function that calls the database/sql function on the underlying sql.DB connection

func (*ProxySQL) Conn

func (p *ProxySQL) Conn() *sql.DB

Conn is a convenience function that returns the underlying sql.DB connection

func (*ProxySQL) HostsLike

func (p *ProxySQL) HostsLike(opts ...HostOpts) ([]*Host, error)

HostsLike will return all hosts that match the given configuration This will error on configuration validation failing This will also propagate error from sql.Query, sql.Rows.Scan, sql.Rows.Err

func (*ProxySQL) PersistChanges

func (p *ProxySQL) PersistChanges() error

PersistChanges saves the mysql servers config to disk, and then loads it to the runtime. This must be called for ProxySQL's staged changes in the mysql_servers table to take effect and transfer to runtime_mysql_servers This propagates errors from sql.Exec

func (*ProxySQL) Ping

func (p *ProxySQL) Ping() error

Ping is a convenience function that calls the database/sql function on the underlying sql.DB connection

func (*ProxySQL) RemoveHost

func (p *ProxySQL) RemoveHost(host *Host) error

RemoveHost removes the host that matches the provided host's configuration exactly. This will propagate error from sql.Exec

func (*ProxySQL) RemoveHosts

func (p *ProxySQL) RemoveHosts(hosts ...*Host) error

RemoveHosts is a convenience function that removes hosts in the given slice This will propagate error from RemoveHost, or from sql.Exec

func (*ProxySQL) RemoveHostsLike

func (p *ProxySQL) RemoveHostsLike(opts ...HostOpts) error

RemoveHostsLike will remove all hosts that match the specified configuration This will error if configuration does not pass validation This will propagate error from sql.Exec

Source Files

  • api.go
  • host.go
  • proxysql.go
  • query.go
  • util.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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