Add timeout control to http server (draft)
This commit is contained in:
@@ -16,7 +16,8 @@ namespace webcc {
|
||||
|
||||
HttpServer::HttpServer(unsigned short port, std::size_t workers)
|
||||
: signals_(io_context_)
|
||||
, workers_(workers) {
|
||||
, workers_(workers)
|
||||
, timeout_seconds_(0) {
|
||||
|
||||
// Register to handle the signals that indicate when the server should exit.
|
||||
// It is safe to register for the same signal multiple times in a program,
|
||||
@@ -47,7 +48,7 @@ HttpServer::~HttpServer() {
|
||||
}
|
||||
|
||||
void HttpServer::Run() {
|
||||
assert(request_handler_ != NULL);
|
||||
assert(GetRequestHandler() != NULL);
|
||||
|
||||
#if WEBCC_DEBUG_OUTPUT
|
||||
boost::thread::id thread_id = boost::this_thread::get_id();
|
||||
@@ -55,7 +56,7 @@ void HttpServer::Run() {
|
||||
#endif
|
||||
|
||||
// Start worker threads.
|
||||
request_handler_->Start(workers_);
|
||||
GetRequestHandler()->Start(workers_);
|
||||
|
||||
// The io_context::run() call will block until all asynchronous operations
|
||||
// have finished. While the server is running, there is always at least one
|
||||
@@ -74,10 +75,10 @@ void HttpServer::DoAccept() {
|
||||
}
|
||||
|
||||
if (!ec) {
|
||||
HttpSessionPtr conn{
|
||||
new HttpSession(std::move(socket), request_handler_)
|
||||
HttpSessionPtr session{
|
||||
new HttpSession(std::move(socket), GetRequestHandler())
|
||||
};
|
||||
conn->Start();
|
||||
session->Start(timeout_seconds_);
|
||||
}
|
||||
|
||||
DoAccept();
|
||||
@@ -91,7 +92,7 @@ void HttpServer::DoAwaitStop() {
|
||||
// operations. Once all operations have finished the io_context::run()
|
||||
// call will exit.
|
||||
acceptor_->close();
|
||||
request_handler_->Stop();
|
||||
GetRequestHandler()->Stop();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+10
-4
@@ -28,6 +28,10 @@ public:
|
||||
|
||||
virtual ~HttpServer();
|
||||
|
||||
void set_timeout_seconds(long seconds) {
|
||||
timeout_seconds_ = seconds;
|
||||
}
|
||||
|
||||
// Run the server's io_service loop.
|
||||
void Run();
|
||||
|
||||
@@ -38,10 +42,8 @@ private:
|
||||
// Wait for a request to stop the server.
|
||||
void DoAwaitStop();
|
||||
|
||||
protected:
|
||||
// The handler for all incoming requests.
|
||||
// TODO: Replace with virtual GetRequestHandler()?
|
||||
HttpRequestHandler* request_handler_;
|
||||
// Get the handler for incoming requests.
|
||||
virtual HttpRequestHandler* GetRequestHandler() = 0;
|
||||
|
||||
private:
|
||||
// The number of worker threads.
|
||||
@@ -55,6 +57,10 @@ private:
|
||||
|
||||
// Acceptor used to listen for incoming connections.
|
||||
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
|
||||
|
||||
// Timeout in seconds for socket connection.
|
||||
// Default is 0 which means no timeout.
|
||||
long timeout_seconds_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
+69
-10
@@ -6,6 +6,7 @@
|
||||
#endif
|
||||
|
||||
#include "boost/asio/write.hpp"
|
||||
#include "boost/date_time/posix_time/posix_time.hpp"
|
||||
|
||||
#include "webcc/http_request_handler.h"
|
||||
|
||||
@@ -18,12 +19,30 @@ HttpSession::HttpSession(boost::asio::ip::tcp::socket socket,
|
||||
, request_parser_(&request_) {
|
||||
}
|
||||
|
||||
void HttpSession::Start() {
|
||||
HttpSession::~HttpSession() {
|
||||
}
|
||||
|
||||
void HttpSession::Start(long timeout_seconds) {
|
||||
if (timeout_seconds > 0) {
|
||||
// Create timer only necessary.
|
||||
boost::asio::io_context& ioc = socket_.get_executor().context();
|
||||
timer_.reset(new boost::asio::deadline_timer(ioc));
|
||||
|
||||
timer_->expires_from_now(boost::posix_time::seconds(timeout_seconds));
|
||||
|
||||
timer_->async_wait(std::bind(&HttpSession::HandleTimer,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
DoRead();
|
||||
}
|
||||
|
||||
void HttpSession::Stop() {
|
||||
socket_.close();
|
||||
CancelTimer();
|
||||
|
||||
boost::system::error_code ignored_ec;
|
||||
socket_.close(ignored_ec);
|
||||
}
|
||||
|
||||
void HttpSession::SetResponseContent(const std::string& content_type,
|
||||
@@ -59,6 +78,8 @@ void HttpSession::HandleRead(boost::system::error_code ec,
|
||||
if (ec) {
|
||||
if (ec != boost::asio::error::operation_aborted) {
|
||||
Stop();
|
||||
} else {
|
||||
CancelTimer();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -89,20 +110,58 @@ void HttpSession::HandleRead(boost::system::error_code ec,
|
||||
// io_context.run), even though DoWrite() is invoked by worker threads. This is
|
||||
// ensured by Asio.
|
||||
void HttpSession::HandleWrite(boost::system::error_code ec,
|
||||
size_t length) {
|
||||
std::size_t length) {
|
||||
#if WEBCC_DEBUG_OUTPUT
|
||||
boost::thread::id thread_id = boost::this_thread::get_id();
|
||||
std::cout << "Response has been sent back (thread: " << thread_id << ")\n";
|
||||
#endif
|
||||
|
||||
if (!ec) {
|
||||
// Initiate graceful connection closure.
|
||||
boost::system::error_code ignored_ec;
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
|
||||
}
|
||||
CancelTimer();
|
||||
|
||||
if (ec != boost::asio::error::operation_aborted) {
|
||||
Stop();
|
||||
// Initiate graceful connection closure.
|
||||
boost::system::error_code ec;
|
||||
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
|
||||
|
||||
#if WEBCC_DEBUG_OUTPUT
|
||||
std::cout << "Response has been sent back (thread: " << thread_id << ")\n";
|
||||
#endif
|
||||
} else {
|
||||
#if WEBCC_DEBUG_OUTPUT
|
||||
std::cout << "(thread: " << thread_id << ") Sending response error: "
|
||||
<< ec.message()
|
||||
<< std::endl;
|
||||
#endif
|
||||
|
||||
if (ec == boost::asio::error::operation_aborted) {
|
||||
CancelTimer();
|
||||
} else {
|
||||
Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpSession::HandleTimer(boost::system::error_code ec) {
|
||||
std::cout << "HandleTimer: ";
|
||||
|
||||
if (!ec) {
|
||||
if (socket_.is_open()) {
|
||||
std::cout << "socket is open, close it.\n";
|
||||
socket_.close();
|
||||
} else {
|
||||
std::cout << "socket is not open.\n";
|
||||
}
|
||||
} else {
|
||||
if (ec == boost::asio::error::operation_aborted) {
|
||||
std::cout << "Timer aborted\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpSession::CancelTimer() {
|
||||
if (timer_) {
|
||||
// The wait handler will be invoked with the operation_aborted error code.
|
||||
boost::system::error_code ec;
|
||||
timer_->cancel(ec);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "boost/asio/ip/tcp.hpp" // for ip::tcp::socket
|
||||
#include "boost/asio/deadline_timer.hpp"
|
||||
|
||||
#include "webcc/common.h"
|
||||
#include "webcc/http_request.h"
|
||||
@@ -17,19 +18,20 @@ class HttpRequestHandler;
|
||||
|
||||
class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
||||
public:
|
||||
friend class HttpRequestHandler;
|
||||
|
||||
HttpSession(const HttpSession&) = delete;
|
||||
HttpSession& operator=(const HttpSession&) = delete;
|
||||
|
||||
HttpSession(boost::asio::ip::tcp::socket socket,
|
||||
HttpRequestHandler* handler);
|
||||
|
||||
~HttpSession();
|
||||
|
||||
const HttpRequest& request() const {
|
||||
return request_;
|
||||
}
|
||||
|
||||
void Start();
|
||||
// Start the session with an optional timeout.
|
||||
void Start(long timeout_seconds = 0);
|
||||
|
||||
void Stop();
|
||||
|
||||
@@ -49,16 +51,20 @@ private:
|
||||
|
||||
void DoWrite();
|
||||
|
||||
void HandleRead(boost::system::error_code ec,
|
||||
std::size_t length);
|
||||
void HandleRead(boost::system::error_code ec, std::size_t length);
|
||||
void HandleWrite(boost::system::error_code ec, std::size_t length);
|
||||
|
||||
void HandleWrite(boost::system::error_code ec,
|
||||
std::size_t length);
|
||||
void HandleTimer(boost::system::error_code ec);
|
||||
|
||||
void CancelTimer();
|
||||
|
||||
private:
|
||||
// Socket for the connection.
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
|
||||
// Timeout timer (optional).
|
||||
std::unique_ptr<boost::asio::deadline_timer> timer_;
|
||||
|
||||
// The handler used to process the incoming request.
|
||||
HttpRequestHandler* request_handler_;
|
||||
|
||||
|
||||
@@ -104,18 +104,16 @@ HttpStatus::Enum RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
||||
|
||||
RestServer::RestServer(unsigned short port, std::size_t workers)
|
||||
: HttpServer(port, workers)
|
||||
, rest_request_handler_(new RestRequestHandler()) {
|
||||
request_handler_ = rest_request_handler_;
|
||||
, request_handler_(new RestRequestHandler()) {
|
||||
}
|
||||
|
||||
RestServer::~RestServer() {
|
||||
request_handler_ = NULL;
|
||||
delete rest_request_handler_;
|
||||
delete request_handler_;
|
||||
}
|
||||
|
||||
bool RestServer::RegisterService(RestServicePtr service,
|
||||
const std::string& url) {
|
||||
return rest_request_handler_->RegisterService(service, url);
|
||||
return request_handler_->RegisterService(service, url);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -97,7 +97,12 @@ public:
|
||||
bool RegisterService(RestServicePtr service, const std::string& url);
|
||||
|
||||
private:
|
||||
RestRequestHandler* rest_request_handler_;
|
||||
HttpRequestHandler* GetRequestHandler() override {
|
||||
return request_handler_;
|
||||
}
|
||||
|
||||
private:
|
||||
RestRequestHandler* request_handler_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -72,18 +72,16 @@ SoapServicePtr SoapRequestHandler::GetServiceByUrl(const std::string& url) {
|
||||
|
||||
SoapServer::SoapServer(unsigned short port, std::size_t workers)
|
||||
: HttpServer(port, workers)
|
||||
, soap_request_handler_(new SoapRequestHandler()) {
|
||||
request_handler_ = soap_request_handler_;
|
||||
, request_handler_(new SoapRequestHandler()) {
|
||||
}
|
||||
|
||||
SoapServer::~SoapServer() {
|
||||
request_handler_ = NULL;
|
||||
delete soap_request_handler_;
|
||||
delete request_handler_;
|
||||
}
|
||||
|
||||
bool SoapServer::RegisterService(SoapServicePtr service,
|
||||
const std::string& url) {
|
||||
return soap_request_handler_->RegisterService(service, url);
|
||||
return request_handler_->RegisterService(service, url);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -44,7 +44,12 @@ public:
|
||||
bool RegisterService(SoapServicePtr service, const std::string& url);
|
||||
|
||||
private:
|
||||
SoapRequestHandler* soap_request_handler_;
|
||||
HttpRequestHandler* GetRequestHandler() override {
|
||||
return request_handler_;
|
||||
}
|
||||
|
||||
private:
|
||||
SoapRequestHandler* request_handler_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
Reference in New Issue
Block a user