Skip to content

Commit

Permalink
after the connection with redis-server is created, the dbnum can be s…
Browse files Browse the repository at this point in the history
…elected in acl::redis_client, acl::redis_client_pool.
  • Loading branch information
zhengshuxin authored and zhengshuxin committed Oct 20, 2018
1 parent 1ec33dd commit 7b34362
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 30 deletions.
3 changes: 3 additions & 0 deletions lib_acl_cpp/changes.txt
@@ -1,5 +1,8 @@
�޸���ʷ�б���

545) 2018.10.21
545.1) feature: redis_client, redis_client_pool �ڷǼ�Ⱥģʽ�´�������ʱ�Զ�ѡ��db

544) 2018.10.20
544.1) bugfix: websocket::read_frame_data ��ѭ������ʱ������ʹ�÷�ʽ����
544.2) bugfix: WebSocketServlet.cpp ���ж����Ա����δ��ʼ��
Expand Down
51 changes: 36 additions & 15 deletions lib_acl_cpp/include/acl_cpp/redis/redis_client.hpp
Expand Up @@ -13,9 +13,9 @@ class redis_result;
class redis_request;

/**
* redis 客户端对象网络通信类,通过此类将组织好的 redis 请求命令发给 redis 服务端,
* 同时接收 redis 服务端响应结果;该类继承于 connect_client 类,主要为了使用连接池
* 功能
* redis 客户端对象网络通信类,通过此类将组织好的 redis 请求命令发给 redis
* 服务端,同时接收 redis 服务端响应结果;该类继承于 connect_client 类,主要
* 为了使用连接池功能
* redis client network IO class. The redis request is sent to server
* and the server's respond is handled in this class. The class inherits
* connect_client, which can use the connection pool function.
Expand Down Expand Up @@ -44,6 +44,25 @@ class ACL_CPP_API redis_client : public connect_client
*/
void set_password(const char* pass);

/**
* 设置本连接所对应的 db,当连接建立后如果指定的 db 值大于 0,则内部自动
* 选择对应的 db,注意:该功能仅针对非集群模式
* if db > 0 in no cluster mode, select the db when the connection
* is created.
* @param dbnum {int}
*/
void set_db(int dbnum);

/**
* 获得本连接所选择的 db
* get db for the connection
* @return {int}
*/
int get_db(void) const
{
return dbnum_;
}

/**
* 获得当前连接的服务器地址,即由 redis_client 构造时传入的地址
* @return {const char*}
Expand All @@ -66,21 +85,21 @@ class ACL_CPP_API redis_client : public connect_client
* check if the connection has been finish
* @return {bool}
*/
bool eof() const;
bool eof(void) const;

/**
* 关闭网络连接
* close the connection to the redis-server
*/
void close();
void close(void);

/**
* 获得网络连接流
* get acl::socket_stream from the connection
* @return {acl::socket_stream*} 如果连接已经关闭则返回 NULL
* NULL will be returned if the connectioin has been closed
*/
socket_stream* get_stream();
socket_stream* get_stream(void);

/**
* 对于请求数据包,此函数设置在组装请求数据包的时候合成一个数据包发送
Expand Down Expand Up @@ -137,18 +156,20 @@ class ACL_CPP_API redis_client : public connect_client

protected:
// 基类虚函数
virtual bool open();
// @override
virtual bool open(void);

private:
socket_stream conn_;
bool check_addr_;
char* addr_;
char* pass_;
bool retry_;
bool authing_;
string buf_;
bool slice_req_;
bool slice_res_;
bool check_addr_;
char* addr_;
char* pass_;
bool retry_;
bool authing_;
string buf_;
bool slice_req_;
bool slice_res_;
int dbnum_;

redis_result* get_redis_objects(dbuf_pool* pool, size_t nobjs);
redis_result* get_redis_object(dbuf_pool* pool);
Expand Down
22 changes: 21 additions & 1 deletion lib_acl_cpp/include/acl_cpp/redis/redis_client_pool.hpp
Expand Up @@ -37,16 +37,36 @@ class ACL_CPP_API redis_client_pool : public connect_pool
*/
redis_client_pool& set_password(const char* pass);

