Documentation ¶
Index ¶
- Constants
- Variables
- type ConnectionPool
- func (cp *ConnectionPool) Active() int64
- func (cp *ConnectionPool) Addr() string
- func (cp *ConnectionPool) Available() int64
- func (cp *ConnectionPool) Capacity() int64
- func (cp *ConnectionPool) Close()
- func (cp *ConnectionPool) Get(ctx context.Context) (*PooledConnection, error)
- func (cp *ConnectionPool) IdleClosed() int64
- func (cp *ConnectionPool) IdleTimeout() time.Duration
- func (cp *ConnectionPool) InUse() int64
- func (cp *ConnectionPool) MaxCap() int64
- func (cp *ConnectionPool) Open()
- func (cp *ConnectionPool) Put(pc *PooledConnection)
- func (cp *ConnectionPool) SetCapacity(capacity int) (err error)
- func (cp *ConnectionPool) SetIdleTimeout(idleTimeout time.Duration)
- func (cp *ConnectionPool) StatsJSON() string
- func (cp *ConnectionPool) WaitCount() int64
- func (cp *ConnectionPool) WaitTime() time.Duration
- type DirectConnection
- func (dc *DirectConnection) Begin() error
- func (dc *DirectConnection) Close()
- func (dc *DirectConnection) Commit() error
- func (dc *DirectConnection) Execute(sql string) (*mysql.Result, error)
- func (dc *DirectConnection) FieldList(table string, wildcard string) ([]*mysql.Field, error)
- func (dc *DirectConnection) GetAddr() string
- func (dc *DirectConnection) GetCharset() string
- func (dc *DirectConnection) GetDB() string
- func (dc *DirectConnection) IsAutoCommit() bool
- func (dc *DirectConnection) IsClosed() bool
- func (dc *DirectConnection) IsInTransaction() bool
- func (dc *DirectConnection) Ping() error
- func (dc *DirectConnection) ResetConnection() error
- func (dc *DirectConnection) Rollback() error
- func (dc *DirectConnection) SetAutoCommit(v uint8) error
- func (dc *DirectConnection) SetCharset(charset string, collation mysql.CollationID) (bool, error)
- func (dc *DirectConnection) SetSessionVariables(frontend *mysql.SessionVariables) (bool, error)
- func (dc *DirectConnection) UseDB(dbName string) error
- func (dc *DirectConnection) WriteSetStatement() error
- type PooledConnection
- func (pc *PooledConnection) Begin() error
- func (pc *PooledConnection) Close()
- func (pc *PooledConnection) Commit() error
- func (pc *PooledConnection) Execute(sql string) (*mysql.Result, error)
- func (pc *PooledConnection) FieldList(table string, wildcard string) ([]*mysql.Field, error)
- func (pc *PooledConnection) GetAddr() string
- func (pc *PooledConnection) IsClosed() bool
- func (pc *PooledConnection) Reconnect() error
- func (pc *PooledConnection) Recycle()
- func (pc *PooledConnection) Rollback() error
- func (pc *PooledConnection) SetAutoCommit(v uint8) error
- func (pc *PooledConnection) SetCharset(charset string, collation mysql.CollationID) (bool, error)
- func (pc *PooledConnection) SetSessionVariables(frontend *mysql.SessionVariables) (bool, error)
- func (pc *PooledConnection) UseDB(db string) error
- func (pc *PooledConnection) WriteSetStatement() error
- type Slice
- func (s *Slice) Close() error
- func (s *Slice) GetConn(fromSlave bool, userType int) (pc *PooledConnection, err error)
- func (s *Slice) GetMasterConn() (*PooledConnection, error)
- func (s *Slice) GetSlaveConn() (*PooledConnection, error)
- func (s *Slice) GetSliceName() string
- func (s *Slice) GetStatisticSlaveConn() (*PooledConnection, error)
- func (s *Slice) ParseMaster(masterStr string) error
- func (s *Slice) ParseSlave(slaves []string) error
- func (s *Slice) ParseStatisticSlave(statisticSlaves []string) error
- func (s *Slice) SetCharsetInfo(charset string, collationID mysql.CollationID)
Constants ¶
const (
// DefaultSlice means default slice for namespace
DefaultSlice = "slice-0"
)
Variables ¶
var ( // ErrConnectionPoolClosed means pool closed error ErrConnectionPoolClosed = errors.New("connection pool is closed") // DefaultCapacity default capacity of connection pool DefaultCapacity = 64 )
Functions ¶
This section is empty.
Types ¶
type ConnectionPool ¶
type ConnectionPool struct {
// contains filtered or unexported fields
}
ConnectionPool means connection pool with specific addr
func NewConnectionPool ¶
func NewConnectionPool(addr, user, password, db string, capacity, maxCapacity int, idleTimeout time.Duration, charset string, collationID mysql.CollationID) *ConnectionPool
NewConnectionPool create connection pool
func (*ConnectionPool) Active ¶
func (cp *ConnectionPool) Active() int64
Active returns the number of active connections in the pool
func (*ConnectionPool) Addr ¶
func (cp *ConnectionPool) Addr() string
Addr return addr of connection pool
func (*ConnectionPool) Available ¶
func (cp *ConnectionPool) Available() int64
Available returns the number of available connections in the pool
func (*ConnectionPool) Capacity ¶
func (cp *ConnectionPool) Capacity() int64
Capacity return the pool capacity
func (*ConnectionPool) Get ¶
func (cp *ConnectionPool) Get(ctx context.Context) (*PooledConnection, error)
Get return a connection, you should call PooledConnection's Recycle once done
func (*ConnectionPool) IdleClosed ¶
func (cp *ConnectionPool) IdleClosed() int64
IdleClosed return the number of closed connections for the pool
func (*ConnectionPool) IdleTimeout ¶
func (cp *ConnectionPool) IdleTimeout() time.Duration
IdleTimeout returns the idle timeout for the pool
func (*ConnectionPool) InUse ¶
func (cp *ConnectionPool) InUse() int64
InUse returns the number of in-use connections in the pool
func (*ConnectionPool) MaxCap ¶
func (cp *ConnectionPool) MaxCap() int64
MaxCap returns the maximum size of the pool
func (*ConnectionPool) Open ¶
func (cp *ConnectionPool) Open()
Open open connection pool without error, should be called before use the pool
func (*ConnectionPool) Put ¶
func (cp *ConnectionPool) Put(pc *PooledConnection)
Put recycle a connection into the pool
func (*ConnectionPool) SetCapacity ¶
func (cp *ConnectionPool) SetCapacity(capacity int) (err error)
SetCapacity alert the size of the pool at runtime
func (*ConnectionPool) SetIdleTimeout ¶
func (cp *ConnectionPool) SetIdleTimeout(idleTimeout time.Duration)
SetIdleTimeout set the idleTimeout of the pool
func (*ConnectionPool) StatsJSON ¶
func (cp *ConnectionPool) StatsJSON() string
StatsJSON return the pool stats as JSON object.
func (*ConnectionPool) WaitCount ¶
func (cp *ConnectionPool) WaitCount() int64
WaitCount returns how many clients are waitting for a connection
func (*ConnectionPool) WaitTime ¶
func (cp *ConnectionPool) WaitTime() time.Duration
WaitTime returns the time wait for a connection
type DirectConnection ¶
type DirectConnection struct {
// contains filtered or unexported fields
}
DirectConnection means connection to backend mysql
func NewDirectConnection ¶
func NewDirectConnection(addr string, user string, password string, db string, charset string, collationID mysql.CollationID) (*DirectConnection, error)
NewDirectConnection return direct and authorised connection to mysql with real net connection
func (*DirectConnection) Begin ¶
func (dc *DirectConnection) Begin() error
Begin send ComQuery with 'begin' to backend mysql to start transaction
func (*DirectConnection) Close ¶
func (dc *DirectConnection) Close()
Close close connection to backend mysql and reset conn structure
func (*DirectConnection) Commit ¶
func (dc *DirectConnection) Commit() error
Commit send ComQuery with 'commit' to backend mysql to commit transaction
func (*DirectConnection) Execute ¶
func (dc *DirectConnection) Execute(sql string) (*mysql.Result, error)
Execute send ComQuery or ComStmtPrepare/ComStmtExecute/ComStmtClose to backend mysql
func (*DirectConnection) GetAddr ¶
func (dc *DirectConnection) GetAddr() string
GetAddr return addr of backend mysql
func (*DirectConnection) GetCharset ¶
func (dc *DirectConnection) GetCharset() string
GetCharset return charset of specific connection
func (*DirectConnection) GetDB ¶
func (dc *DirectConnection) GetDB() string
GetDB return database name
func (*DirectConnection) IsAutoCommit ¶
func (dc *DirectConnection) IsAutoCommit() bool
IsAutoCommit check if autocommit
func (*DirectConnection) IsClosed ¶
func (dc *DirectConnection) IsClosed() bool
IsClosed check if connection closed
func (*DirectConnection) IsInTransaction ¶
func (dc *DirectConnection) IsInTransaction() bool
IsInTransaction check if in transaction
func (*DirectConnection) Ping ¶
func (dc *DirectConnection) Ping() error
Ping implements mysql ping command.
func (*DirectConnection) ResetConnection ¶
func (dc *DirectConnection) ResetConnection() error
ResetConnection reset connection stattus, include transaction、autocommit、charset、sql_mode .etc
func (*DirectConnection) Rollback ¶
func (dc *DirectConnection) Rollback() error
Rollback send ComQuery with 'rollback' to backend mysql to rollback transaction
func (*DirectConnection) SetAutoCommit ¶
func (dc *DirectConnection) SetAutoCommit(v uint8) error
SetAutoCommit trun on/off autocommit
func (*DirectConnection) SetCharset ¶
func (dc *DirectConnection) SetCharset(charset string, collation mysql.CollationID) (bool, error)
SetCharset set charset of connection to backend mysql
func (*DirectConnection) SetSessionVariables ¶
func (dc *DirectConnection) SetSessionVariables(frontend *mysql.SessionVariables) (bool, error)
SetSessionVariables set direction variables according to Session
func (*DirectConnection) UseDB ¶
func (dc *DirectConnection) UseDB(dbName string) error
UseDB send ComInitDB to backend mysql
func (*DirectConnection) WriteSetStatement ¶
func (dc *DirectConnection) WriteSetStatement() error
WriteSetStatement execute sql
type PooledConnection ¶
type PooledConnection struct {
// contains filtered or unexported fields
}
PooledConnection app use this object to exec sql
func (*PooledConnection) Begin ¶
func (pc *PooledConnection) Begin() error
Begin wrapper of direct connection, begin transaction
func (*PooledConnection) Close ¶
func (pc *PooledConnection) Close()
Close implement util.Resource interface
func (*PooledConnection) Commit ¶
func (pc *PooledConnection) Commit() error
Commit wrapper of direct connection, commit transaction
func (*PooledConnection) Execute ¶
func (pc *PooledConnection) Execute(sql string) (*mysql.Result, error)
Execute wrapper of direct connection, execute sql
func (*PooledConnection) FieldList ¶
FieldList wrapper of direct connection, send field list to mysql
func (*PooledConnection) GetAddr ¶
func (pc *PooledConnection) GetAddr() string
GetAddr wrapper of return addr of direct connection
func (*PooledConnection) IsClosed ¶
func (pc *PooledConnection) IsClosed() bool
IsClosed check if pooled connection closed
func (*PooledConnection) Reconnect ¶
func (pc *PooledConnection) Reconnect() error
Reconnect replaces the existing underlying connection with a new one. If we get "MySQL server has gone away (errno 2006)", then call Reconnect
func (*PooledConnection) Recycle ¶
func (pc *PooledConnection) Recycle()
Recycle return PooledConnection to the pool
func (*PooledConnection) Rollback ¶
func (pc *PooledConnection) Rollback() error
Rollback wrapper of direct connection, rollback transaction
func (*PooledConnection) SetAutoCommit ¶
func (pc *PooledConnection) SetAutoCommit(v uint8) error
SetAutoCommit wrapper of direct connection, set autocommit
func (*PooledConnection) SetCharset ¶
func (pc *PooledConnection) SetCharset(charset string, collation mysql.CollationID) (bool, error)
SetCharset wrapper of direct connection, set charset of connection
func (*PooledConnection) SetSessionVariables ¶
func (pc *PooledConnection) SetSessionVariables(frontend *mysql.SessionVariables) (bool, error)
SetSessionVariables set pc variables according to session
func (*PooledConnection) UseDB ¶
func (pc *PooledConnection) UseDB(db string) error
UseDB wrapper of direct connection, init database
func (*PooledConnection) WriteSetStatement ¶
func (pc *PooledConnection) WriteSetStatement() error
WriteSetStatement exec sql
type Slice ¶
type Slice struct { Cfg models.Slice sync.RWMutex Master *ConnectionPool Slave []*ConnectionPool LastSlaveIndex int RoundRobinQ []int SlaveWeights []int StatisticSlave []*ConnectionPool LastStatisticSlaveIndex int StatisticSlaveRoundRobinQ []int StatisticSlaveWeights []int // contains filtered or unexported fields }
Slice means one slice of the mysql cluster
func (*Slice) GetConn ¶
func (s *Slice) GetConn(fromSlave bool, userType int) (pc *PooledConnection, err error)
GetConn get backend connection from different node based on fromSlave and userType
func (*Slice) GetMasterConn ¶
func (s *Slice) GetMasterConn() (*PooledConnection, error)
GetMasterConn return a connection in master pool
func (*Slice) GetSlaveConn ¶
func (s *Slice) GetSlaveConn() (*PooledConnection, error)
GetSlaveConn return a connection in slave pool
func (*Slice) GetSliceName ¶
GetSliceName return name of slice
func (*Slice) GetStatisticSlaveConn ¶
func (s *Slice) GetStatisticSlaveConn() (*PooledConnection, error)
GetStatisticSlaveConn return a connection in statistic slave pool
func (*Slice) ParseMaster ¶
ParseMaster create master connection pool
func (*Slice) ParseSlave ¶
ParseSlave create connection pool of slaves (127.0.0.1:3306@2,192.168.0.12:3306@3)
func (*Slice) ParseStatisticSlave ¶
ParseStatisticSlave create connection pool of statistic slaves slaveStr(127.0.0.1:3306@2,192.168.0.12:3306@3)
func (*Slice) SetCharsetInfo ¶
func (s *Slice) SetCharsetInfo(charset string, collationID mysql.CollationID)
SetCharsetInfo set charset