我创建了简单的基于 evhttp 的服务器。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <event.h>
#include <evhttp.h>
void
handler(struct evhttp_request *req, void *arg) {
struct evbuffer *buf;
buf = evbuffer_new();
if(buf == NULL) {
fprintf(stderr, "ERROR: Failed to create response buffer\n");
exit(EXIT_FAILURE);
}
evbuffer_add_printf(buf, "Server called");
evhttp_send_reply(req, HTTP_OK, "OK", buf);
}
int
main(int argc, char **argv) {
struct evhttp *http;
event_init();
http = evhttp_start("0.0.0.0", 8081);
evhttp_set_gencb(http, handler, NULL);
event_dispatch();
evhttp_free(http);
exit(EXIT_SUCCESS);
}
当我开始使用它进行基准测试时
ab -r -n 1000 -c 50 http://0.0.0.0:8081/
经过几次尝试后,我收到了这些警告:
[warn] Error from accept() call: Too many open files
有点像我没有关闭套接字......并发级别 50 的目标是一次只使用 50 个套接字,对吧?
我应该在处理函数中关闭套接字吗?