使用net/http时出现了“打开文件过多”的错误。

huangapple go评论99阅读模式
英文:

too many open files, when using net/http

问题

我正在使用go-json-rest编写REST服务,它使用net/http。

我的服务器代码很简单,只是获取请求并将其传递给一个通道。

以下是我的服务器代码:

package main

import (
    "github.com/ant0ine/go-json-rest/rest"
    "log"
    "net/http"
    "strconv"
    "time"
)

const workerCount = 4
var evChannel = make(chan Event)
var workers = make([]*LogWorker, workerCount)
const maxLogFileSize = 100 // In MB
const maxLogFileBackups = 30
const maxLogFileAge = 5
const logFileName = "/home/sam/tmp/go_logs/event_"

func main() {
    // 初始化工作线程
    // 创建了四个工作线程
    for i := 0; i < workerCount; i++ {
        var fileName = logFileName + strconv.Itoa(i)
        workers[i] = NewLogWorker(fileName, maxLogFileSize, maxLogFileBackups, maxLogFileAge)
        go workers[i].Work(evChannel)
    }    

    // 初始化 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 : 如果需要的话添加验证
    // 添加代码来解析请求并将进一步的信息添加到事件中
    // log.Println()
    select {
        case evChannel <- event:
        case <- time.After(5 * time.Second):
          // 丢弃消息,太遗憾了
    }    
    // evChannel <- event
    //log.Println(Csv(event))
    w.WriteHeader(http.StatusOK)
}

当我使用jmeter连续执行它时,偶尔会出现以下错误:

http: Accept error: accept tcp [::]:4545: too many open files; retrying in 10ms

net/http是否为每个请求打开文件?

英文:

I am writing REST service using go-json-rest, which inturn using net/http.

My server code is simply, get the request and pass it to a channel

Here is my server code

package main
import (
&quot;github.com/ant0ine/go-json-rest/rest&quot;
&quot;log&quot;
&quot;net/http&quot;
&quot;strconv&quot;
&quot;time&quot;
)
const workerCount = 4
var evChannel = make(chan Event)
var workers = make([]*LogWorker, workerCount)
const maxLogFileSize = 100 // In MB
const maxLogFileBackups = 30
const maxLogFileAge = 5
const logFileName = &quot;/home/sam/tmp/go_logs/event_&quot;
func main() {
// Initialize workers
// Four workers is being created
for i := 0; i &lt; 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(&quot;/events&quot;, StoreEvents),
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(&quot;:4545&quot;, api.MakeHandler()))
}
func StoreEvents(w rest.ResponseWriter, r *rest.Request) {
event := Event{}
err := r.DecodeJsonPayload(&amp;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 &lt;- event:
case &lt;- time.After(5 * time.Second):
// throw away the message, so sad
}    
// evChannel &lt;- event
//log.Println(Csv(event))
w.WriteHeader(http.StatusOK)
}

When I execute it continuously using jmeter I am occasionally getting the below error

> http: Accept error: accept tcp [::]:4545: too many open files; retrying in 10ms

Does net/http open files for every request?

答案1

得分: 0

将elithrar的评论发布为答案

套接字,是的。您可能需要通过ulimit或sysctl来增加您的fd限制。

英文:

Posting elithrar comment as answer

Sockets, yes. You may need to increase your fd limit (via ulimit or sysctl).

huangapple
  • 本文由 发表于 2015年11月28日 13:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/33968213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定