Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dumb_timer_queue with new task_timer #278

Merged
merged 5 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/crow.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "crow/json.h"
#include "crow/mustache.h"
#include "crow/logging.h"
#include "crow/dumb_timer_queue.h"
#include "crow/task_timer.h"
#include "crow/utility.h"
#include "crow/common.h"
#include "crow/http_request.h"
Expand Down
13 changes: 5 additions & 8 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "crow/middleware_context.h"
#include "crow/http_request.h"
#include "crow/http_server.h"
#include "crow/dumb_timer_queue.h"
#include "crow/task_timer.h"
#ifdef CROW_ENABLE_COMPRESSION
#include "crow/compression.h"
#endif
Expand All @@ -35,10 +35,6 @@

namespace crow
{
#ifdef CROW_MAIN
int detail::dumb_timer_queue::tick = 5;
#endif

#ifdef CROW_ENABLE_SSL
using ssl_context_t = boost::asio::ssl::context;
#endif
Expand Down Expand Up @@ -131,7 +127,7 @@ namespace crow
///Set the connection timeout in seconds (default is 5)
self_t& timeout(std::uint8_t timeout)
{
detail::dumb_timer_queue::tick = timeout;
timeout_ = timeout;
return *this;
}

Expand Down Expand Up @@ -284,15 +280,15 @@ namespace crow
#ifdef CROW_ENABLE_SSL
if (use_ssl_)
{
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, &ssl_context_)));
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
ssl_server_->set_tick_function(tick_interval_, tick_function_);
notify_server_start();
ssl_server_->run();
}
else
#endif
{
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, nullptr)));
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
server_->set_tick_function(tick_interval_, tick_function_);
server_->signal_clear();
for (auto snum : signals_)
Expand Down Expand Up @@ -424,6 +420,7 @@ namespace crow
}

private:
std::uint8_t timeout_{5};
uint16_t port_ = 80;
uint16_t concurrency_ = 1;
bool validated_ = false;
Expand Down
83 changes: 0 additions & 83 deletions include/crow/dumb_timer_queue.h

This file was deleted.

