两项优化

1. 优化json类
2. 优化数据库默认连接配置
This commit is contained in:
liangzy 2020-08-11 14:31:27 +08:00
parent 42bbd3f0f5
commit eba7a63f4b
5 changed files with 94 additions and 14 deletions

View File

@ -20,17 +20,24 @@ type dsnConfig struct {
}
type DbItem struct {
User string `yaml:"user"`
Pass string `yaml:"pass"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Name string `yaml:"name"`
Driver string `yaml:"driver"`
Prefix string `yaml:"prefix"`
User string `yaml:"user"`
Pass string `yaml:"pass"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Name string `yaml:"name"`
Driver string `yaml:"driver"`
Prefix string `yaml:"prefix"`
MaxIdleConns int `yaml:"max_idle_conns"`
MaxOpenConns int `yaml:"max_open_conns"`
ConnMaxLifetime int64 `yaml:"conn_max_lifetime"`
Timeout int `yaml:"timeout"`
}
func (d *DbItem) Dsn() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=Local&autocommit=true&timeout=5s", d.User, d.Pass, d.Host, d.Port, d.Name)
if d.Timeout <= 0 {
d.Timeout = 5
}
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&loc=Local&autocommit=true&timeout=%ds", d.User, d.Pass, d.Host, d.Port, d.Name, d.Timeout)
}
func init() {

View File

@ -45,8 +45,23 @@ func LoadXorm(dsns map[string]dsnConfig) (err error) {
tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, cast.ToString(dsn.Item.Prefix))
engine.SetTableMapper(tbMapper)
engine.SetLogger(logger)
engine.SetMaxIdleConns(1000)
engine.SetMaxOpenConns(1000)
if dsn.Item.MaxIdleConns <= 0 {
engine.SetMaxIdleConns(200)
} else {
engine.SetMaxIdleConns(dsn.Item.MaxIdleConns)
}
if dsn.Item.MaxOpenConns <= 0 {
engine.SetMaxOpenConns(200)
} else {
engine.SetMaxOpenConns(dsn.Item.MaxOpenConns)
}
if dsn.Item.ConnMaxLifetime > 0 {
engine.SetConnMaxLifetime(time.Second * time.Duration(dsn.Item.ConnMaxLifetime))
}
engine.TZLocation, _ = time.LoadLocation("Asia/Shanghai")
if runmode == "dev" {
engine.ShowSQL(true)

View File

@ -3,7 +3,6 @@ package response
import (
"bytes"
"encoding/json"
"golib.gaore.com/GaoreGo/grlogs"
)
const (
@ -19,9 +18,14 @@ type Json struct {
}
func (j *Json) String() string {
b, err := json.Marshal(j)
grlogs.Debug(err)
return bytes.NewBuffer(b).String()
s, _ := j.ToString()
return s
}
func (j *Json) ToString() (str string, err error) {
var b []byte
b, err = json.Marshal(j)
return bytes.NewBuffer(b).String(), err
}
func NewJsonByDefaultSuccess(data ...interface{}) *Json {

View File

@ -1,8 +1,48 @@
package response
import (
"bytes"
"encoding/xml"
"log"
)
type Xml struct {
Code int `xml:"code"`
Data interface{} `xml:"data"`
Msg string `xml:"msg"`
Status bool `xml:"status"`
}
func (x *Xml) String() string {
b, err := xml.MarshalIndent(x, "", " ")
if err != nil {
log.Println("xml encode", err)
}
return xml.Header + bytes.NewBuffer(b).String()
}
func NewXmlByDefaultSuccess(data ...interface{}) Xml {
return NewXml(true, 0, JsonMsgDefaultOk, data...)
}
func NewXmlByDefaultFailed(data ...interface{}) Xml {
return NewXml(false, 1, JsonMsgDefaultFailed, data...)
}
func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
if len(data) > 0 {
return Xml{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return Xml{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}

14
response/xml_test.go Normal file
View File

@ -0,0 +1,14 @@
package response
import (
"fmt"
"testing"
)
type XmlResponse struct {
Xml
}
func TestNewXmlByDefaultSuccess(t *testing.T) {
fmt.Println(&XmlResponse{Xml: NewXmlByDefaultSuccess()})
}