服务器报打开的文件过多,mgo go服务器中打开文件过多

我在日志中收到这些错误:

接受错误:接受tcp [::]:80:接受4:打开的文件过多;

对于ubuntu上的mongodb服务器,使用mgo编写在go中。运行约一天后,它们开始出现。

码:

package main

import (

"encoding/json"

"io"

"net/http"

"gopkg.in/mgo.v2/bson"

)

var (

Database *mgo.Database

)

func hello(w http.ResponseWriter, r *http.Request) {

io.WriteString(w, "hello")

}

func setTile(w http.ResponseWriter, r *http.Request) {

var requestJSON map[string]interface{}

err := json.NewDecoder(r.Body).Decode(&requestJSON)

if err != nil {

http.Error(w, err.Error(), 400)

return

}

collection := Database.C("tiles")

if requestJSON["tileId"] != nil {

query := bson.M{"tileId": requestJSON["tileId"]}

collection.RemoveAll(query)

collection.Insert(requestJSON)

w.WriteHeader(200)

w.Header().Set("Content-Type", "application/json")

js, _ := json.Marshal(map[string]string{"result": "ok"})

w.Write(js)

} else {

w.WriteHeader(200)

w.Header().Set("Content-Type", "application/json")

w.Write(js)

}

}

func getTile(w http.ResponseWriter, r *http.Request) {

var requestJSON map[string]interface{}

err := json.NewDecoder(r.Body).Decode(&requestJSON)

if err != nil {

http.Error(w, err.Error(), 400)

return

}

collection := Database.C("tiles")

var result []map[string]interface{}

if requestJSON["tileId"] != nil {

query := bson.M{"tileId": requestJSON["tileId"]}

collection.Find(query).All(&result)

}

if len(result) > 0 {

w.WriteHeader(200)

w.Header().Set("Content-Type", "application/json")

js, _ := json.Marshal(result[0])

w.Write(js)

} else {

w.WriteHeader(200)

w.Header().Set("Content-Type", "application/json")

js, _ := json.Marshal(map[string]string{"result": "tile id not found"})

w.Write(js)

}

}

func main() {

session, _ := mgo.Dial("localhost")

Database = session.DB("mapdb")

mux := http.NewServeMux()

mux.HandleFunc("/", hello)

mux.HandleFunc("/setTile", setTile)

mux.HandleFunc("/getTile", getTile)

http.ListenAndServe(":80", mux)

}

那里有需要关闭的东西吗?还是某种程度上的结构错误?

似乎有很多地方可以设置打开文件的限制,所以我不确定如何找出限制。但是增加限制似乎并不是问题所在,肯定是在每个请求上都打开了一些东西而没有关闭它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值