mirror of
https://gitee.com/TarsCloud/TarsCpp.git
synced 2025-01-05 17:42:24 +08:00
Merge branch 'master' of https://github.com/TarsCloud/TarsCpp into fix_hash_conhash_weight_bug
This commit is contained in:
commit
b80fd9d789
@ -1319,7 +1319,10 @@ void EndpointManager::updateConHashProxyWeighted(bool bStatic, vector<AdapterPro
|
||||
{
|
||||
iWeight = 1;
|
||||
}
|
||||
conHash.addNode(_vRegProxys[i]->endpoint().desc(), i, iWeight);
|
||||
// 同一服务有多个obj的情况
|
||||
// 同一hash值调用不同的obj会hash到不同的服务器
|
||||
// 因为addNode会根据desc(ip+port)计算md5,导致顺序不一致
|
||||
conHash.addNode(_vRegProxys[i]->endpoint().host(), i, iWeight);
|
||||
}
|
||||
//防止多个服务节点权重同时更新时一致性哈希环多次更新
|
||||
_vRegProxys[i]->resetWeightChanged();
|
||||
|
@ -94,6 +94,18 @@ struct TC_DBConf
|
||||
*/
|
||||
int _flag;
|
||||
|
||||
/**
|
||||
* 连接超时
|
||||
* Port
|
||||
*/
|
||||
int _connectTimeout;
|
||||
|
||||
/**
|
||||
* 读写超时
|
||||
* Port
|
||||
*/
|
||||
int _writeReadTimeout;
|
||||
|
||||
/**
|
||||
* @brief 构造函数
|
||||
* @brief Constructor
|
||||
@ -125,18 +137,30 @@ struct TC_DBConf
|
||||
{
|
||||
map<string, string> mpTmp = mpParam;
|
||||
|
||||
_host = mpTmp["dbhost"];
|
||||
_user = mpTmp["dbuser"];
|
||||
_password = mpTmp["dbpass"];
|
||||
_database = mpTmp["dbname"];
|
||||
_charset = mpTmp["charset"];
|
||||
_port = atoi(mpTmp["dbport"].c_str());
|
||||
_flag = 0;
|
||||
_host = mpTmp["dbhost"];
|
||||
_user = mpTmp["dbuser"];
|
||||
_password = mpTmp["dbpass"];
|
||||
_database = mpTmp["dbname"];
|
||||
_charset = mpTmp["charset"];
|
||||
_port = atoi(mpTmp["dbport"].c_str());
|
||||
_flag = 0;
|
||||
_connectTimeout = atoi(mpTmp["connectTimeout"].c_str());
|
||||
_writeReadTimeout = atoi(mpTmp["writeReadTimeout"].c_str());
|
||||
|
||||
if(mpTmp["dbport"] == "")
|
||||
{
|
||||
_port = 3306;
|
||||
}
|
||||
|
||||
if(mpTmp["connectTimeout"] == "")
|
||||
{
|
||||
_connectTimeout = 5;
|
||||
}
|
||||
|
||||
if(mpTmp["writeReadTimeout"] == "")
|
||||
{
|
||||
_writeReadTimeout = 15;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -82,7 +82,21 @@ public:
|
||||
|
||||
static void setEnv(const std::string &name, const std::string &value);
|
||||
|
||||
static std::string exec(const char* cmd);
|
||||
/**
|
||||
* exec command
|
||||
* @param cmd
|
||||
* @return string
|
||||
*/
|
||||
static std::string exec(const char* cmd);
|
||||
|
||||
/**
|
||||
* exec command
|
||||
*
|
||||
* @param cmd
|
||||
* @param errstr, if error, get error message
|
||||
* @return string
|
||||
*/
|
||||
static std::string exec(const char* cmd, std::string &errstr);
|
||||
|
||||
static void registerCtrlC(std::function<void()> callback);
|
||||
|
||||
@ -90,15 +104,12 @@ public:
|
||||
|
||||
protected:
|
||||
|
||||
// static void registerCtrlC();
|
||||
|
||||
static void registerSig(int sig, std::function<void()> callback);
|
||||
|
||||
static void registerSig(int sig);
|
||||
|
||||
static std::mutex _mutex;
|
||||
|
||||
// static vector<std::function<void()>> _callbacks;
|
||||
static unordered_map<int, vector<std::function<void()>>> _callbacks;
|
||||
|
||||
#if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
|
||||
|
@ -245,7 +245,7 @@ int TC_BitMap::set(size_t i, unsigned iBit)
|
||||
|
||||
if(iBit > _bitmaps.size())
|
||||
{
|
||||
throw TC_BitMap_Exception("[TC_BitMap::get] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
throw TC_BitMap_Exception("[TC_BitMap::set] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
}
|
||||
|
||||
return _bitmaps[iBit-1].set(i);
|
||||
@ -257,7 +257,7 @@ int TC_BitMap::clear(size_t i, unsigned iBit)
|
||||
|
||||
if(iBit > _bitmaps.size())
|
||||
{
|
||||
throw TC_BitMap_Exception("[TC_BitMap::get] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
throw TC_BitMap_Exception("[TC_BitMap::clear] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
}
|
||||
|
||||
return _bitmaps[iBit-1].clear(i);
|
||||
@ -269,7 +269,7 @@ int TC_BitMap::clear4all(unsigned iBit)
|
||||
|
||||
if (iBit != (unsigned)(-1) && iBit > _bitmaps.size())
|
||||
{
|
||||
throw TC_BitMap_Exception("[TC_BitMap::get] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
throw TC_BitMap_Exception("[TC_BitMap::clear4all] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
|
||||
}
|
||||
|
||||
for (vector<BitMap>::size_type i = 0; i < _bitmaps.size(); i++)
|
||||
|
@ -95,6 +95,26 @@ void TC_Mysql::connect()
|
||||
}
|
||||
|
||||
|
||||
//设置连接超时
|
||||
if(_dbConf._connectTimeout > 0) {
|
||||
if (mysql_options(_pstMql, MYSQL_OPT_CONNECT_TIMEOUT, &_dbConf._connectTimeout)) {
|
||||
throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_CONNECT_TIMEOUT ") + TC_Common::tostr(_dbConf._connectTimeout) + ":" + string(mysql_error(_pstMql)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(_dbConf._writeReadTimeout > 0) {
|
||||
//设置读超时
|
||||
if (mysql_options(_pstMql, MYSQL_OPT_READ_TIMEOUT, &_dbConf._writeReadTimeout)) {
|
||||
throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_READ_TIMEOUT ") + TC_Common::tostr(_dbConf._writeReadTimeout) + ":" + string(mysql_error(_pstMql)));
|
||||
}
|
||||
//设置写超时
|
||||
if (mysql_options(_pstMql, MYSQL_OPT_WRITE_TIMEOUT, &_dbConf._writeReadTimeout)) {
|
||||
throw TC_Mysql_Exception(string("TC_Mysql::connect: mysql_options MYSQL_OPT_WRITE_TIMEOUT ") + TC_Common::tostr(_dbConf._writeReadTimeout) + ":" + string(mysql_error(_pstMql)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (mysql_real_connect(_pstMql, _dbConf._host.c_str(), _dbConf._user.c_str(), _dbConf._password.c_str(), _dbConf._database.c_str(), _dbConf._port, NULL, _dbConf._flag) == NULL)
|
||||
{
|
||||
throw TC_Mysql_Exception("[TC_Mysql::connect]: mysql_real_connect: " + string(mysql_error(_pstMql)));
|
||||
|
@ -238,7 +238,14 @@ void TC_Port::setEnv(const string &name, const string &value)
|
||||
#endif
|
||||
}
|
||||
|
||||
string TC_Port::exec(const char *cmd)
|
||||
std::string TC_Port::exec(const char* cmd)
|
||||
{
|
||||
string err;
|
||||
|
||||
return TC_Port::exec(cmd, err);
|
||||
}
|
||||
|
||||
string TC_Port::exec(const char *cmd, std::string &errstr)
|
||||
{
|
||||
string fileData;
|
||||
#if TARGET_PLATFORM_WINDOWS
|
||||
@ -246,6 +253,10 @@ string TC_Port::exec(const char *cmd)
|
||||
#else
|
||||
FILE* fp = popen(cmd, "r");
|
||||
#endif
|
||||
if (fp == NULL) {
|
||||
errstr = "popen '" + string(cmd) + "' error.";
|
||||
return fileData;
|
||||
}
|
||||
static size_t buf_len = 2 * 1024 * 1024;
|
||||
char *buf = new char[buf_len];
|
||||
memset(buf, 0, buf_len);
|
||||
|
Loading…
Reference in New Issue
Block a user