Now closes unwanted client connections when a request is finished. Also added additional tests
This commit is contained in:
parent
20d3c44366
commit
362c1a2591
2 changed files with 84 additions and 13 deletions
|
|
@ -165,11 +165,26 @@ namespace SimpleWeb {
|
|||
auto connection=session->connection;
|
||||
auto response=session->response;
|
||||
auto request_callback=std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code&)>>(std::move(request_callback_));
|
||||
auto connections=this->connections;
|
||||
auto connections_mutex=this->connections_mutex;
|
||||
session->callback=[connection, response, request_callback, connections_mutex](const error_code &ec) {
|
||||
session->callback=[connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
||||
connection->in_use=false;
|
||||
|
||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||
size_t unused_connections=0;
|
||||
for(auto it=connections->begin();it!=connections->end();) {
|
||||
if((*it)->in_use)
|
||||
++it;
|
||||
else {
|
||||
++unused_connections;
|
||||
if(unused_connections>1)
|
||||
it=connections->erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(*request_callback)
|
||||
|
|
@ -208,11 +223,26 @@ namespace SimpleWeb {
|
|||
auto connection=session->connection;
|
||||
auto response=session->response;
|
||||
auto request_callback=std::make_shared<std::function<void(std::shared_ptr<Response>, const error_code&)>>(std::move(request_callback_));
|
||||
auto connections=this->connections;
|
||||
auto connections_mutex=this->connections_mutex;
|
||||
session->callback=[connection, response, request_callback, connections_mutex](const error_code &ec) {
|
||||
session->callback=[connection, response, request_callback, connections, connections_mutex](const error_code &ec) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
||||
connection->in_use=false;
|
||||
|
||||
// Remove unused connections, but keep one open for HTTP persistent connection:
|
||||
size_t unused_connections=0;
|
||||
for(auto it=connections->begin();it!=connections->end();) {
|
||||
if((*it)->in_use)
|
||||
++it;
|
||||
else {
|
||||
++unused_connections;
|
||||
if(unused_connections>1)
|
||||
it=connections->erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(*request_callback)
|
||||
|
|
@ -242,10 +272,10 @@ namespace SimpleWeb {
|
|||
std::string host;
|
||||
unsigned short port;
|
||||
|
||||
std::vector<std::shared_ptr<Connection>> connections;
|
||||
std::shared_ptr<std::vector<std::shared_ptr<Connection>>> connections;
|
||||
std::shared_ptr<std::mutex> connections_mutex;
|
||||
|
||||
ClientBase(const std::string& host_port, unsigned short default_port) : io_service(new asio::io_service()), connections_mutex(new std::mutex()) {
|
||||
ClientBase(const std::string& host_port, unsigned short default_port) : io_service(new asio::io_service()), connections(new std::vector<std::shared_ptr<Connection>>()), connections_mutex(new std::mutex()) {
|
||||
auto parsed_host_port=parse_host_port(host_port, default_port);
|
||||
host=parsed_host_port.first;
|
||||
port=parsed_host_port.second;
|
||||
|
|
@ -254,19 +284,15 @@ namespace SimpleWeb {
|
|||
std::shared_ptr<Connection> get_connection() {
|
||||
std::shared_ptr<Connection> connection;
|
||||
std::lock_guard<std::mutex> lock(*connections_mutex);
|
||||
for(auto it=connections.begin();it!=connections.end();) {
|
||||
if((*it)->in_use)
|
||||
++it;
|
||||
else if(!connection) {
|
||||
for(auto it=connections->begin();it!=connections->end();++it) {
|
||||
if(!(*it)->in_use && !connection) {
|
||||
connection=*it;
|
||||
++it;
|
||||
break;
|
||||
}
|
||||
else
|
||||
it=connections.erase(it);
|
||||
}
|
||||
if(!connection) {
|
||||
connection=create_connection();
|
||||
connections.emplace_back(connection);
|
||||
connections->emplace_back(connection);
|
||||
}
|
||||
connection->reconnecting=false;
|
||||
connection->in_use=true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue