Skip to content

Commit b1718be

Browse files
authored
Merge pull request #278 from CrowCpp/wip-luca-schlecker
Replace `dumb_timer_queue` with new `task_timer`
2 parents 22d3918 + 250c793 commit b1718be

File tree

7 files changed

+201
-126
lines changed

7 files changed

+201
-126
lines changed

include/crow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "crow/json.h"
99
#include "crow/mustache.h"
1010
#include "crow/logging.h"
11-
#include "crow/dumb_timer_queue.h"
11+
#include "crow/task_timer.h"
1212
#include "crow/utility.h"
1313
#include "crow/common.h"
1414
#include "crow/http_request.h"

include/crow/app.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "crow/middleware_context.h"
1919
#include "crow/http_request.h"
2020
#include "crow/http_server.h"
21-
#include "crow/dumb_timer_queue.h"
21+
#include "crow/task_timer.h"
2222
#ifdef CROW_ENABLE_COMPRESSION
2323
#include "crow/compression.h"
2424
#endif
@@ -35,10 +35,6 @@
3535

3636
namespace crow
3737
{
38-
#ifdef CROW_MAIN
39-
int detail::dumb_timer_queue::tick = 5;
40-
#endif
41-
4238
#ifdef CROW_ENABLE_SSL
4339
using ssl_context_t = boost::asio::ssl::context;
4440
#endif
@@ -131,7 +127,7 @@ namespace crow
131127
///Set the connection timeout in seconds (default is 5)
132128
self_t& timeout(std::uint8_t timeout)
133129
{
134-
detail::dumb_timer_queue::tick = timeout;
130+
timeout_ = timeout;
135131
return *this;
136132
}
137133

@@ -284,15 +280,15 @@ namespace crow
284280
#ifdef CROW_ENABLE_SSL
285281
if (use_ssl_)
286282
{
287-
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, &ssl_context_)));
283+
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
288284
ssl_server_->set_tick_function(tick_interval_, tick_function_);
289285
notify_server_start();
290286
ssl_server_->run();
291287
}
292288
else
293289
#endif
294290
{
295-
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, nullptr)));
291+
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
296292
server_->set_tick_function(tick_interval_, tick_function_);
297293
server_->signal_clear();
298294
for (auto snum : signals_)
@@ -424,6 +420,7 @@ namespace crow
424420
}
425421

426422
private:
423+
std::uint8_t timeout_{5};
427424
uint16_t port_ = 80;
428425
uint16_t concurrency_ = 1;
429426
bool validated_ = false;

include/crow/dumb_timer_queue.h

Lines changed: 0 additions & 83 deletions
This file was deleted.

