change throw_package_rate to capture_package_rate

This commit is contained in:
hebo 2019-09-10 17:58:34 +08:00
parent b13f9eec6c
commit 03c0a3effd
6 changed files with 33 additions and 28 deletions

View File

@ -116,16 +116,16 @@ func (nc *networkCard) listenNormal() {
continue
}
// throw packets according to a certain probability
throwPacketRate := communicator.GetTCPThrowPacketRate()
if throwPacketRate >= 1.0 {
// capture packets according to a certain probability
capturePacketRate := communicator.GetTCPCapturePacketRate()
if capturePacketRate <= 0 {
time.Sleep(time.Second*3)
continue
} else if 0 < throwPacketRate && throwPacketRate < 1.0 {
} else if 0 < capturePacketRate && capturePacketRate < 1.0 {
// fall into throw range
rn := rand.Float64()
if rn <= throwPacketRate {
if rn > capturePacketRate {
continue
}
}

View File

@ -8,23 +8,28 @@ import (
)
const (
THROW_PACKET_RATE = "throw_packet_rate"
CAPTURE_PACKET_RATE = "capture_packet_rate"
)
var (
communicatePort int
// capturePacketRate float64
router = mux.NewRouter()
)
var (
configMapLock sync.RWMutex
configMap map[string]configItem
throwPacketRate *throwPacketRateConfig
configMapLock sync.RWMutex
configMap map[string]configItem
catpurePacketRate *capturePacketRateConfig
)
func init() {
catpurePacketRate = newCapturePacketRateConfig()
flag.IntVar(&communicatePort, "communicate_port", 8088, "http server port. Default is 8088")
var cpr float64
flag.Float64Var(&cpr, CAPTURE_PACKET_RATE, 1, "capture packet rate. Default is 1.0")
_ = catpurePacketRate.setVal(cpr)
configMap = make(map[string]configItem)
throwPacketRate = newThrowPacketRateConfig()
}

View File

@ -82,10 +82,10 @@ func outletSetConfig(resp http.ResponseWriter, req *http.Request) {
mp.Err = SetConfig(ep.ConfigName, ep.Value)
}
func GetTCPThrowPacketRate() float64 {
return throwPacketRate.tcpTPR
func GetTCPCapturePacketRate() float64 {
return catpurePacketRate.tcpTPR
}
func GetMysqlThrowPacketRate() float64 {
return throwPacketRate.mysqlTPR
func GetMysqlCapturePacketRate() float64 {
return catpurePacketRate.mysqlTPR
}

View File

@ -4,8 +4,8 @@ import "fmt"
// SetConfig set config by config key(name) and value
func SetConfig(key string, val interface{}) (err error) {
if key == throwPacketRate.name {
err = throwPacketRate.setVal(val)
if key == catpurePacketRate.name {
err = catpurePacketRate.setVal(val)
return
}

View File

@ -10,33 +10,33 @@ type configItem interface {
getVal () interface{}
}
type throwPacketRateConfig struct {
type capturePacketRateConfig struct {
name string
tcpTPR float64
mysqlTPR float64
}
func newThrowPacketRateConfig() (tpr *throwPacketRateConfig) {
tpr = &throwPacketRateConfig{
name: THROW_PACKET_RATE,
tcpTPR: 0.0,
mysqlTPR: 0.0,
func newCapturePacketRateConfig() (cprc *capturePacketRateConfig) {
cprc = &capturePacketRateConfig{
name: CAPTURE_PACKET_RATE,
tcpTPR: 1.0,
mysqlTPR: 1.0,
}
return
}
func (tc *throwPacketRateConfig) setVal (val interface{}) (err error){
func (cprc *capturePacketRateConfig) setVal (val interface{}) (err error){
realVal, ok := val.(float64)
if !ok {
err = fmt.Errorf("cannot reansform val: %v to float64", val)
return
}
tc.mysqlTPR = realVal
tc.tcpTPR = math.Sqrt(realVal)
cprc.mysqlTPR = realVal
cprc.tcpTPR = math.Sqrt(realVal)
return
}
func (tc *throwPacketRateConfig) getVal () (val interface{}){
return tc.mysqlTPR
func (cprc *capturePacketRateConfig) getVal () (val interface{}){
return cprc.mysqlTPR
}

View File

@ -313,5 +313,5 @@ func filterQueryPieceBySQL(mqp *model.PooledMysqlQueryPiece, querySQL []byte) (m
func (ms *MysqlSession) composeQueryPiece() (mqp *model.PooledMysqlQueryPiece) {
return model.NewPooledMysqlQueryPiece(
ms.connectionID, ms.clientHost, ms.visitUser, ms.visitDB, ms.clientHost, ms.serverIP,
ms.clientPort, ms.serverPort, communicator.GetMysqlThrowPacketRate(), ms.stmtBeginTime)
ms.clientPort, ms.serverPort, communicator.GetMysqlCapturePacketRate(), ms.stmtBeginTime)
}