Use adaptive timer instead of idle handler for HTTP server.

This commit is contained in:
XMRig 2018-03-01 09:53:27 +07:00
parent ff6dc727f5
commit 3edc30d067
2 changed files with 33 additions and 9 deletions

View file

@ -32,22 +32,25 @@
Httpd::Httpd(int port, const char *accessToken) : Httpd::Httpd(int port, const char *accessToken) :
m_accessToken(accessToken), m_idle(true),
m_accessToken(accessToken ? strdup(accessToken) : nullptr),
m_port(port), m_port(port),
m_daemon(nullptr) m_daemon(nullptr)
{ {
uv_idle_init(uv_default_loop(), &m_idle); uv_timer_init(uv_default_loop(), &m_timer);
m_idle.data = this; m_timer.data = this;
} }
Httpd::~Httpd() Httpd::~Httpd()
{ {
uv_idle_stop(&m_idle); uv_timer_stop(&m_timer);
if (m_daemon) { if (m_daemon) {
MHD_stop_daemon(m_daemon); MHD_stop_daemon(m_daemon);
} }
delete m_accessToken;
} }
@ -68,7 +71,7 @@ bool Httpd::start()
return false; return false;
} }
uv_idle_start(&m_idle, Httpd::onIdle); uv_timer_start(&m_timer, Httpd::onTimer, kIdleInterval, kIdleInterval);
return true; return true;
} }
@ -92,6 +95,22 @@ int Httpd::auth(const char *header)
} }
void Httpd::run()
{
MHD_run(m_daemon);
const MHD_DaemonInfo *info = MHD_get_daemon_info(m_daemon, MHD_DAEMON_INFO_CURRENT_CONNECTIONS);
if (m_idle && info->num_connections) {
uv_timer_set_repeat(&m_timer, kActiveInterval);
m_idle = false;
}
else if (!m_idle && !info->num_connections) {
uv_timer_set_repeat(&m_timer, kIdleInterval);
m_idle = true;
}
}
int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp) int Httpd::done(MHD_Connection *connection, int status, MHD_Response *rsp)
{ {
if (!rsp) { if (!rsp) {
@ -134,7 +153,7 @@ int Httpd::handler(void *cls, struct MHD_Connection *connection, const char *url
} }
void Httpd::onIdle(uv_idle_t *handle) void Httpd::onTimer(uv_timer_t *handle)
{ {
MHD_run(static_cast<Httpd*>(handle->data)->m_daemon); static_cast<Httpd*>(handle->data)->run();
} }

View file

@ -41,16 +41,21 @@ public:
bool start(); bool start();
private: private:
constexpr static const int kIdleInterval = 200;
constexpr static const int kActiveInterval = 50;
int auth(const char *header); int auth(const char *header);
void run();
static int done(MHD_Connection *connection, int status, MHD_Response *rsp); static int done(MHD_Connection *connection, int status, MHD_Response *rsp);
static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls); static int handler(void *cls, MHD_Connection *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls);
static void onIdle(uv_idle_t *handle); static void onTimer(uv_timer_t *handle);
bool m_idle;
const char *m_accessToken; const char *m_accessToken;
const int m_port; const int m_port;
MHD_Daemon *m_daemon; MHD_Daemon *m_daemon;
uv_idle_t m_idle; uv_timer_t m_timer;
}; };
#endif /* __HTTPD_H__ */ #endif /* __HTTPD_H__ */