Commit 0d8215d1 authored by octal's avatar octal

Http::Endpoint can now be configured through a set of options

parent cf26bf91
......@@ -46,6 +46,10 @@ int main(int argc, char *argv[]) {
cout << "Cores = " << hardware_concurrency() << endl;
Net::Http::Endpoint server(addr);
auto opts = Net::Http::Endpoint::options()
.threads(2)
.flags(Net::Tcp::Options::InstallSignalHandler);
server.init(opts);
server.setHandler(std::make_shared<MyHandler>());
server.serve();
......
......@@ -479,6 +479,28 @@ Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const {
return *peer->getData<Private::Parser>(ParserData);
}
Endpoint::Options::Options()
: threads_(1)
{ }
Endpoint::Options&
Endpoint::Options::threads(int val) {
threads_ = val;
return *this;
}
Endpoint::Options&
Endpoint::Options::flags(Flags<Tcp::Options> flags) {
flags_ = flags;
return *this;
}
Endpoint::Options&
Endpoint::Options::backlog(int val) {
backlog_ = val;
return *this;
}
Endpoint::Endpoint()
{ }
......@@ -486,6 +508,11 @@ Endpoint::Endpoint(const Net::Address& addr)
: listener(addr)
{ }
void
Endpoint::init(const Endpoint::Options& options) {
listener.init(options.threads_, options.flags_);
}
void
Endpoint::setHandler(const std::shared_ptr<Handler>& handler) {
handler_ = handler;
......@@ -497,14 +524,6 @@ Endpoint::serve()
if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()");
listener.init(4, Tcp::Options::InstallSignalHandler);
#if 1
listener.pinWorker(0, CpuSet { 0 } );
listener.pinWorker(1, CpuSet { 1 } );
listener.pinWorker(2, CpuSet { 2 } );
listener.pinWorker(3, CpuSet { 3 } );
#endif
listener.setHandler(handler_);
if (listener.bind()) {
......@@ -514,6 +533,10 @@ Endpoint::serve()
}
}
Endpoint::Options
Endpoint::options() {
return Options();
}
} // namespace Http
......
......@@ -216,12 +216,35 @@ private:
class Endpoint {
public:
struct Options {
friend class Endpoint;
Options& threads(int val);
Options& flags(Flags<Tcp::Options> flags);
Options& backlog(int val);
private:
int threads_;
Flags<Tcp::Options> flags_;
int backlog_;
Options();
};
Endpoint();
Endpoint(const Net::Address& addr);
template<typename... Args>
void initArgs(Args&& ...args) {
listener.init(std::forward<Args>(args)...);
}
void init(const Options& options);
void setHandler(const std::shared_ptr<Handler>& handler);
void serve();
static Options options();
private:
std::shared_ptr<Handler> handler_;
Net::Tcp::Listener listener;
......
......@@ -257,23 +257,26 @@ Handler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) {
Listener::Listener()
: listen_fd(-1)
, backlog_(Const::MaxBacklog)
{ }
Listener::Listener(const Address& address)
: addr_(address)
, listen_fd(-1)
, backlog_(Const::MaxBacklog)
{
}
void
Listener::init(size_t workers, Flags<Options> options)
Listener::init(size_t workers, Flags<Options> options, int backlog)
{
if (workers > hardware_concurrency()) {
// Log::warning() << "More workers than available cores"
}
options_ = options;
backlog_ = backlog;
if (options_.hasFlag(Options::InstallSignalHandler)) {
if (signal(SIGINT, handle_sigint) == SIG_ERR) {
......@@ -354,7 +357,7 @@ Listener::bind(const Address& address) {
continue;
}
TRY(::listen(fd, Const::MaxBacklog));
TRY(::listen(fd, backlog_));
}
......
......@@ -79,7 +79,9 @@ public:
Listener();
Listener(const Address& address);
void init(size_t workers, Flags<Options> options = Options::None);
void init(
size_t workers, Flags<Options> options = Options::None,
int backlog = Const::MaxBacklog);
void setHandler(const std::shared_ptr<Handler>& handler);
bool bind();
......@@ -96,6 +98,7 @@ public:
private:
Address addr_;
int listen_fd;
int backlog_;
std::vector<std::unique_ptr<IoWorker>> ioGroup;
Flags<Options> options_;
std::shared_ptr<Handler> handler_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment