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[]) { ...@@ -46,6 +46,10 @@ int main(int argc, char *argv[]) {
cout << "Cores = " << hardware_concurrency() << endl; cout << "Cores = " << hardware_concurrency() << endl;
Net::Http::Endpoint server(addr); 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.setHandler(std::make_shared<MyHandler>());
server.serve(); server.serve();
......
...@@ -479,6 +479,28 @@ Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const { ...@@ -479,6 +479,28 @@ Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const {
return *peer->getData<Private::Parser>(ParserData); 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() Endpoint::Endpoint()
{ } { }
...@@ -486,6 +508,11 @@ Endpoint::Endpoint(const Net::Address& addr) ...@@ -486,6 +508,11 @@ Endpoint::Endpoint(const Net::Address& addr)
: listener(addr) : listener(addr)
{ } { }
void
Endpoint::init(const Endpoint::Options& options) {
listener.init(options.threads_, options.flags_);
}
void void
Endpoint::setHandler(const std::shared_ptr<Handler>& handler) { Endpoint::setHandler(const std::shared_ptr<Handler>& handler) {
handler_ = handler; handler_ = handler;
...@@ -497,14 +524,6 @@ Endpoint::serve() ...@@ -497,14 +524,6 @@ Endpoint::serve()
if (!handler_) if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()"); 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_); listener.setHandler(handler_);
if (listener.bind()) { if (listener.bind()) {
...@@ -514,6 +533,10 @@ Endpoint::serve() ...@@ -514,6 +533,10 @@ Endpoint::serve()
} }
} }
Endpoint::Options
Endpoint::options() {
return Options();
}
} // namespace Http } // namespace Http
......
...@@ -216,12 +216,35 @@ private: ...@@ -216,12 +216,35 @@ private:
class Endpoint { class Endpoint {
public: 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();
Endpoint(const Net::Address& addr); 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 setHandler(const std::shared_ptr<Handler>& handler);
void serve(); void serve();
static Options options();
private: private:
std::shared_ptr<Handler> handler_; std::shared_ptr<Handler> handler_;
Net::Tcp::Listener listener; Net::Tcp::Listener listener;
......
...@@ -257,23 +257,26 @@ Handler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) { ...@@ -257,23 +257,26 @@ Handler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) {
Listener::Listener() Listener::Listener()
: listen_fd(-1) : listen_fd(-1)
, backlog_(Const::MaxBacklog)
{ } { }
Listener::Listener(const Address& address) Listener::Listener(const Address& address)
: addr_(address) : addr_(address)
, listen_fd(-1) , listen_fd(-1)
, backlog_(Const::MaxBacklog)
{ {
} }
void void
Listener::init(size_t workers, Flags<Options> options) Listener::init(size_t workers, Flags<Options> options, int backlog)
{ {
if (workers > hardware_concurrency()) { if (workers > hardware_concurrency()) {
// Log::warning() << "More workers than available cores" // Log::warning() << "More workers than available cores"
} }
options_ = options; options_ = options;
backlog_ = backlog;
if (options_.hasFlag(Options::InstallSignalHandler)) { if (options_.hasFlag(Options::InstallSignalHandler)) {
if (signal(SIGINT, handle_sigint) == SIG_ERR) { if (signal(SIGINT, handle_sigint) == SIG_ERR) {
...@@ -354,7 +357,7 @@ Listener::bind(const Address& address) { ...@@ -354,7 +357,7 @@ Listener::bind(const Address& address) {
continue; continue;
} }
TRY(::listen(fd, Const::MaxBacklog)); TRY(::listen(fd, backlog_));
} }
......
...@@ -79,7 +79,9 @@ public: ...@@ -79,7 +79,9 @@ public:
Listener(); Listener();
Listener(const Address& address); 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); void setHandler(const std::shared_ptr<Handler>& handler);
bool bind(); bool bind();
...@@ -96,6 +98,7 @@ public: ...@@ -96,6 +98,7 @@ public:
private: private:
Address addr_; Address addr_;
int listen_fd; int listen_fd;
int backlog_;
std::vector<std::unique_ptr<IoWorker>> ioGroup; std::vector<std::unique_ptr<IoWorker>> ioGroup;
Flags<Options> options_; Flags<Options> options_;
std::shared_ptr<Handler> handler_; 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