Add header Keep-Alive: timeout=.. to http server responce [#METR-23933]

This commit is contained in:
proller 2016-12-30 23:52:56 +03:00
parent 220c7f2b93
commit c2a6681522
7 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#pragma once
#include <Poco/Net/HTTPServerResponse.h>
namespace DB
{
void setResponseDefaultHeaders(Poco::Net::HTTPServerResponse & response);
}

View File

@ -9,6 +9,7 @@
#include <DB/IO/WriteBuffer.h>
#include <DB/IO/BufferWithOwnMemory.h>
#include <DB/IO/HTTPCommon.h>
#include <DB/Common/NetException.h>
@ -53,6 +54,8 @@ private:
response.set("Access-Control-Allow-Origin","*");
}
setResponseDefaultHeaders(response);
if (compress && offset()) /// Пустой ответ сжимать не нужно.
{
if (compression_method == Poco::DeflatingStreamBuf::STREAM_GZIP)

View File

@ -0,0 +1,17 @@
#include <DB/IO/HTTPCommon.h>
#include <Poco/Util/Application.h>
namespace DB
{
void setResponseDefaultHeaders(Poco::Net::HTTPServerResponse & response) {
if (!response.getKeepAlive())
return;
Poco::Timespan keep_alive_timeout(Poco::Util::Application::instance().config().getInt("keep_alive_timeout", 10), 0);
if (keep_alive_timeout.totalSeconds())
response.set("Keep-Alive", "timeout=" + std::to_string(keep_alive_timeout.totalSeconds()));
}
}

View File

@ -4,6 +4,7 @@
#include <DB/Storages/StorageReplicatedMergeTree.h>
#include <DB/Common/HTMLForm.h>
#include <DB/Databases/IDatabase.h>
#include <DB/IO/HTTPCommon.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
@ -60,6 +61,8 @@ void ReplicasStatusHandler::handleRequest(Poco::Net::HTTPServerRequest & request
}
}
setResponseDefaultHeaders(response);
if (ok && !verbose)
{
const char * data = "Ok.\n";

View File

@ -17,6 +17,7 @@
#include <DB/Common/Macros.h>
#include <DB/Common/getFQDNOrHostName.h>
#include <DB/Common/StringUtils.h>
#include <DB/IO/HTTPCommon.h>
#include <DB/Interpreters/loadMetadata.h>
#include <DB/Interpreters/ProcessList.h>
@ -73,6 +74,7 @@ public:
{
try
{
setResponseDefaultHeaders(response);
const char * data = "Ok.\n";
response.sendBuffer(data, strlen(data));
}

View File

@ -0,0 +1,8 @@
< Connection: Keep-Alive
< Keep-Alive: timeout=3
< Connection: Keep-Alive
< Keep-Alive: timeout=3
< Connection: Keep-Alive
< Keep-Alive: timeout=3
< Connection: Keep-Alive
< Keep-Alive: timeout=3

View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
curl -vsS http://localhost:8123/ --data-binary @- <<< "SELECT 1" 2>&1 | perl -lnE 'print if /Keep-Alive/';
curl -vsS http://localhost:8123/ --data-binary @- <<< " error here " 2>&1 | perl -lnE 'print if /Keep-Alive/';
curl -vsS http://localhost:8123/ping 2>&1 | perl -lnE 'print if /Keep-Alive/';
curl -vsS http://localhost:8123/replicas_status 2>&1 | perl -lnE 'print if /Keep-Alive/';
# no keep-alive:
curl -vsS http://localhost:8123/404/not/found/ 2>&1 | perl -lnE 'print if /Keep-Alive/';