Skip to content
Open
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
14 changes: 11 additions & 3 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,13 @@ namespace crow
return concurrency_;
}

/// \brief Set the maximum number of concurrent connections (default is unlimited with value 0)
self_t& max_connections(size_t size)
{
max_connections_ = size;
return *this;
}

/// \brief Set the server's log level
///
/// Possible values are:
Expand Down Expand Up @@ -639,7 +646,7 @@ namespace crow
}
tcp::endpoint endpoint(addr, port_);
router_.using_ssl = true;
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_, tcp_socket_options_)));
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, max_connections_, timeout_, &ssl_context_, tcp_socket_options_)));
ssl_server_->set_tick_function(tick_interval_, tick_function_);
ssl_server_->signal_clear();
for (auto snum : signals_)
Expand All @@ -655,7 +662,7 @@ namespace crow
if (use_unix_)
{
UnixSocketAcceptor::endpoint endpoint(bindaddr_);
unix_server_ = std::move(std::unique_ptr<unix_server_t>(new unix_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
unix_server_ = std::move(std::unique_ptr<unix_server_t>(new unix_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, max_connections_, timeout_, nullptr)));
unix_server_->set_tick_function(tick_interval_, tick_function_);
for (auto snum : signals_)
{
Expand All @@ -673,7 +680,7 @@ namespace crow
return;
}
TCPAcceptor::endpoint endpoint(addr, port_);
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr, tcp_socket_options_)));
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, endpoint, server_name_, &middlewares_, concurrency_, max_connections_, timeout_, nullptr, tcp_socket_options_)));
server_->set_tick_function(tick_interval_, tick_function_);
for (auto snum : signals_)
{
Expand Down Expand Up @@ -912,6 +919,7 @@ namespace crow
std::uint8_t timeout_{5};
uint16_t port_ = 80;
unsigned int concurrency_ = 2;
size_t max_connections_{};
std::atomic_bool is_bound_ = false;
uint64_t max_payload_{UINT64_MAX};
std::string server_name_ = std::string("Crow/") + VERSION;
Expand Down
12 changes: 10 additions & 2 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <memory>
#include <thread>
#include <vector>
#include <optional>

#include "crow/version.h"
#include "crow/http_connection.h"
Expand Down Expand Up @@ -51,11 +52,13 @@ namespace crow // NOTE: Already documented in "crow/app.h"
std::string server_name = std::string("Crow/") + VERSION,
std::tuple<Middlewares...>* middlewares = nullptr,
unsigned int concurrency = 1,
size_t max_connections = 0,
uint8_t timeout = 5,
typename Adaptor::context* adaptor_ctx = nullptr,
detail::socket::tcp_socket_options tcp_socket_options = {}):
concurrency_(concurrency),
task_queue_length_pool_(concurrency_ - 1),
max_task_queue_length_(max_connections),
acceptor_(io_context_),
signals_(io_context_),
tick_timer_(io_context_),
Expand Down Expand Up @@ -318,8 +321,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"

acceptor_.raw_acceptor().async_accept(
p->socket(),
[this, p, &ic](error_code ec) {
if (!ec)
[this, p, &ic, context_idx](error_code ec) {
if(max_task_queue_length_ > 0 && task_queue_length_pool_[context_idx] > max_task_queue_length_)
{
CROW_LOG_DEBUG << "Too many queued tasks for io context " << &ic << " {" << context_idx << "}, rejecting connection. Queue length: " << task_queue_length_pool_[context_idx];
}
else if (!ec)
{
detail::socket::apply_tcp_socket_options(p->socket(), tcp_socket_options_);
asio::post(ic,
Expand All @@ -343,6 +350,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
private:
unsigned int concurrency_{2};
std::vector<std::atomic<unsigned int>> task_queue_length_pool_;
size_t max_task_queue_length_{};
std::vector<std::unique_ptr<asio::io_context>> io_context_pool_;
asio::io_context io_context_;
std::vector<detail::task_timer*> task_timer_pool_;
Expand Down
Loading