/**
* 在非集群模式下,本方法用来设置连接建立后所选择的db
* in no-cluster mode, the method is used to select the db after
* the connection is created
* @param dbnum {int}
* @return {redis_client_pool&}
*/
redis_client_pool& set_db(int dbnum);

/**
* 获得本连接池所对应的db
* get the current db of the connections pool
* @return {int}
*/
int get_db(void) const
{
return dbnum_;
}

protected:
/**
* 基类纯虚函数: 调用此函数用来创建一个新的连接
* virtual function in class connect_pool to create a new connection
* @return {connect_client*}
*/
connect_client* create_connect();
connect_client* create_connect(void);

private:
char* pass_;
int dbnum_;
};

} // namespace acl
12 changes: 10 additions & 2 deletions lib_acl_cpp/samples/redis/redis/redis.cpp
Expand Up @@ -161,6 +161,7 @@ static void usage(const char* procname)
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
"-p password[default: \"\"]\r\n"
"-d dbnum[default: 0]\r\n"
"-n count\r\n"
"-C connect_timeout[default: 10]\r\n"
"-T rw_timeout[default: 10]\r\n"
Expand All @@ -170,10 +171,10 @@ static void usage(const char* procname)

int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
int ch, n = 1, conn_timeout = 10, rw_timeout = 10, dbnum = 0;
acl::string addr("127.0.0.1:6379"), command, passwd;

while ((ch = getopt(argc, argv, "hs:n:C:T:a:p:")) > 0)
while ((ch = getopt(argc, argv, "hs:n:C:T:a:p:d:")) > 0)
{
switch (ch)
{
Expand All @@ -198,15 +199,22 @@ int main(int argc, char* argv[])
case 'p':
passwd = optarg;
break;
case 'd':
dbnum = atoi(optarg);
break;
default:
break;
}
}

acl::acl_cpp_init();
acl::log::stdout_open(true);

acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
client.set_password(passwd);
if (dbnum > 0)
client.set_db(dbnum);

acl::redis cmd(&client);

bool ret;
Expand Down
40 changes: 36 additions & 4 deletions lib_acl_cpp/samples/redis/redis_pool/redis_pool.cpp
Expand Up @@ -2,6 +2,24 @@

static acl::string __keypre("test_key");

static bool test_set(acl::redis_client* conn, int i)
{
acl::redis_string cmd(conn);
acl::string key, val;

key.format("%s_%d", __keypre.c_str(), i);
val.format("val_%d", i);
if (cmd.set(key, val) == false)
{
printf("set key: %s val: %s error: %s\r\n", key.c_str(),
val.c_str(), cmd.result_error());
return false;
}
else if (i < 10)
printf("set ok, key=%s, value=%s\r\n", key.c_str(), val.c_str());
return true;
}

