This commit is contained in:
boyce
2019-07-30 19:44:07 +08:00
3 changed files with 96 additions and 35 deletions

View File

@@ -347,9 +347,9 @@ func (client *Client) Call(serviceMethod string, args interface{}, reply interfa
select { select {
case call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1), false).Done: case call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1), false).Done:
return call.Error return call.Error
case <-time.After(15 * time.Second): case <-time.After(30 * time.Second):
} }
//call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done //call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done
return errors.New(fmt.Sprintf("Call RPC %s is time out 10s", serviceMethod)) return errors.New(fmt.Sprintf("Call RPC %s is time out 30s", serviceMethod))
} }

View File

@@ -241,12 +241,10 @@ func (slf *DBResult) mapSingle2interface(m map[string]string, v reflect.Value) e
return nil return nil
} }
func (slf *DBModule) SetQuerySlowTime(Time time.Duration) { func (slf *DBModule) SetQuerySlowTime(Time time.Duration) {
slf.PrintTime = Time slf.PrintTime = Time
} }
func (slf *DBModule) IsPrintTimeLog(Time time.Duration) bool { func (slf *DBModule) IsPrintTimeLog(Time time.Duration) bool {
if slf.PrintTime != 0 && Time >= slf.PrintTime { if slf.PrintTime != 0 && Time >= slf.PrintTime {
return true return true
@@ -337,8 +335,58 @@ func (slf *SyncQueryDBResultEx) Get(timeoutMs int) (*DataSetList, error) {
return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs) return nil, fmt.Errorf("Getting the return result timeout [%d]ms", timeoutMs)
} }
func (slf *DBModule) CheckArgs(args ...interface{}) error {
for _, val := range args {
if reflect.TypeOf(val).Kind() == reflect.String {
retVal := val.(string)
if strings.Contains(retVal, "-") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(retVal, "#") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(retVal, "&") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(retVal, "=") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(retVal, "%") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(retVal, "'") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(strings.ToLower(retVal), "delete ") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(strings.ToLower(retVal), "truncate ") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(strings.ToLower(retVal), " or ") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(strings.ToLower(retVal), "from ") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
if strings.Contains(strings.ToLower(retVal), "set ") == true {
return fmt.Errorf("CheckArgs is error arg is %+v", retVal)
}
}
}
return nil
}
// Query ... // Query ...
func (slf *DBModule) Query(query string, args ...interface{}) DBResult { func (slf *DBModule) Query(query string, args ...interface{}) DBResult {
if slf.CheckArgs(args) != nil {
ret := DBResult{}
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
ret.Err = fmt.Errorf("CheckArgs is error!")
return ret
}
if slf.db == nil { if slf.db == nil {
ret := DBResult{} ret := DBResult{}
service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query) service.GetLogger().Printf(service.LEVER_ERROR, "cannot connect database:%s", query)
@@ -363,6 +411,11 @@ func (slf *DBModule) QueryEx(query string, args ...interface{}) (*DataSetList, e
datasetList.tag = "json" datasetList.tag = "json"
datasetList.blur = true datasetList.blur = true
if slf.CheckArgs(args) != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
return &datasetList, fmt.Errorf("CheckArgs is error!")
}
if slf.db == nil { if slf.db == nil {
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!")
@@ -454,6 +507,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!")
} }
if slf.CheckArgs(args) != nil {
service.GetLogger().Printf(service.LEVER_ERROR, "CheckArgs is error :%s", query)
//return ret, fmt.Errorf("cannot connect database!")
return ret, fmt.Errorf("CheckArgs is error!")
}
TimeFuncStart := time.Now() TimeFuncStart := time.Now()
res, err := slf.db.Exec(query, args...) res, err := slf.db.Exec(query, args...)
TimeFuncPass := time.Since(TimeFuncStart) TimeFuncPass := time.Since(TimeFuncStart)
@@ -514,7 +573,7 @@ func (slf *DBModule) RunExecuteDBCoroutine() {
func (slf *DataSetList) UnMarshal(args ...interface{}) error { func (slf *DataSetList) UnMarshal(args ...interface{}) error {
if len(slf.dataSetList) != len(args) { if len(slf.dataSetList) != len(args) {
return errors.New("Data set len(%d) is not equal to args!") return errors.New(fmt.Sprintf("Data set len(%d,%d) is not equal to args!", len(slf.dataSetList), len(args)))
} }
for _, out := range args { for _, out := range args {

View File

@@ -130,6 +130,23 @@ func (slf *HttpServerService) staticServer(w http.ResponseWriter, r *http.Reques
w.Write([]byte(msg)) w.Write([]byte(msg))
} }
nowpath, _ := os.Getwd()
upath := r.URL.Path
destLocalPath := nowpath + upath
switch r.Method {
//获取资源
case "GET":
//判断文件夹是否存在
_, err := os.Stat(destLocalPath)
if err == nil {
http.ServeFile(w, r, destLocalPath)
} else {
writeResp(http.StatusNotFound, "")
return
}
//上传资源
case "POST":
// 在这儿处理例外路由接口 // 在这儿处理例外路由接口
var errRet error var errRet error
for _, filter := range slf.httpfiltrateList { for _, filter := range slf.httpfiltrateList {
@@ -147,22 +164,6 @@ func (slf *HttpServerService) staticServer(w http.ResponseWriter, r *http.Reques
return return
} }
nowpath, _ := os.Getwd()
upath := r.URL.Path
destLocalPath := nowpath + upath
switch r.Method {
//获取资源
case "GET":
//判断文件夹是否存在
_, err := os.Stat(destLocalPath)
if err == nil {
http.ServeFile(w, r, destLocalPath)
} else {
writeResp(http.StatusNotFound, "")
return
}
//上传资源
case "POST":
r.ParseMultipartForm(32 << 20) // max memory is set to 32MB r.ParseMultipartForm(32 << 20) // max memory is set to 32MB
resourceFile, resourceFileHeader, err := r.FormFile("file") resourceFile, resourceFileHeader, err := r.FormFile("file")
if err != nil { if err != nil {
@@ -190,6 +191,7 @@ func (slf *HttpServerService) staticServer(w http.ResponseWriter, r *http.Reques
defer localfd.Close() defer localfd.Close()
io.Copy(localfd, resourceFile) io.Copy(localfd, resourceFile)
writeResp(http.StatusOK, upath+fileName) writeResp(http.StatusOK, upath+fileName)
} }