我正在使用go-json-rest编写 REST 服务,它反过来使用 net/http。我的服务器代码很简单,获取请求并将其传递给通道这是我的服务器代码package mainimport ( "github.com/ant0ine/go-json-rest/rest" "log" "net/http" "strconv" "time")const workerCount = 4var evChannel = make(chan Event)var workers = make([]*LogWorker, workerCount)const maxLogFileSize = 100 // In MBconst maxLogFileBackups = 30const maxLogFileAge = 5const logFileName = "/home/sam/tmp/go_logs/event_"func main() { // Initialize workers // Four workers is being created for i := 0; i < workerCount; i++ { var fileName = logFileName + strconv.Itoa(i) workers[i] = NewLogWorker(fileName, maxLogFileSize, maxLogFileBackups, maxLogFileAge) go workers[i].Work(evChannel) } // Initialize REST API api := rest.NewApi() //api.Use(rest.DefaultDevStack...) api.Use(rest.DefaultCommonStack...) router, err := rest.MakeRouter( rest.Post("/events", StoreEvents), ) if err != nil { log.Fatal(err) } api.SetApp(router) log.Fatal(http.ListenAndServe(":4545", api.MakeHandler()))}func StoreEvents(w rest.ResponseWriter, r *rest.Request) { event := Event{} err := r.DecodeJsonPayload(&event) if err != nil { rest.Error(w, err.Error(), http.StatusInternalServerError) return } // TODO : Add validation if needed // Add code to parse the request and add further information to event // log.Println() select { case evChannel <- event: case <- time.After(5 * time.Second): // throw away the message, so sad } // evChannel <- event //log.Println(Csv(event)) w.WriteHeader(http.StatusOK)}当我使用 jmeter 连续执行它时,我偶尔会收到以下错误http: 接受错误:接受 tcp [::]:4545: 打开的文件太多;10ms 后重试net/http 是否为每个请求打开文件?
- 1 回答
- 0 关注
- 234 浏览
添加回答
举报
0/150
提交
取消