-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd_wastage.pike
25 lines (23 loc) · 1.24 KB
/
fd_wastage.pike
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
constant listen_addr = "::", listen_port = 9876;
void http_handler(Protocols.HTTP.Server.Request req) {
int before = sizeof(get_dir("/proc/self/fd"));
int garbo = gc(); //Disabling this check results in FDs accumulating.
int after = sizeof(get_dir("/proc/self/fd"));
werror("Garbage %O, closed %d files, now %d open\n", garbo, before - after, after);
//The "Connection: close" header is vital to the sockets becoming garbage.
//Without it, they are retained pending a followup request, which doesn't change the
//fundamental issue but does mean that a call to gc() doesn't clean them up.
req->response_and_finish((["data": "OK", "extra_heads": (["Connection": "close"])]));
//req->response_and_finish((["data": "OK"]));
}
int main() {
//If you don't have a cert, the first request is slower b/c generating self-signed.
//This ONLY happens with SSL connections. There are *three* file descriptors wasted
//for every request.
string cert = Stdio.read_file("certificate_local.pem");
string key = Stdio.read_file("privkey_local.pem");
array certs = cert && Standards.PEM.Messages(cert)->get_certificates();
string pk = key && Standards.PEM.simple_decode(key);
Protocols.HTTP.Server.SSLPort(http_handler, listen_port, listen_addr, pk, certs);
return -1;
}