优化http服务器和客户端内存占用

This commit is contained in:
xiongziliang 2019-04-23 12:22:59 +08:00
parent 74621618ff
commit 5dfb7663e4
2 changed files with 10 additions and 5 deletions

View File

@ -186,7 +186,7 @@ int64_t HttpClient::onRecvHeader(const char *data, uint64_t len) {
//所以返回-1代表我们接下来分段接收content //所以返回-1代表我们接下来分段接收content
_recvedBodySize = 0; _recvedBodySize = 0;
//根据_totalBodySize设置接收缓存大小 //根据_totalBodySize设置接收缓存大小
_sock->setReadBuffer(std::make_shared<BufferRaw>(MAX(_totalBodySize + 1,256 * 1024))); _sock->setReadBuffer(std::make_shared<BufferRaw>(MIN(_totalBodySize + 1,256 * 1024)));
return -1; return -1;
} }

View File

@ -635,6 +635,14 @@ inline bool HttpSession::Handle_Req_POST(int64_t &content_len) {
return true; return true;
} }
//根据Content-Length设置接收缓存大小
if(totalContentLen > 0){
_sock->setReadBuffer(std::make_shared<BufferRaw>(MIN(totalContentLen + 1,256 * 1024)));
}else{
//不定长度的Content-Length
_sock->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
}
if(totalContentLen > 0 && totalContentLen < maxReqSize ){ if(totalContentLen > 0 && totalContentLen < maxReqSize ){
//返回固定长度的content //返回固定长度的content
content_len = totalContentLen; content_len = totalContentLen;
@ -652,9 +660,6 @@ inline bool HttpSession::Handle_Req_POST(int64_t &content_len) {
return false; return false;
}; };
}else{ }else{
//如果是post请求 并且totalContentLen数据超过maxReqSize那么我们加大接收缓存提升性能
_sock->setReadBuffer(std::make_shared<BufferRaw>(256 * 1024));
//返回不固定长度的content //返回不固定长度的content
content_len = -1; content_len = -1;
auto parserCopy = _parser; auto parserCopy = _parser;