From 6b3cf5bf31e628a2b490e1494686101b91a53a7d Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Fri, 12 Jan 2024 14:01:51 +0800 Subject: [PATCH] fix: mml enhancement --- config/etc/default/restconf.yaml | 5 +++-- features/mml/mml.go | 37 +++++++++++++++++++++++++------- restagent/config/config.go | 14 ++++++------ restagent/etc/restconf.yaml | 5 +++-- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/config/etc/default/restconf.yaml b/config/etc/default/restconf.yaml index 8f7a61c0..3f66343d 100644 --- a/config/etc/default/restconf.yaml +++ b/config/etc/default/restconf.yaml @@ -65,15 +65,16 @@ redis: # used to specify the default data source for multiple data resourece defaultDataSourceName: "default" -# upfHeaderLength: spare upf buffer, must destroy +# sleep: time delay for after write buffer (millisecond) +# deadLine: timeout for io read and write (second) mml: port: 4100 sleep: 200 + deadLine: 10 user: admin password: admin mmlHome: ./mmlhome upload: /home/agtuser - upfHeaderLength: 2335 # NE config ne: diff --git a/features/mml/mml.go b/features/mml/mml.go index f013df75..be2da12d 100644 --- a/features/mml/mml.go +++ b/features/mml/mml.go @@ -21,10 +21,10 @@ import ( "github.com/gorilla/mux" ) -const ( - //经过测试,linux下,延时需要大于100ms - TIME_DELAY_AFTER_WRITE = 200 -) +// const ( +// //经过测试,linux下,延时需要大于100ms +// TIME_DELAY_AFTER_WRITE = 200 +// ) type Response struct { Data []string `json:"data"` @@ -48,6 +48,15 @@ var ( CustomUriOmMmlInt = config.UriPrefix + "/omManagement/{apiVersion}/mml/{neType}/{neId}" ) +// func init() { +// if config.GetYamlConfig().MML.Sleep != 0 { +// TIME_DELAY_AFTER_WRITE = time.Duration(config.GetYamlConfig().MML.Sleep) +// } +// if config.GetYamlConfig().MML.DeadLine != 0 { +// TIME_DEAD_LINE = time.Duration(config.GetYamlConfig().MML.DeadLine) +// } +// } + func PostMMLToNF(w http.ResponseWriter, r *http.Request) { log.Debug("PostMMLToNF processing... ") @@ -56,6 +65,16 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { log.Error("Request error:", err) return } + // 经过测试,linux下,延时需要大于100ms + var TIME_DELAY_AFTER_WRITE time.Duration = 200 + var TIME_DEAD_LINE time.Duration = 10 + if config.GetYamlConfig().MML.Sleep != 0 { + TIME_DELAY_AFTER_WRITE = time.Duration(config.GetYamlConfig().MML.Sleep) + } + if config.GetYamlConfig().MML.DeadLine != 0 { + TIME_DEAD_LINE = time.Duration(config.GetYamlConfig().MML.DeadLine) + } + pack := "mml" vars := mux.Vars(r) module := vars["managedType"] @@ -73,6 +92,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { token, r.Method, module, neType, neId[0], pack) var buf [20 * 1024]byte + //buf := make([]byte, 0) var n int var mmlResult []string @@ -215,7 +235,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { } defer conn.Close() - conn.SetDeadline(time.Now().Add(10 * time.Second)) + conn.SetDeadline(time.Now().Add(TIME_DEAD_LINE * time.Second)) loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password) _, err = conn.Write([]byte(loginStr)) @@ -254,7 +274,8 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { re2 := regexp.MustCompile(`[\x00-\x08\x0B\x0C\x0E-\x1F\x7F\x1B]`) // 匹配空字符和包含␛的控制字符 //re := regexp.MustCompile(`[\x00-\x1F\x7F]`) // upf telnet buffer只能读取一次,需要去掉前面的多余字符 - result := re1.ReplaceAllString(string(buf[config.GetYamlConfig().MML.UpfHeaderLength:n-len(neType)-2]), "") + //result := re1.ReplaceAllString(string(buf[config.GetYamlConfig().MML.UpfHeaderLength:n-len(neType)-2]), "") + result := re1.ReplaceAllString(string(buf[0:n-len(neType)-2]), "") result = re2.ReplaceAllString(result, "") mmlResult = append(mmlResult, result) conn.Close() @@ -286,7 +307,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { // services.ResponseWithJson(w, http.StatusOK, response) // return // } - conn.SetDeadline(time.Now().Add(10 * time.Second)) + conn.SetDeadline(time.Now().Add(TIME_DEAD_LINE * time.Second)) _, err = conn.Write([]byte(config.GetYamlConfig().MML.User + "\r\n")) if err != nil { @@ -405,7 +426,7 @@ func PostMMLToNF(w http.ResponseWriter, r *http.Request) { // return // } - conn.SetDeadline(time.Now().Add(10 * time.Second)) + conn.SetDeadline(time.Now().Add(TIME_DEAD_LINE * time.Second)) loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password) _, err = conn.Write([]byte(loginStr)) if err != nil { diff --git a/restagent/config/config.go b/restagent/config/config.go index 81fd15f5..0c985634 100644 --- a/restagent/config/config.go +++ b/restagent/config/config.go @@ -103,13 +103,13 @@ type YamlConfig struct { } `yaml:"alarm"` MML struct { - Port int `yaml:"port"` - Sleep int64 `yaml:"sleep"` - User string `yaml:"user"` - Password string `ymal:"password"` - MmlHome string `yaml:"mmlHome"` - Upload string `yaml:"upload"` - UpfHeaderLength int `yaml:"upfHeaderLength"` + Port int `yaml:"port"` + Sleep int64 `yaml:"sleep"` + DeadLine int64 `yaml:"deadLine"` + User string `yaml:"user"` + Password string `ymal:"password"` + MmlHome string `yaml:"mmlHome"` + Upload string `yaml:"upload"` } `yaml:"mml"` NE struct { diff --git a/restagent/etc/restconf.yaml b/restagent/etc/restconf.yaml index 8f451f3d..cebae0f3 100644 --- a/restagent/etc/restconf.yaml +++ b/restagent/etc/restconf.yaml @@ -65,15 +65,16 @@ redis: # 多个数据源时可以用这个指定默认的数据源 defaultDataSourceName: "default" -# upfHeaderLength: spare upf buffer, must destroy +# sleep: time delay for after write buffer (millisecond) +# deadLine: timeout for io read and write (second) mml: port: 4100 sleep: 200 + deadLine: 10 user: admin password: admin mmlHome: ./mmlhome upload: /home/agtuser - upfHeaderLength: 2335 # NE config ne: