Skip to content

Commit

Permalink
Updated thread joinlist logic and some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maddsua committed Feb 16, 2024
1 parent 7f4f102 commit 15ac315
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
22 changes: 17 additions & 5 deletions core/server/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ using namespace Lambda::HTTP::Transport;
using namespace std::chrono_literals;

LambdaInstance::LambdaInstance(RequestCallback handlerCallback, ServerConfig init) :
listener({ init.service.fastPortReuse, init.service.port, init.service.connectionTimeouts }),
config(init), httpHandler(handlerCallback) {

if (this->config.service.fastPortReuse) {
listener({
init.service.fastPortReuse,
init.service.port,
init.service.connectionTimeouts
}),
config(init),
httpHandler(handlerCallback)
{

if (init.service.fastPortReuse) {
syncout.log("[Service] Warning: fast port reuse enabled");
}

if (init.service.maxConnections < ServiceOptions::minConnections) {
throw Lambda::Error("ServiceOptions::maxConnections value is lower than allowed by minConnections");
}

this->serviceWorker = std::async([&]() {

const auto workerJoinFilter = [&](WorkerContext& node) -> bool {
Expand All @@ -37,6 +47,8 @@ LambdaInstance::LambdaInstance(RequestCallback handlerCallback, ServerConfig ini
};

const auto& svcmaxconn = this->config.service.maxConnections;
const auto gcThreshold = svcmaxconn * 0.75;

while (!this->m_terminated && this->listener.active()) {

auto nextConn = this->listener.acceptConnection();
Expand All @@ -59,7 +71,7 @@ LambdaInstance::LambdaInstance(RequestCallback handlerCallback, ServerConfig ini
worker.finished = true;
}, std::ref(nextWorker));

if (!svcmaxconn || this->m_connections_count > svcmaxconn) {
if (!svcmaxconn || this->m_connections_count > gcThreshold) {
this->m_connections.remove_if(workerJoinFilter);
}
}
Expand Down
66 changes: 64 additions & 2 deletions core/server/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,92 @@

namespace Lambda {

/**
* Server log options
*/
struct LogOptions {
/**
* Log events like connections and transport creations/destructions
*/
bool transportEvents = false;
/**
* Log http requests
*/
bool requests = true;
/**
* Display server start message
*/
bool startMessage = true;
};

/**
* What type of error page to use
*/
enum struct ErrorResponseType {
Plain, HTML, JSON
/**
* Return a plaintext error message
*/
Plain,
/**
* Display a nice html page with an error message
*/
HTML,
/**
* Return error message as a JSON object
*/
JSON
};

/**
* Web server options
*/
struct ServeOptions {
/**
* Logging options
*/
LogOptions loglevel;
/**
* HTTP transport options
*/
HTTP::Transport::TransportOptions transport;
/**
* Choose error page type
*/
ErrorResponseType errorResponseType = ErrorResponseType::HTML;
};

/**
* Service settings (sockets stuff and such)
*/
struct ServiceOptions {
/**
* Service port
*/
uint16_t port = 8180;
/**
* Allow fast port reuse
*/
bool fastPortReuse = false;
/**
* Set default connection timeout
*/
Network::ConnectionTimeouts connectionTimeouts;
uint32_t maxConnections = 100;
/**
* Max number of simultaneous connections
*
* 0 = unlimited, but with the current joinlist implementation
* can cause slowdowns when a lot of connections are created at the same time
*
* Note that 50 is a minimal allowed value
*/
uint32_t maxConnections = 500;
static const uint32_t minConnections = 50;
};

struct ServerConfig : ServeOptions {
/**
* Network options
*/
ServiceOptions service;
};
};
Expand Down
3 changes: 3 additions & 0 deletions core/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

namespace Lambda {

/**
* Lambda server class (this is what you want)
*/
class LambdaInstance {
private:
Network::TCP::ListenSocket listener;
Expand Down

0 comments on commit 15ac315

Please sign in to comment.