mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-11-30 02:37:57 +08:00
Promote performance
This commit is contained in:
parent
3ecd0c0e51
commit
bd5cd65c6a
@ -71,7 +71,7 @@ int gzipDecompress(const char *zdata, const size_t nzdata,
|
||||
* like this:Sun, 06 Nov 1994 08:49:37 GMT
|
||||
* Wed, 12 Sep 2018 09:22:40 GMT
|
||||
*/
|
||||
char *getHttpFullDate(const trantor::Date &date);
|
||||
char *getHttpFullDate(const trantor::Date &date = trantor::Date::now(), bool *isChanged = nullptr);
|
||||
|
||||
/// Get a formatted string
|
||||
std::string formattedString(const char *format, ...);
|
||||
|
@ -360,6 +360,17 @@ void HttpResponseImpl::makeHeaderString(MsgBuffer *output) const
|
||||
|
||||
void HttpResponseImpl::appendToBuffer(MsgBuffer *output) const
|
||||
{
|
||||
if (_expriedTime >= 0 && _httpString && _datePos != std::string::npos)
|
||||
{
|
||||
bool isDateChanged = false;
|
||||
auto newDate = getHttpFullDate(trantor::Date::now(), &isDateChanged);
|
||||
if(isDateChanged)
|
||||
{
|
||||
memcpy(_httpString->data() + _datePos, newDate, strlen(newDate));
|
||||
}
|
||||
output->append(*_httpString);
|
||||
return;
|
||||
}
|
||||
if (!_fullHeaderString)
|
||||
{
|
||||
makeHeaderString(output);
|
||||
@ -381,9 +392,17 @@ void HttpResponseImpl::appendToBuffer(MsgBuffer *output) const
|
||||
|
||||
//output Date header
|
||||
output->append("Date: ");
|
||||
if (_expriedTime >= 0)
|
||||
{
|
||||
_datePos = output->readableBytes();
|
||||
}
|
||||
output->append(getHttpFullDate(trantor::Date::date()));
|
||||
output->append("\r\n\r\n");
|
||||
|
||||
LOG_TRACE << "reponse(no body):" << output->peek();
|
||||
output->append(*_bodyPtr);
|
||||
if (_expriedTime >= 0)
|
||||
{
|
||||
_httpString = std::make_shared<std::string>(output->peek(), output->readableBytes());
|
||||
}
|
||||
}
|
||||
|
@ -255,6 +255,8 @@ class HttpResponseImpl : public HttpResponse
|
||||
std::swap(_contentType, that._contentType);
|
||||
_jsonPtr.swap(that._jsonPtr);
|
||||
_fullHeaderString.swap(that._fullHeaderString);
|
||||
_httpString.swap(that._httpString);
|
||||
std::swap(_datePos, that._datePos);
|
||||
}
|
||||
void parseJson() const
|
||||
{
|
||||
@ -322,6 +324,8 @@ class HttpResponseImpl : public HttpResponse
|
||||
mutable std::shared_ptr<Json::Value> _jsonPtr;
|
||||
|
||||
std::shared_ptr<std::string> _fullHeaderString;
|
||||
mutable std::shared_ptr<std::string> _httpString;
|
||||
mutable std::string::size_type _datePos = std::string::npos;
|
||||
//trantor::Date receiveTime_;
|
||||
|
||||
void setContentType(const std::string &contentType)
|
||||
|
@ -175,6 +175,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
|
||||
{
|
||||
//Cached response,make a copy
|
||||
newResp = std::make_shared<HttpResponseImpl>(*std::dynamic_pointer_cast<HttpResponseImpl>(response));
|
||||
newResp->setExpiredTime(-1);
|
||||
}
|
||||
newResp->setBody(std::string());
|
||||
}
|
||||
@ -202,6 +203,7 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r
|
||||
{
|
||||
//cached response,we need to make a clone
|
||||
newResp = std::make_shared<HttpResponseImpl>(*std::dynamic_pointer_cast<HttpResponseImpl>(response));
|
||||
newResp->setExpiredTime(-1);
|
||||
}
|
||||
newResp->setBody(std::string(zbuf, zlen));
|
||||
newResp->addHeader("Content-Encoding", "gzip");
|
||||
|
@ -404,15 +404,19 @@ int gzipDecompress(const char *zdata, const size_t nzdata,
|
||||
*ndata = d_stream.total_out;
|
||||
return 0;
|
||||
}
|
||||
char *getHttpFullDate(const trantor::Date &date)
|
||||
char *getHttpFullDate(const trantor::Date &date, bool *isChanged)
|
||||
{
|
||||
static __thread int64_t lastSecond = 0;
|
||||
static __thread char lastTimeString[128] = {0};
|
||||
auto nowSecond = date.microSecondsSinceEpoch() / MICRO_SECONDS_PRE_SEC;
|
||||
if (nowSecond == lastSecond)
|
||||
{
|
||||
if(isChanged)
|
||||
*isChanged = false;
|
||||
return lastTimeString;
|
||||
}
|
||||
if(isChanged)
|
||||
*isChanged = true;
|
||||
lastSecond = nowSecond;
|
||||
date.toCustomedFormattedString("%a, %d %b %Y %T GMT", lastTimeString, sizeof(lastTimeString));
|
||||
return lastTimeString;
|
||||
|
Loading…
Reference in New Issue
Block a user