Skip to content

Commit

Permalink
Supports using socketopt to set the timeout of all connections in con…
Browse files Browse the repository at this point in the history
…nection pool.
  • Loading branch information
zhengshuxin committed Aug 14, 2024
1 parent 22392dc commit 0835d76
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
23 changes: 14 additions & 9 deletions lib_acl_cpp/include/acl_cpp/connpool/connect_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct conns_pools {
std::vector<connect_pool*> pools;
size_t check_next; // 连接检测时的检测点下标
size_t conns_next; // 下一个要访问的的下标值
conns_pools(void) {
conns_pools() {
check_next = 0;
conns_next = 0;
}
Expand All @@ -29,11 +29,13 @@ struct conn_config {
size_t count;
int conn_timeout;
int rw_timeout;
bool sockopt_timeo;

conn_config(void) {
count = 0;
conn_timeout = 5;
rw_timeout = 5;
conn_config() {
count = 0;
conn_timeout = 5;
rw_timeout = 5;
sockopt_timeo = false;
}
};

Expand Down Expand Up @@ -64,10 +66,12 @@ class ACL_CPP_API connect_manager : public noncopyable {
* COUNT 信息时便用此值,当此值为 0 时,则不限制连接数上限
* @param conn_timeout {int} 网络连接时间(秒)
* @param rw_timeout {int} 网络 IO 超时时间(秒)
* @param sockopt_timeo {bool} 是否使用 setsockopt 设置网络读写超时
* 注:default_addr 和 addr_list 不能同时为空
*/
void init(const char* default_addr, const char* addr_list,
size_t count, int conn_timeout = 30, int rw_timeout = 30);
size_t count, int conn_timeout = 30, int rw_timeout = 30,
bool sockopt_timeo = false);

/**
* 添加服务器的客户端连接池,该函数可以在程序运行时被调用,内部自动加锁
Expand All @@ -77,9 +81,10 @@ class ACL_CPP_API connect_manager : public noncopyable {
* 连接池的连接上限
* @param conn_timeout {int} 网络连接时间(秒)
* @param rw_timeout {int} 网络 IO 超时时间(秒)
* @param sockopt_timeo {bool} 是否使用 setsockopt 设置网络读写超时
*/
void set(const char* addr, size_t count,
int conn_timeout = 30, int rw_timeout = 30);
void set(const char* addr, size_t count, int conn_timeout = 30,
int rw_timeout = 30, bool sockopt_timeo = false);

/**
* 根据指定地址获取该地址对应的连接池配置对象
Expand Down Expand Up @@ -256,7 +261,7 @@ class ACL_CPP_API connect_manager : public noncopyable {

// 设置除缺省服务之外的服务器集群
void set_service_list(const char* addr_list, int count,
int conn_timeout, int rw_timeout);
int conn_timeout, int rw_timeout, bool sockopt_timeo = false);
conns_pools& get_pools_by_id(unsigned long id);
connect_pool* create_pool(const conn_config& cf, size_t idx);
void create_pools_for(pools_t& pools);
Expand Down
5 changes: 4 additions & 1 deletion lib_acl_cpp/include/acl_cpp/connpool/connect_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ class ACL_CPP_API connect_pool : public noncopyable {
* 此接口用来设置超时时间
* @param conn_timeout {int} 网络连接超时时间(秒)
* @param rw_timeout {int} 网络 IO 超时时间(秒)
* @param sockopt_timo {bool} 是否使用 setsockopt 设置读写超时
*/
connect_pool& set_timeout(int conn_timeout, int rw_timeout);
connect_pool& set_timeout(int conn_timeout, int rw_timeout,
bool sockopt_timo = false);

/**
* 设置连接池异常的重试时间间隔
Expand Down Expand Up @@ -196,6 +198,7 @@ class ACL_CPP_API connect_pool : public noncopyable {
char addr_[256]; // 连接池对应的服务器地址,IP:PORT
int conn_timeout_; // 网络连接超时时间(秒)
int rw_timeout_; // 网络 IO 超时时间(秒)
bool sockopt_timo_; // 是否使用s setsockopt 设置超时
size_t idx_; // 该连接池对象在集合中的下标位置
size_t max_; // 最大连接数
size_t count_; // 当前的连接数
Expand Down
51 changes: 28 additions & 23 deletions lib_acl_cpp/src/connpool/connect_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,33 @@ void connect_manager::set_idle_ttl(time_t ttl)
}

void connect_manager::init(const char* default_addr, const char* addr_list,
size_t count, int conn_timeout /* = 30 */, int rw_timeout /* = 30 */)
size_t count, int conn_timeout /* 30 */, int rw_timeout /* 30 */,
bool sockopt_timeo /* false */)
{
if (addr_list != NULL && *addr_list != 0) {
set_service_list(addr_list, (int) count,
conn_timeout, rw_timeout);
set_service_list(addr_list, (int) count, conn_timeout,
rw_timeout, sockopt_timeo);
}

// 创建缺省服务连接池对象,该对象一同放入总的连接池集群中
if (default_addr != NULL && *default_addr != 0) {
logger("default_pool: %s", default_addr);
int max = check_addr(default_addr, default_addr_, count);
if (max < 0) {
logger("no default connection set");
} else {
set(default_addr_.c_str(), max, conn_timeout, rw_timeout);
default_pool_ = get(default_addr_);
}
} else {
if (default_addr == NULL || *default_addr == 0) {
logger("no default connection set");
return;
}

logger("default_pool: %s", default_addr);
int max = check_addr(default_addr, default_addr_, count);
if (max < 0) {
logger("no default connection set");
return;
}

set(default_addr_.c_str(), max, conn_timeout, rw_timeout, sockopt_timeo);
default_pool_ = get(default_addr_);
}

void connect_manager::set_service_list(const char* addr_list, int count,
int conn_timeout, int rw_timeout)
int conn_timeout, int rw_timeout, bool sockopt_timeo /* false */)
{
if (addr_list == NULL || *addr_list == 0) {
logger("addr_list null");
Expand All @@ -143,7 +146,7 @@ void connect_manager::set_service_list(const char* addr_list, int count,
logger_error("invalid server addr: %s", addr.c_str());
continue;
}
set(addr.c_str(), max, conn_timeout, rw_timeout);
set(addr.c_str(), max, conn_timeout, rw_timeout, sockopt_timeo);
logger("add one service: %s, max connect: %d",
addr.c_str(), max);
}
Expand All @@ -169,8 +172,8 @@ size_t connect_manager::size() const
return n;
}

void connect_manager::set(const char* addr, size_t count,
int conn_timeout /* = 30 */, int rw_timeout /* = 30 */)
void connect_manager::set(const char* addr, size_t count, int conn_timeout /* 30 */,
int rw_timeout /* 30 */, bool sockopt_timeo /* false */)
{
string buf(addr);
buf.lower();
Expand All @@ -179,15 +182,17 @@ void connect_manager::set(const char* addr, size_t count,
std::map<string, conn_config>::iterator it = addrs_.find(buf);
if (it == addrs_.end()) {
conn_config config;
config.addr = addr;
config.count = count;
config.conn_timeout = conn_timeout;
config.rw_timeout = rw_timeout;
addrs_[buf] = config;
config.addr = addr;
config.count = count;
config.conn_timeout = conn_timeout;
config.rw_timeout = rw_timeout;
config.sockopt_timeo = sockopt_timeo;
addrs_[buf] = config;
} else {
it->second.count = count;
it->second.conn_timeout = conn_timeout;
it->second.rw_timeout = rw_timeout;
it->second.sockopt_timeo = sockopt_timeo;
}
}

Expand Down Expand Up @@ -311,7 +316,7 @@ connect_pool* connect_manager::create_pool(const conn_config& cf, size_t idx)
connect_pool* pool = create_pool(cf.addr, cf.count, idx);
pool->set_key(key);
pool->set_retry_inter(retry_inter_);
pool->set_timeout(cf.conn_timeout, cf.rw_timeout);
pool->set_timeout(cf.conn_timeout, cf.rw_timeout, cf.sockopt_timeo);
if (idle_ttl_ >= 0) {
pool->set_idle_ttl(idle_ttl_);
}
Expand Down
6 changes: 4 additions & 2 deletions lib_acl_cpp/src/connpool/connect_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ void connect_pool::set_key(const char* key)
acl_lowercase(key_);
}

connect_pool& connect_pool::set_timeout(int conn_timeout, int rw_timeout)
connect_pool& connect_pool::set_timeout(int conn_timeout, int rw_timeout,
bool sockopt_timeo /* false */)
{
conn_timeout_ = conn_timeout;
rw_timeout_ = rw_timeout;
rw_timeout_ = rw_timeout;
sockopt_timo_ = sockopt_timeo;
return *this;
}

Expand Down

0 comments on commit 0835d76

Please sign in to comment.