mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-06 14:17:31 +08:00
增加DB和HTTP调用时间
This commit is contained in:
@@ -29,7 +29,7 @@ type DBModule struct {
|
|||||||
password string
|
password string
|
||||||
dbname string
|
dbname string
|
||||||
maxconn int
|
maxconn int
|
||||||
|
PrintTime time.Duration
|
||||||
syncExecuteFun chan SyncFun
|
syncExecuteFun chan SyncFun
|
||||||
syncCoroutineNum int
|
syncCoroutineNum int
|
||||||
}
|
}
|
||||||
@@ -241,6 +241,19 @@ func (slf *DBResult) mapSingle2interface(m map[string]string, v reflect.Value) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (slf *DBModule) SetQuerySlowTime(Time time.Duration){
|
||||||
|
slf.PrintTime = Time
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (slf *DBModule) IsPrintTimeLog(Time time.Duration)bool{
|
||||||
|
if slf.PrintTime != 0 && Time >= slf.PrintTime{
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (slf *DBResult) mapSlice2interface(data []map[string]string, in interface{}) error {
|
func (slf *DBResult) mapSlice2interface(data []map[string]string, in interface{}) error {
|
||||||
length := len(data)
|
length := len(data)
|
||||||
|
|
||||||
@@ -354,7 +367,14 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
|
|||||||
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
|
||||||
return &datasetList, fmt.Errorf("cannot connect database!")
|
return &datasetList, fmt.Errorf("cannot connect database!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TimeFuncStart := time.Now()
|
||||||
rows, err := slf.db.Query(query, args...)
|
rows, err := slf.db.Query(query, args...)
|
||||||
|
TimeFuncPass := time.Since(TimeFuncStart)
|
||||||
|
|
||||||
|
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||||
|
service.GetLogger().Printf(service.LEVER_INFO, "DBModule QueryEx Time %s , Query :%s , args :%+v",TimeFuncPass,query,args)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%v)", query, err)
|
service.GetLogger().Printf(service.LEVER_ERROR, "Query:%s(%v)", query, err)
|
||||||
if rows != nil {
|
if rows != nil {
|
||||||
@@ -434,7 +454,12 @@ func (slf *DBModule) Exec(query string, args ...interface{}) (*DBResultEx, error
|
|||||||
return ret, fmt.Errorf("cannot connect database!")
|
return ret, fmt.Errorf("cannot connect database!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TimeFuncStart := time.Now()
|
||||||
res, err := slf.db.Exec(query, args...)
|
res, err := slf.db.Exec(query, args...)
|
||||||
|
TimeFuncPass := time.Since(TimeFuncStart)
|
||||||
|
if slf.IsPrintTimeLog(TimeFuncPass) {
|
||||||
|
service.GetLogger().Printf(service.LEVER_INFO, "DBModule QueryEx Time %s , Query :%s , args :%+v",TimeFuncPass,query,args)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err)
|
service.GetLogger().Printf(service.LEVER_ERROR, "Exec:%s(%v)", query, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -33,9 +33,9 @@ type ControllerMapsType map[string]reflect.Value
|
|||||||
|
|
||||||
type HttpServerService struct {
|
type HttpServerService struct {
|
||||||
service.BaseService
|
service.BaseService
|
||||||
httpserver network.HttpServer
|
httpserver network.HttpServer
|
||||||
port uint16
|
port uint16
|
||||||
|
PrintRequestTime bool
|
||||||
controllerMaps ControllerMapsType
|
controllerMaps ControllerMapsType
|
||||||
certfile string
|
certfile string
|
||||||
keyfile string
|
keyfile string
|
||||||
@@ -48,7 +48,6 @@ func (slf *HttpServerService) OnInit() error {
|
|||||||
if slf.ishttps == true {
|
if slf.ishttps == true {
|
||||||
slf.httpserver.SetHttps(slf.certfile, slf.keyfile)
|
slf.httpserver.SetHttps(slf.certfile, slf.keyfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +88,6 @@ func (slf *HttpServerService) OnDestory() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) OnSetupService(iservice service.IService) {
|
func (slf *HttpServerService) OnSetupService(iservice service.IService) {
|
||||||
//
|
|
||||||
rpc.RegisterName(iservice.GetServiceName(), "HTTP_", iservice)
|
rpc.RegisterName(iservice.GetServiceName(), "HTTP_", iservice)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +95,19 @@ func (slf *HttpServerService) OnRemoveService(iservice service.IService) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (slf *HttpServerService) IsPrintRequestTime() bool {
|
||||||
|
if slf.PrintRequestTime == true {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (slf *HttpServerService) SetPrintRequestTime(isPrint bool) {
|
||||||
|
slf.PrintRequestTime = isPrint
|
||||||
|
}
|
||||||
|
|
||||||
func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request) {
|
func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
writeError := func(status int, msg string) {
|
writeError := func(status int, msg string) {
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
@@ -142,7 +153,14 @@ func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request
|
|||||||
request := HttpRequest{r.Header, string(msg)}
|
request := HttpRequest{r.Header, string(msg)}
|
||||||
var resp HttpRespone
|
var resp HttpRespone
|
||||||
|
|
||||||
|
TimeFuncStart := time.Now()
|
||||||
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
|
||||||
|
|
||||||
|
TimeFuncPass := time.Since(TimeFuncStart)
|
||||||
|
if slf.IsPrintRequestTime() {
|
||||||
|
service.GetLogger().Printf(service.LEVER_INFO, "HttpServer Time : %s IP : %S url : %s", TimeFuncPass, strCallPath)
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json;charset=utf-8")
|
w.Header().Set("Content-Type", "application/json;charset=utf-8")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Respone = []byte(fmt.Sprint(err))
|
resp.Respone = []byte(fmt.Sprint(err))
|
||||||
|
|||||||
Reference in New Issue
Block a user