Skip to content

Commit

Permalink
add mysql/redis try
Browse files Browse the repository at this point in the history
Signed-off-by: sylar-yin <[email protected]>
  • Loading branch information
sylar-yin committed Dec 10, 2019
1 parent 41ed343 commit ac52eeb
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ set(LIBS
zookeeper_mt
sqlite3
tinyxml2
jemalloc
)
#jemalloc
sylar_add_executable(test_util "tests/test_util.cc" sylar "${LIBS}")
sylar_add_executable(test_hashmultimap "tests/test_hashmultimap.cc" sylar "${LIBS}")
sylar_add_executable(test_hashmap "tests/test_hashmap.cc" sylar "${LIBS}")
Expand Down
106 changes: 103 additions & 3 deletions sylar/db/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static MYSQL* mysql_init(std::map<std::string, std::string>& params,
}
bool close = false;
mysql_options(mysql, MYSQL_OPT_RECONNECT, &close);
mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "UTF8");
mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8mb4");

int port = sylar::GetParamValue(params, "port", 0);
std::string host = sylar::GetParamValue<std::string>(params, "host");
Expand All @@ -89,7 +89,8 @@ static MYSQL* mysql_init(std::map<std::string, std::string>& params,
MySQL::MySQL(const std::map<std::string, std::string>& args)
:m_params(args)
,m_lastUsedTime(0)
,m_hasError(false) {
,m_hasError(false)
,m_poolSize(10) {
}

bool MySQL::connect() {
Expand All @@ -103,6 +104,7 @@ bool MySQL::connect() {
return false;
}
m_hasError = false;
m_poolSize = sylar::GetParamValue(m_params, "pool", 5);
m_mysql.reset(m, mysql_close);
return true;
}
Expand Down Expand Up @@ -1136,12 +1138,110 @@ MySQLTransaction::ptr MySQLManager::openTransaction(const std::string& name, boo
void MySQLManager::freeMySQL(const std::string& name, MySQL* m) {
if(m->m_mysql) {
MutexType::Lock lock(m_mutex);
if(m_conns[name].size() < m_maxConn) {
if(m_conns[name].size() < (size_t)m->m_poolSize) {
m_conns[name].push_back(m);
return;
}
}
delete m;
}

ISQLData::ptr MySQLUtil::Query(const std::string& name, const char* format, ...) {
va_list ap;
va_start(ap, format);
auto rpy = Query(name, format, ap);
va_end(ap);
return rpy;
}

ISQLData::ptr MySQLUtil::Query(const std::string& name, const char* format,va_list ap) {
auto m = MySQLMgr::GetInstance()->get(name);
if(!m) {
return nullptr;
}
return m->query(format, ap);
}

ISQLData::ptr MySQLUtil::Query(const std::string& name, const std::string& sql) {
auto m = MySQLMgr::GetInstance()->get(name);
if(!m) {
return nullptr;
}
return m->query(sql);
}

ISQLData::ptr MySQLUtil::TryQuery(const std::string& name, uint32_t count, const char* format, ...) {
for(uint32_t i = 0; i < count; ++i) {
va_list ap;
va_start(ap, format);
auto rpy = Query(name, format, ap);
va_end(ap);
if(rpy) {
return rpy;
}
}
return nullptr;
}

ISQLData::ptr MySQLUtil::TryQuery(const std::string& name, uint32_t count, const std::string& sql) {
for(uint32_t i = 0; i < count; ++i) {
auto rpy = Query(name, sql);
if(rpy) {
return rpy;
}
}
return nullptr;

}

int MySQLUtil::Execute(const std::string& name, const char* format, ...) {
va_list ap;
va_start(ap, format);
auto rpy = Execute(name, format, ap);
va_end(ap);
return rpy;
}

int MySQLUtil::Execute(const std::string& name, const char* format, va_list ap) {
auto m = MySQLMgr::GetInstance()->get(name);
if(!m) {
return -1;
}
return m->execute(format, ap);
}

int MySQLUtil::Execute(const std::string& name, const std::string& sql) {
auto m = MySQLMgr::GetInstance()->get(name);
if(!m) {
return -1;
}
return m->execute(sql);

}

int MySQLUtil::TryExecute(const std::string& name, uint32_t count, const char* format, ...) {
int rpy = 0;
for(uint32_t i = 0; i < count; ++i) {
va_list ap;
va_start(ap, format);
rpy = Execute(name, format, ap);
va_end(ap);
if(!rpy) {
return rpy;
}
}
return rpy;
}

int MySQLUtil::TryExecute(const std::string& name, uint32_t count, const std::string& sql) {
int rpy = 0;
for(uint32_t i = 0; i < count; ++i) {
rpy = Execute(name, sql);
if(!rpy) {
return rpy;
}
}
return rpy;
}

}
19 changes: 19 additions & 0 deletions sylar/db/mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ friend class MySQLManager;

uint64_t m_lastUsedTime;
bool m_hasError;
int32_t m_poolSize;
};

class MySQLTransaction : public ITransaction {
Expand Down Expand Up @@ -305,6 +306,24 @@ class MySQLManager {
std::map<std::string, std::map<std::string, std::string> > m_dbDefines;
};

class MySQLUtil {
public:
static ISQLData::ptr Query(const std::string& name, const char* format, ...);
static ISQLData::ptr Query(const std::string& name, const char* format,va_list ap);
static ISQLData::ptr Query(const std::string& name, const std::string& sql);

static ISQLData::ptr TryQuery(const std::string& name, uint32_t count, const char* format, ...);
static ISQLData::ptr TryQuery(const std::string& name, uint32_t count, const std::string& sql);

static int Execute(const std::string& name, const char* format, ...);
static int Execute(const std::string& name, const char* format, va_list ap);
static int Execute(const std::string& name, const std::string& sql);

static int TryExecute(const std::string& name, uint32_t count, const char* format, ...);
static int TryExecute(const std::string& name, uint32_t count, const char* format, va_list ap);
static int TryExecute(const std::string& name, uint32_t count, const std::string& sql);
};

typedef sylar::Singleton<MySQLManager> MySQLMgr;

namespace {
Expand Down
36 changes: 31 additions & 5 deletions sylar/db/redis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -935,19 +935,20 @@ bool FoxRedisCluster::pinit() {
}
SYLAR_LOG_INFO(g_logger) << "FoxRedisCluster pinit:" << m_host;
auto ctx = redisClusterAsyncConnect(m_host.c_str(), 0);
ctx->data = this;
redisClusterLibeventAttach(ctx, m_thread->getBase());
redisClusterAsyncSetConnectCallback(ctx, ConnectCb);
redisClusterAsyncSetDisconnectCallback(ctx, DisconnectCb);
if(!ctx) {
SYLAR_LOG_ERROR(g_logger) << "redisClusterAsyncConnect (" << m_host
<< ") null";
return false;
}
if(ctx->err) {
SYLAR_LOG_ERROR(g_logger) << "Error:(" << ctx->err << ")" << ctx->errstr;
SYLAR_LOG_ERROR(g_logger) << "Error:(" << ctx->err << ")" << ctx->errstr
<< " passwd=" << m_passwd;
return false;
}
ctx->data = this;
redisClusterLibeventAttach(ctx, m_thread->getBase());
redisClusterAsyncSetConnectCallback(ctx, ConnectCb);
redisClusterAsyncSetDisconnectCallback(ctx, DisconnectCb);
m_status = CONNECTED;
//m_context.reset(ctx, redisAsyncFree);
m_context.reset(ctx, sylar::nop<redisClusterAsyncContext>);
Expand Down Expand Up @@ -1298,4 +1299,29 @@ ReplyPtr RedisUtil::Cmd(const std::string& name, const std::vector<std::string>&
return rds->cmd(args);
}


ReplyPtr RedisUtil::TryCmd(const std::string& name, uint32_t count, const char* fmt, ...) {
for(uint32_t i = 0; i < count; ++i) {
va_list ap;
va_start(ap, fmt);
ReplyPtr rt = Cmd(name, fmt, ap);
va_end(ap);

if(rt) {
return rt;
}
}
return nullptr;
}

ReplyPtr RedisUtil::TryCmd(const std::string& name, uint32_t count, const std::vector<std::string>& args) {
for(uint32_t i = 0; i < count; ++i) {
ReplyPtr rt = Cmd(name, args);
if(rt) {
return rt;
}
}
return nullptr;
}

}
3 changes: 3 additions & 0 deletions sylar/db/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ class RedisUtil {
static ReplyPtr Cmd(const std::string& name, const char* fmt, ...);
static ReplyPtr Cmd(const std::string& name, const char* fmt, va_list ap);
static ReplyPtr Cmd(const std::string& name, const std::vector<std::string>& args);

static ReplyPtr TryCmd(const std::string& name, uint32_t count, const char* fmt, ...);
static ReplyPtr TryCmd(const std::string& name, uint32_t count, const std::vector<std::string>& args);
};

}
Expand Down

0 comments on commit ac52eeb

Please sign in to comment.