static bool test_del(acl::redis_key& redis, int i)
{
acl::string key;
Expand Down Expand Up @@ -115,7 +133,9 @@ class test_thread : public acl::thread

redis.set_client(conn);

if (cmd_ == "del")
if (cmd_ == "set")
ret = test_set(conn, i);
else if (cmd_ == "del")
ret = test_del(redis, i);
else if (cmd_ == "expire")
ret = test_expire(redis, i);
Expand Down Expand Up @@ -161,6 +181,8 @@ static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
"-p password[default: '']\r\n"
"-d dbnum[default: 0]\r\n"
"-n count[default: 10]\r\n"
"-C connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 10]\r\n"
Expand All @@ -171,11 +193,11 @@ static void usage(const char* procname)

int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
int ch, n = 1, conn_timeout = 10, rw_timeout = 10, dbnum = 0;
int max_threads = 10;
acl::string addr("127.0.0.1:6379"), cmd;
acl::string addr("127.0.0.1:6379"), cmd, passwd;

while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:")) > 0)
while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:p:d:")) > 0)
{
switch (ch)
{
Expand All @@ -185,6 +207,12 @@ int main(int argc, char* argv[])
case 's':
addr = optarg;
break;
case 'p':
passwd = optarg;
break;
case 'd':
dbnum = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
Expand All @@ -209,6 +237,10 @@ int main(int argc, char* argv[])

acl::redis_client_pool pool(addr.c_str(), max_threads);
pool.set_timeout(conn_timeout, rw_timeout);
if (!passwd.empty())
pool.set_password(passwd);
if (dbnum > 0)
pool.set_db(dbnum);

std::vector<test_thread*> threads;
for (int i = 0; i < max_threads; i++)
Expand Down
30 changes: 24 additions & 6 deletions lib_acl_cpp/src/redis/redis_client.cpp
Expand Up @@ -23,13 +23,14 @@ redis_client::redis_client(const char* addr, int conn_timeout /* = 60 */,
, authing_(false)
, slice_req_(false)
, slice_res_(false)
, dbnum_(0)
{
addr_ = acl_mystrdup(addr);
pass_ = NULL;
set_timeout(conn_timeout, rw_timeout);
}

redis_client::~redis_client()
redis_client::~redis_client(void)
{
acl_myfree(addr_);
if (pass_)
Expand All @@ -54,7 +55,13 @@ void redis_client::set_password(const char* pass)
pass_ = NULL;
}

socket_stream* redis_client::get_stream()
void redis_client::set_db(int dbnum)
{
if (dbnum > 0)
dbnum_ = dbnum;
}

socket_stream* redis_client::get_stream(void)
{
if (conn_.opened())
return (socket_stream*) &conn_;
Expand Down Expand Up @@ -86,7 +93,7 @@ bool redis_client::check_connection(socket_stream& conn)
return true;
}

bool redis_client::open()
bool redis_client::open(void)
{
if (conn_.opened())
return true;
Expand All @@ -98,31 +105,42 @@ bool redis_client::open()
}

// 如果连接密码非空,则尝试用该密码向 redis-server 认证合法性
if (pass_ && *pass_ && !authing_)
if (pass_ && *pass_) // && !authing_)
{
// 设置当前连接的状态为认证状态,以避免进入死循环
authing_ = true;
redis_connection connection(this);
if (connection.auth(pass_) == false)
{
authing_ = false;
conn_.close();
logger_error("auth error, addr: %s, passwd: %s",
addr_, pass_);
return false;
}
authing_ = false;
}

if (dbnum_ > 0)
{
redis_connection connection(this);
if (connection.select(dbnum_) == false)
{
conn_.close();
logger_error("select db error, db=%d", dbnum_);
return false;
}
}
return true;
}

void redis_client::close()
void redis_client::close(void)
{
if (conn_.opened())
conn_.close();
}

bool redis_client::eof() const
bool redis_client::eof(void) const
{
return conn_.eof();
}
Expand Down
14 changes: 12 additions & 2 deletions lib_acl_cpp/src/redis/redis_client_pool.cpp
Expand Up @@ -11,10 +11,11 @@ redis_client_pool::redis_client_pool(const char* addr, size_t count,
size_t idx /* = 0 */)
: connect_pool(addr, count, idx)
, pass_(NULL)
, dbnum_(0)
{
}

redis_client_pool::~redis_client_pool()
redis_client_pool::~redis_client_pool(void)
{
if (pass_)
acl_myfree(pass_);
Expand All @@ -31,12 +32,21 @@ redis_client_pool& redis_client_pool::set_password(const char* pass)
return *this;
}

connect_client* redis_client_pool::create_connect()
redis_client_pool& redis_client_pool::set_db(int dbnum)
{
if (dbnum > 0)
dbnum_ = dbnum;
return *this;
}

connect_client* redis_client_pool::create_connect(void)
{
redis_client* conn = NEW redis_client(addr_, conn_timeout_,
rw_timeout_);
if (pass_)
conn->set_password(pass_);
if (dbnum_ > 0)
conn->set_db(dbnum_);
return conn;
}

Expand Down

0 comments on commit 7b34362

Please sign in to comment.