include/crow/http_connection.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "crow/http_response.h"
1515
#include "crow/logging.h"
1616
#include "crow/settings.h"
17-
#include "crow/dumb_timer_queue.h"
17+
#include "crow/task_timer.h"
1818
#include "crow/middleware_context.h"
1919
#include "crow/socket_adaptors.h"
2020
#include "crow/compression.h"
@@ -193,7 +193,7 @@ namespace crow
193193
const std::string& server_name,
194194
std::tuple<Middlewares...>* middlewares,
195195
std::function<std::string()>& get_cached_date_str_f,
196-
detail::dumb_timer_queue& timer_queue,
196+
detail::task_timer& task_timer,
197197
typename Adaptor::context* adaptor_ctx_
198198
)
199199
: adaptor_(io_service, adaptor_ctx_),
@@ -202,7 +202,7 @@ namespace crow
202202
server_name_(server_name),
203203
middlewares_(middlewares),
204204
get_cached_date_str(get_cached_date_str_f),
205-
timer_queue(timer_queue),
205+
task_timer_(task_timer),
206206
res_stream_threshold_(handler->stream_threshold())
207207
{
208208
#ifdef CROW_ENABLE_DEBUG
@@ -653,15 +653,15 @@ namespace crow
653653

654654
void cancel_deadline_timer()
655655
{
656-
CROW_LOG_DEBUG << this << " timer cancelled: " << timer_cancel_key_.first << ' ' << timer_cancel_key_.second;
657-
timer_queue.cancel(timer_cancel_key_);
656+
CROW_LOG_DEBUG << this << " timer cancelled: " << &task_timer_ << ' ' << task_id_;
657+
task_timer_.cancel(task_id_);
658658
}
659659

660660
void start_deadline(/*int timeout = 5*/)
661661
{
662662
cancel_deadline_timer();
663-
664-
timer_cancel_key_ = timer_queue.add([this]
663+
664+
task_id_ = task_timer_.schedule([this]
665665
{
666666
if (!adaptor_.is_open())
667667
{
@@ -670,7 +670,7 @@ namespace crow
670670
adaptor_.shutdown_readwrite();
671671
adaptor_.close();
672672
});
673-
CROW_LOG_DEBUG << this << " timer added: " << timer_cancel_key_.first << ' ' << timer_cancel_key_.second;
673+
CROW_LOG_DEBUG << this << " timer added: " << &task_timer_ << ' ' << task_id_;
674674
}
675675

676676
private:
@@ -692,8 +692,7 @@ namespace crow
692692
std::string date_str_;
693693
std::string res_body_copy_;
694694

695-
//boost::asio::deadline_timer deadline_;
696-
detail::dumb_timer_queue::key timer_cancel_key_;
695+
detail::task_timer::identifier_type task_id_;
697696

698697
bool is_reading{};
699698
bool is_writing{};
@@ -705,7 +704,7 @@ namespace crow
705704
detail::context<Middlewares...> ctx_;
706705

707706
std::function<std::string()>& get_cached_date_str;
708-
detail::dumb_timer_queue& timer_queue;
707+
detail::task_timer& task_timer_;
709708

710709
size_t res_stream_threshold_;
711710
};

include/crow/http_server.h

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "crow/version.h"
1717
#include "crow/http_connection.h"
1818
#include "crow/logging.h"
19-
#include "crow/dumb_timer_queue.h"
19+
#include "crow/task_timer.h"
2020

2121
namespace crow
2222
{
@@ -27,12 +27,13 @@ namespace crow
2727
class Server
2828
{
2929
public:
30-
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)
30+
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)
3131
: acceptor_(io_service_, tcp::endpoint(boost::asio::ip::address::from_string(bindaddr), port)),
3232
signals_(io_service_, SIGINT, SIGTERM),
3333
tick_timer_(io_service_),
3434
handler_(handler),
3535
concurrency_(concurrency == 0 ? 1 : concurrency),
36+
timeout_(timeout),
3637
server_name_(server_name),
3738
port_(port),
3839
bindaddr_(bindaddr),
@@ -64,7 +65,7 @@ namespace crow
6465
for(int i = 0; i < concurrency_; i++)
6566
io_service_pool_.emplace_back(new boost::asio::io_service());
6667
get_cached_date_str_pool_.resize(concurrency_);
67-
timer_queue_pool_.resize(concurrency_);
68+
task_timer_pool_.resize(concurrency_);
6869

6970
std::vector<std::future<void>> v;
7071
std::atomic<int> init_count(0);
@@ -101,23 +102,10 @@ namespace crow
101102
return date_str;
102103
};
103104

104-
// initializing timer queue
105-
detail::dumb_timer_queue timer_queue;
106-
timer_queue_pool_[i] = &timer_queue;
107-
108-
timer_queue.set_io_service(*io_service_pool_[i]);
109-
boost::asio::deadline_timer timer(*io_service_pool_[i]);
110-
timer.expires_from_now(boost::posix_time::seconds(1));
111-
112-
std::function<void(const boost::system::error_code& ec)> handler;
113-
handler = [&](const boost::system::error_code& ec){
114-
if (ec)
115-
return;
116-
timer_queue.process();
117-
timer.expires_from_now(boost::posix_time::seconds(1));
118-
timer.async_wait(handler);
119-
};
120-
timer.async_wait(handler);
105+
// initializing task timers
106+
detail::task_timer task_timer(*io_service_pool_[i]);
107+
task_timer.set_default_timeout(timeout_);
108+
task_timer_pool_[i] = &task_timer;
121109

122110
init_count ++;
123111
while(1)
@@ -202,8 +190,7 @@ namespace crow
202190
asio::io_service& is = pick_io_service();
203191
auto p = new Connection<Adaptor, Handler, Middlewares...>(
204192
is, handler_, server_name_, middlewares_,
205-
get_cached_date_str_pool_[roundrobin_index_], *timer_queue_pool_[roundrobin_index_],
206-
adaptor_ctx_);
193+
get_cached_date_str_pool_[roundrobin_index_], *task_timer_pool_[roundrobin_index_], adaptor_ctx_);
207194
acceptor_.async_accept(p->socket(),
208195
[this, p, &is](boost::system::error_code ec)
209196
{
@@ -225,14 +212,15 @@ namespace crow
225212
private:
226213
asio::io_service io_service_;
227214
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
228-
std::vector<detail::dumb_timer_queue*> timer_queue_pool_;
215+
std::vector<detail::task_timer*> task_timer_pool_;
229216
std::vector<std::function<std::string()>> get_cached_date_str_pool_;
230217
tcp::acceptor acceptor_;
231218
boost::asio::signal_set signals_;
232219
boost::asio::deadline_timer tick_timer_;
233220

234221
Handler* handler_;
235222
uint16_t concurrency_{1};
223+
std::uint8_t timeout_;
236224
std::string server_name_;
237225
uint16_t port_;
238226
std::string bindaddr_;

0 commit comments

Comments
 (0)