golang http server - too many open files


用go写了日志收集的http服务,并发大约50-100.
问题:出现了连接缓慢累积不能正常释放,最后导致 net/http 包报 too many open files 的错误.
我是使用 netstat -ano | wc -l 命令查看连接数,停止go程序之后,连接数恢复正常,所以判断是这个程序有问题.
系统打开最大文件数已经调过了,现在是6W,这个问题没解决调很大应该也没用。
以下是几个相关的方法

   
  func (Controller *CController) serverInit() {
  
log.Info("/****** Service start ********/, Listen port ", config.Port)
s := &http.Server{
Addr: config.Port,
Handler: Controller.Log(http.DefaultServeMux),
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
}
http.HandleFunc("/this/is/example/", Controller.desHandler(phoneController.posUpdate))
err := s.ListenAndServe()
if err != nil {
log.Critical(err)
os.Exit(1)
}
}

func (Controller *CController) Log(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
access := fmt.Sprintf("%v,,%v,,%v,,%v", r.Method, r.RemoteAddr, r.URL, r.Header.Get("User-Agent"))
log.Trace(access)
handler.ServeHTTP(w, r)
})
}

func (Controller *CController) desHandler(fn func(http.ResponseWriter, *http.Request, map[string]string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res := new(Res)
q := r.URL.Query().Get("q")
param, err := Controller.getDesParam(q)
if err != nil {
log.Warn(err)
res.Msg = err.Error()
Controller.echoDesJson(res, w)
return
}

fn(w, r, param)
}
}

func (Phone *PhoneController) posUpdate(w http.ResponseWriter, r *http.Request, param map[string]string) {
res := new(Res)
var err error
err = valueInMap(param, "lat", "lng", "acc", "imei")
if err != nil {
log.Warn(err)
res.Msg = err.Error()
Controller.echoDesJson(res, w)
return
}

var posOptsStr []byte
t := strconv.FormatInt(time.Now().Unix(), 10)
posOpts := map[string]string{"acc": param["acc"], "time": t}
posOptsStr, err = json.Marshal(posOpts)
if err != nil {
log.Warn(err)
res.Msg = err.Error()
Controller.echoDesJson(res, w)
return
}

param["pos_opts"] = string(posOptsStr)

deviceModel.updatePos(param)
stsModel.location(param)

res.Code = 1
Controller.echoDesJson(res, w)
}

刚学go,实在是找不出问题在哪了。
感谢!

go

Colin's 11 years ago

可以把
netstat -ano | wc -l 之后输出的内容贴上来?
哪些项在上涨?

kuizl answered 11 years ago

Your Answer