Use larger socket buffers for serving files to improve efficiency.

The combination of nodelay + minimum buffers is horrific for
file-serving; speedup is from 3.3s to 33ms to serve bundle.js.
This commit is contained in:
Michael Meeks 2017-03-09 21:46:52 +00:00 committed by Jan Holesovsky
parent 42a19e66e3
commit 3bfc8aa7f3

View file

@ -780,6 +780,10 @@ namespace HttpHelper
return;
}
const int socketBufferSize = 16 * 1024;
if (st.st_size >= socketBufferSize)
socket->setSendBufferSize(socketBufferSize);
response.setContentLength(st.st_size);
response.set("User-Agent", HTTP_AGENT_STRING);
std::ostringstream oss;
@ -789,15 +793,17 @@ namespace HttpHelper
socket->sendHttpResponse(header);
std::ifstream file(path, std::ios::binary);
bool flush = true;
do
{
char buf[16 * 1024];
char buf[socketBufferSize];
file.read(buf, sizeof(buf));
const int size = file.gcount();
if (size > 0)
socket->send(buf, size);
socket->send(buf, size, flush);
else
break;
flush = false;
}
while (file);
}