From 0092f741f299c8fd766fcf873d4285fad359fd93 Mon Sep 17 00:00:00 2001 From: XMRig Date: Thu, 29 Mar 2018 14:27:08 +0700 Subject: [PATCH] Added configurable keepalive support, now possible override default timeout (60 seconds) via config file (only). --- CHANGELOG.md | 3 +++ src/core/Config.cpp | 8 ++++++ src/core/ConfigLoader.cpp | 11 +++++++- src/core/ConfigLoader_static.h | 2 ++ src/net/Client.cpp | 48 ++++++++++++---------------------- src/net/Client.h | 6 +---- src/net/Url.cpp | 12 ++++++--- src/net/Url.h | 6 ++++- src/version.h | 4 +-- 9 files changed, 55 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56123d1..1cbd3c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# v2.5.3 +- Added configurable keepalive support, now possible override default timeout (60 seconds) via config file (only). + # v2.5.2 - [#448](https://github.com/xmrig/xmrig/issues/478) Fixed broken reconnect. diff --git a/src/core/Config.cpp b/src/core/Config.cpp index ab686a3..8ce595d 100644 --- a/src/core/Config.cpp +++ b/src/core/Config.cpp @@ -210,6 +210,14 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) obj.AddMember("user", rapidjson::StringRef(url->user()), allocator); obj.AddMember("pass", rapidjson::StringRef(url->password()), allocator); obj.AddMember("coin", rapidjson::StringRef(url->coin()), allocator); + + if (url->keepAlive() == 0 || url->keepAlive() == Url::kKeepAliveTimeout) { + obj.AddMember("keepalive", url->keepAlive() > 0, allocator); + } + else { + obj.AddMember("keepalive", url->keepAlive(), allocator); + } + obj.AddMember("variant", url->variant(), allocator); pools.PushBack(obj, allocator); diff --git a/src/core/ConfigLoader.cpp b/src/core/ConfigLoader.cpp index 58e6ba3..6ed4092 100644 --- a/src/core/ConfigLoader.cpp +++ b/src/core/ConfigLoader.cpp @@ -316,6 +316,7 @@ bool xmrig::ConfigLoader::parseArg(xmrig::Config *config, int key, const char *a case 'B': /* --background */ case 'S': /* --syslog */ + case 'k': /* --keepalive */ case 1100: /* --verbose */ case 1101: /* --debug */ return parseBoolean(config, key, true); @@ -387,6 +388,10 @@ bool xmrig::ConfigLoader::parseArg(xmrig::Config *config, int key, uint64_t arg) config->m_retryPause = (int) arg; break; + case 'k': /* --keepalive */ + config->m_pools.back()->setKeepAlive(static_cast(arg)); + break; + case 1003: /* --donate-level */ if ((int) arg < 0 || arg > 99) { return true; @@ -434,6 +439,10 @@ bool xmrig::ConfigLoader::parseBoolean(xmrig::Config *config, int key, bool enab config->m_syslog = enable; break; + case 'k': /* --keepalive */ + config->m_pools.back()->setKeepAlive(enable ? Url::kKeepAliveTimeout : 0); + break; + case 1002: /* --no-color */ config->m_colors = enable; break; @@ -486,7 +495,7 @@ void xmrig::ConfigLoader::parseJSON(xmrig::Config *config, const struct option * else if (option->has_arg && value.IsInt64()) { parseArg(config, option->val, value.GetUint64()); } - else if (!option->has_arg && value.IsBool()) { + else if ((option->has_arg == 0 || option->has_arg == 2) && value.IsBool()) { parseBoolean(config, option->val, value.IsTrue()); } } diff --git a/src/core/ConfigLoader_static.h b/src/core/ConfigLoader_static.h index d96f9a9..613ad28 100644 --- a/src/core/ConfigLoader_static.h +++ b/src/core/ConfigLoader_static.h @@ -54,6 +54,7 @@ Options:\n\ -O, --userpass=U:P username:password pair for mining server\n\ -u, --user=USERNAME username for mining server\n\ -p, --pass=PASSWORD password for mining server\n\ + -k, --keepalive prevent timeout (need pool support)\n\ -r, --retries=N number of times to retry before switch to backup server (default: 1)\n\ -R, --retry-pause=N time to pause between retries (default: 1 second)\n\ --custom-diff=N override pool diff\n\ @@ -155,6 +156,7 @@ static struct option const pool_options[] = { { "user", 1, nullptr, 'u' }, { "userpass", 1, nullptr, 'O' }, { "coin", 1, nullptr, 'C' }, + { "keepalive", 2, nullptr ,'k' }, { "variant", 1, nullptr, 1010 }, { 0, 0, 0, 0 } }; diff --git a/src/net/Client.cpp b/src/net/Client.cpp index ef71174..ec9dc86 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -68,6 +68,7 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_state(UnconnectedState), m_expire(0), m_jobs(0), + m_keepAlive(0), m_key(0), m_stream(nullptr), m_socket(nullptr) @@ -85,11 +86,6 @@ Client::Client(int id, const char *agent, IClientListener *listener) : m_recvBuf.base = m_buf; m_recvBuf.len = sizeof(m_buf); - -# ifndef XMRIG_PROXY_PROJECT - m_keepAliveTimer.data = m_storage.ptr(m_key); - uv_timer_init(uv_default_loop(), &m_keepAliveTimer); -# endif } @@ -148,17 +144,17 @@ void Client::setUrl(const Url *url) void Client::tick(uint64_t now) { - if (m_expire == 0 || now < m_expire) { - return; - } - if (m_state == ConnectedState) { - LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); - close(); + if (m_expire && now > m_expire) { + LOG_DEBUG_ERR("[%s:%u] timeout", m_url.host(), m_url.port()); + close(); + } + else if (m_keepAlive && now > m_keepAlive) { + ping(); + } } - - if (m_state == ConnectingState) { + if (m_expire && now > m_expire && m_state == ConnectingState) { connect(); } } @@ -166,12 +162,9 @@ void Client::tick(uint64_t now) bool Client::disconnect() { -# ifndef XMRIG_PROXY_PROJECT - uv_timer_stop(&m_keepAliveTimer); -# endif - - m_expire = 0; - m_failures = -1; + m_keepAlive = 0; + m_expire = 0; + m_failures = -1; return close(); } @@ -576,7 +569,7 @@ void Client::parseResponse(int64_t id, const rapidjson::Value &result, const rap LOG_ERR("[%s:%u] error: \"%s\", code: %d", m_url.host(), m_url.port(), message, error["code"].GetInt()); } - if (id == 1 || isCriticalError(message)) { + if (isCriticalError(message)) { close(); } @@ -628,12 +621,7 @@ void Client::reconnect() } setState(ConnectingState); - -# ifndef XMRIG_PROXY_PROJECT - if (m_url.isKeepAlive()) { - uv_timer_stop(&m_keepAliveTimer); - } -# endif + m_keepAlive = 0; if (m_failures == -1) { return m_listener->onClose(this, -1); @@ -662,13 +650,9 @@ void Client::startTimeout() { m_expire = 0; -# ifndef XMRIG_PROXY_PROJECT - if (!m_url.isKeepAlive()) { - return; + if (m_url.keepAlive()) { + m_keepAlive = uv_now(uv_default_loop()) + (m_url.keepAlive() * 1000); } - - uv_timer_start(&m_keepAliveTimer, [](uv_timer_t *handle) { getClient(handle->data)->ping(); }, kKeepAliveTimeout, 0); -# endif } diff --git a/src/net/Client.h b/src/net/Client.h index dc320f8..fc0335c 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -54,7 +54,6 @@ public: }; constexpr static int kResponseTimeout = 20 * 1000; - constexpr static int kKeepAliveTimeout = 60 * 1000; Client(int id, const char *agent, IClientListener *listener); ~Client(); @@ -123,6 +122,7 @@ private: std::map m_results; uint64_t m_expire; uint64_t m_jobs; + uint64_t m_keepAlive; uintptr_t m_key; Url m_url; uv_buf_t m_recvBuf; @@ -133,10 +133,6 @@ private: static int64_t m_sequence; static xmrig::Storage m_storage; - -# ifndef XMRIG_PROXY_PROJECT - uv_timer_t m_keepAliveTimer; -# endif }; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 464114b..32faa82 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -43,6 +43,7 @@ Url::Url() : m_password(nullptr), m_user(nullptr), m_coin(), + m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) @@ -66,6 +67,7 @@ Url::Url(const char *url) : m_password(nullptr), m_user(nullptr), m_coin(), + m_keepAlive(0), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(kDefaultPort) @@ -74,10 +76,11 @@ Url::Url(const char *url) : } -Url::Url(const char *host, uint16_t port, const char *user, const char *password) : +Url::Url(const char *host, uint16_t port, const char *user, const char *password, int keepAlive) : m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), m_coin(), + m_keepAlive(keepAlive), m_variant(xmrig::VARIANT_AUTO), m_url(nullptr), m_port(port) @@ -237,7 +240,7 @@ void Url::setVariant(int variant) bool Url::operator==(const Url &other) const { - if (m_port != other.m_port || m_variant != other.m_variant) { + if (m_port != other.m_port || m_variant != other.m_variant || m_keepAlive != other.m_keepAlive) { return false; } @@ -251,8 +254,9 @@ bool Url::operator==(const Url &other) const Url &Url::operator=(const Url *other) { - m_port = other->m_port; - m_variant = other->m_variant; + m_port = other->m_port; + m_variant = other->m_variant; + m_keepAlive = other->m_keepAlive; free(m_host); m_host = strdup(other->m_host); diff --git a/src/net/Url.h b/src/net/Url.h index 9485b63..0ca2ace 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -34,10 +34,11 @@ public: constexpr static const char *kDefaultPassword = "x"; constexpr static const char *kDefaultUser = "x"; constexpr static uint16_t kDefaultPort = 3333; + constexpr static int kKeepAliveTimeout = 60; Url(); Url(const char *url); - Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr); + Url(const char *host, uint16_t port, const char *user = nullptr, const char *password = nullptr, int keepAlive = 0); ~Url(); inline bool isValid() const { return m_host && m_port > 0; } @@ -45,8 +46,10 @@ public: inline const char *host() const { return m_host; } inline const char *password() const { return m_password ? m_password : kDefaultPassword; } inline const char *user() const { return m_user ? m_user : kDefaultUser; } + inline int keepAlive() const { return m_keepAlive; } inline int variant() const { return m_variant; } inline uint16_t port() const { return m_port; } + inline void setKeepAlive(int keepAlive) { m_keepAlive = keepAlive >= 0 ? keepAlive : 0; } bool parse(const char *url); bool setUserpass(const char *userpass); @@ -67,6 +70,7 @@ private: char *m_password; char *m_user; char m_coin[5]; + int m_keepAlive; int m_variant; mutable char *m_url; uint16_t m_port; diff --git a/src/version.h b/src/version.h index ec1ae43..b02267e 100644 --- a/src/version.h +++ b/src/version.h @@ -27,7 +27,7 @@ #define APP_ID "xmrig-proxy" #define APP_NAME "xmrig-proxy" #define APP_DESC "XMRig Stratum proxy" -#define APP_VERSION "2.5.2" +#define APP_VERSION "2.5.3-dev" #define APP_DOMAIN "xmrig.com" #define APP_SITE "www.xmrig.com" #define APP_COPYRIGHT "Copyright (C) 2016-2018 xmrig.com" @@ -35,7 +35,7 @@ #define APP_VER_MAJOR 2 #define APP_VER_MINOR 5 -#define APP_VER_BUILD 2 +#define APP_VER_BUILD 3 #define APP_VER_REV 0 #ifdef _MSC_VER