21 changes: 10 additions & 11 deletions include/crow/http_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "crow/http_response.h"
#include "crow/logging.h"
#include "crow/settings.h"
#include "crow/dumb_timer_queue.h"
#include "crow/task_timer.h"
#include "crow/middleware_context.h"
#include "crow/socket_adaptors.h"
#include "crow/compression.h"
Expand Down Expand Up @@ -193,7 +193,7 @@ namespace crow
const std::string& server_name,
std::tuple<Middlewares...>* middlewares,
std::function<std::string()>& get_cached_date_str_f,
detail::dumb_timer_queue& timer_queue,
detail::task_timer& task_timer,
typename Adaptor::context* adaptor_ctx_
)
: adaptor_(io_service, adaptor_ctx_),
Expand All @@ -202,7 +202,7 @@ namespace crow
server_name_(server_name),
middlewares_(middlewares),
get_cached_date_str(get_cached_date_str_f),
timer_queue(timer_queue),
task_timer_(task_timer),
res_stream_threshold_(handler->stream_threshold())
{
#ifdef CROW_ENABLE_DEBUG
Expand Down Expand Up @@ -653,15 +653,15 @@ namespace crow

void cancel_deadline_timer()
{
CROW_LOG_DEBUG << this << " timer cancelled: " << timer_cancel_key_.first << ' ' << timer_cancel_key_.second;
timer_queue.cancel(timer_cancel_key_);
CROW_LOG_DEBUG << this << " timer cancelled: " << &task_timer_ << ' ' << task_id_;
task_timer_.cancel(task_id_);
}

void start_deadline(/*int timeout = 5*/)
{
cancel_deadline_timer();
timer_cancel_key_ = timer_queue.add([this]

task_id_ = task_timer_.schedule([this]
{
if (!adaptor_.is_open())
{
Expand All @@ -670,7 +670,7 @@ namespace crow
adaptor_.shutdown_readwrite();
adaptor_.close();
});
CROW_LOG_DEBUG << this << " timer added: " << timer_cancel_key_.first << ' ' << timer_cancel_key_.second;
CROW_LOG_DEBUG << this << " timer added: " << &task_timer_ << ' ' << task_id_;
}

private:
Expand All @@ -692,8 +692,7 @@ namespace crow
std::string date_str_;
std::string res_body_copy_;

//boost::asio::deadline_timer deadline_;
detail::dumb_timer_queue::key timer_cancel_key_;
detail::task_timer::identifier_type task_id_;

bool is_reading{};
bool is_writing{};
Expand All @@ -705,7 +704,7 @@ namespace crow
detail::context<Middlewares...> ctx_;

std::function<std::string()>& get_cached_date_str;
detail::dumb_timer_queue& timer_queue;
detail::task_timer& task_timer_;

size_t res_stream_threshold_;
};
Expand Down
34 changes: 11 additions & 23 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "crow/version.h"
#include "crow/http_connection.h"
#include "crow/logging.h"
#include "crow/dumb_timer_queue.h"
#include "crow/task_timer.h"

namespace crow
{
Expand All @@ -27,12 +27,13 @@ namespace crow
class Server
{
public:
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, typename Adaptor::context* adaptor_ctx = nullptr)
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr)
: acceptor_(io_service_, tcp::endpoint(boost::asio::ip::address::from_string(bindaddr), port)),
signals_(io_service_, SIGINT, SIGTERM),
tick_timer_(io_service_),
handler_(handler),
concurrency_(concurrency == 0 ? 1 : concurrency),
timeout_(timeout),
server_name_(server_name),
port_(port),
bindaddr_(bindaddr),
Expand Down Expand Up @@ -64,7 +65,7 @@ namespace crow
for(int i = 0; i < concurrency_; i++)
io_service_pool_.emplace_back(new boost::asio::io_service());
get_cached_date_str_pool_.resize(concurrency_);
timer_queue_pool_.resize(concurrency_);
task_timer_pool_.resize(concurrency_);

std::vector<std::future<void>> v;
std::atomic<int> init_count(0);
Expand Down Expand Up @@ -101,23 +102,10 @@ namespace crow
return date_str;
};

// initializing timer queue
detail::dumb_timer_queue timer_queue;
timer_queue_pool_[i] = &timer_queue;

timer_queue.set_io_service(*io_service_pool_[i]);
boost::asio::deadline_timer timer(*io_service_pool_[i]);
timer.expires_from_now(boost::posix_time::seconds(1));

std::function<void(const boost::system::error_code& ec)> handler;
handler = [&](const boost::system::error_code& ec){
if (ec)
return;
timer_queue.process();
timer.expires_from_now(boost::posix_time::seconds(1));
timer.async_wait(handler);
};
timer.async_wait(handler);
// initializing task timers
detail::task_timer task_timer(*io_service_pool_[i]);
task_timer.set_default_timeout(timeout_);
task_timer_pool_[i] = &task_timer;

init_count ++;
while(1)
Expand Down Expand Up @@ -202,8 +190,7 @@ namespace crow
asio::io_service& is = pick_io_service();
auto p = new Connection<Adaptor, Handler, Middlewares...>(
is, handler_, server_name_, middlewares_,
get_cached_date_str_pool_[roundrobin_index_], *timer_queue_pool_[roundrobin_index_],
adaptor_ctx_);
get_cached_date_str_pool_[roundrobin_index_], *task_timer_pool_[roundrobin_index_], adaptor_ctx_);
acceptor_.async_accept(p->socket(),
[this, p, &is](boost::system::error_code ec)
{
Expand All @@ -225,14 +212,15 @@ namespace crow
private:
asio::io_service io_service_;
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
std::vector<detail::dumb_timer_queue*> timer_queue_pool_;
std::vector<detail::task_timer*> task_timer_pool_;
std::vector<std::function<std::string()>> get_cached_date_str_pool_;
tcp::acceptor acceptor_;
boost::asio::signal_set signals_;
boost::asio::deadline_timer tick_timer_;

Handler* handler_;
uint16_t concurrency_{1};
std::uint8_t timeout_;
std::string server_name_;
uint16_t port_;
std::string bindaddr_;
Expand Down
Loading