change marshal strategy

This commit is contained in:
hebo 2019-10-08 15:16:42 +08:00
parent 2c54f18374
commit c3531819bd
5 changed files with 20 additions and 7 deletions

2
Godeps/Godeps.json generated
View File

@ -109,7 +109,7 @@
}, },
{ {
"ImportPath": "github.com/zr-hebo/util-db", "ImportPath": "github.com/zr-hebo/util-db",
"Rev": "3ff29f916f7b712b3adc53c4b9b19b13b8bbed87" "Rev": "06948bca5665b2d80078f9ff5c015eabb43c54f8"
}, },
{ {
"ImportPath": "github.com/zr-hebo/util-http", "ImportPath": "github.com/zr-hebo/util-http",

View File

@ -62,7 +62,7 @@ func (mqp *MysqlQueryPiece) Bytes() (content []byte) {
return mqp.jsonContent return mqp.jsonContent
} }
mqp.jsonContent = marsharQueryPiece(mqp) mqp.jsonContent = marsharQueryPieceMonopolize(mqp)
return mqp.jsonContent return mqp.jsonContent
} }

View File

@ -1,13 +1,15 @@
package model package model
import ( import (
// "github.com/json-iterator/go"
"bytes" "bytes"
"encoding/json" "encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/pingcap/tidb/util/hack" "github.com/pingcap/tidb/util/hack"
"time" "time"
) )
var jsonIterator = jsoniter.ConfigCompatibleWithStandardLibrary
type QueryPiece interface { type QueryPiece interface {
String() *string String() *string
Bytes() []byte Bytes() []byte
@ -70,7 +72,7 @@ func (bqp *BaseQueryPiece) Bytes() (content []byte) {
return bqp.jsonContent return bqp.jsonContent
} }
bqp.jsonContent = marsharQueryPiece(bqp) bqp.jsonContent = marsharQueryPieceMonopolize(bqp)
return bqp.jsonContent return bqp.jsonContent
} }
@ -81,7 +83,7 @@ func (bqp *BaseQueryPiece) GetSQL() (*string) {
func (bqp *BaseQueryPiece) Recovery() { func (bqp *BaseQueryPiece) Recovery() {
} }
func marsharQueryPiece(qp interface{}) []byte { func marsharQueryPieceShareMemory(qp interface{}) []byte {
var cacheBuffer = localSliceBufferPool.Dequeue() var cacheBuffer = localSliceBufferPool.Dequeue()
if len(cacheBuffer) > 0 { if len(cacheBuffer) > 0 {
panic("there already have bytes in buffer") panic("there already have bytes in buffer")
@ -95,3 +97,12 @@ func marsharQueryPiece(qp interface{}) []byte {
return buffer.Bytes() return buffer.Bytes()
} }
func marsharQueryPieceMonopolize(qp interface{}) (content []byte) {
content, err := jsonIterator.Marshal(qp)
if err != nil {
return []byte(err.Error())
}
return content
}

View File

@ -43,12 +43,14 @@ type MysqlDB struct {
DatabaseType string DatabaseType string
DBName string DBName string
ConnectTimeout int ConnectTimeout int
QueryTimeout int
} }
// NewMysqlDB 创建MySQL数据库 // NewMysqlDB 创建MySQL数据库
func NewMysqlDB() (md *MysqlDB) { func NewMysqlDB() (md *MysqlDB) {
md = new(MysqlDB) md = new(MysqlDB)
md.DatabaseType = dbTypeMysql md.DatabaseType = dbTypeMysql
md.QueryTimeout = 5
return return
} }
@ -352,7 +354,7 @@ func (md *MysqlDB) fillConnStr() string {
md.UserName, md.Passwd, md.IP, md.Port, md.DBName) md.UserName, md.Passwd, md.IP, md.Port, md.DBName)
if md.ConnectTimeout > 0 { if md.ConnectTimeout > 0 {
dbServerInfoStr = fmt.Sprintf("%s?timeout=%ds&readTimeout=%ds&writeTimeout=%ds", dbServerInfoStr = fmt.Sprintf("%s?timeout=%ds&readTimeout=%ds&writeTimeout=%ds",
dbServerInfoStr, md.ConnectTimeout, md.ConnectTimeout, md.ConnectTimeout) dbServerInfoStr, md.ConnectTimeout, md.QueryTimeout, md.QueryTimeout)
} }
return dbServerInfoStr return dbServerInfoStr

View File

@ -6,7 +6,6 @@ import (
"time" "time"
) )
// PooledMysqlDB Mysql主机实例 // PooledMysqlDB Mysql主机实例
type PooledMysqlDB struct { type PooledMysqlDB struct {
MysqlDB MysqlDB
@ -46,6 +45,7 @@ func NewPooledMysqlDBWithAllParam(
func NewPooledMysqlDB() (pmd *PooledMysqlDB) { func NewPooledMysqlDB() (pmd *PooledMysqlDB) {
pmd = new(PooledMysqlDB) pmd = new(PooledMysqlDB)
pmd.DatabaseType = dbTypeMysql pmd.DatabaseType = dbTypeMysql
pmd.QueryTimeout = 5
pmd.lock = new(sync.Mutex) pmd.lock = new(sync.Mutex)
return return
} }