acl/lib_acl_cpp/include/acl_cpp/connpool/connect_manager.hpp

267 lines
9.8 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/string.hpp"
#include "../stdlib/locker.hpp"
#include "../stdlib/noncopyable.hpp"
2014-11-19 00:25:21 +08:00
#include <vector>
2018-12-15 23:32:41 +08:00
#include <map>
2014-11-19 00:25:21 +08:00
struct ACL_EVENT;
namespace acl
{
class connect_pool;
class connect_monitor;
// 内部使用数据结构
struct conns_pools {
std::vector<connect_pool*> pools;
size_t check_next; // 连接检测时的检测点下标
size_t conns_next; // 下一个要访问的的下标值
conns_pools(void)
{
check_next = 0;
conns_next = 0;
}
};
2014-11-19 00:25:21 +08:00
/**
* connect pool
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API connect_manager : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
connect_manager(void);
virtual ~connect_manager(void);
2014-11-19 00:25:21 +08:00
2018-12-14 20:19:19 +08:00
/**
* 线 false
*
2018-12-14 20:19:19 +08:00
* @param yes {bool}
*/
void bind_thread(bool yes);
2014-11-19 00:25:21 +08:00
/**
* set
* @param default_addr {const char*}
* 使
* @param addr_list {const char*}
* : IP:PORT:COUNT;IP:PORT:COUNT;IP:PORT;IP:PORT ...
* IP:PORT:COUNT,IP:PORT:COUNT,IP:PORT;IP:PORT ...
* 127.0.0.1:7777:50;192.168.1.1:7777:10;127.0.0.1:7778
* @param count {size_t} addr_list
* COUNT 便 0
* @param conn_timeout {int} ()
* @param rw_timeout {int} IO ()
* default_addr addr_list
2014-11-19 00:25:21 +08:00
*/
void init(const char* default_addr, const char* addr_list,
size_t count, int conn_timeout = 30, int rw_timeout = 30);
2014-11-19 00:25:21 +08:00
/**
*
* @param addr {const char*} ip:port
*
* @param count {size_t} , 0
*
* @param conn_timeout {int} ()
* @param rw_timeout {int} IO ()
2014-11-19 00:25:21 +08:00
*/
2018-12-15 23:32:41 +08:00
void set(const char* addr, size_t count,
int conn_timeout = 30, int rw_timeout = 30);
2014-11-19 00:25:21 +08:00
/**
*
*
* @param n {int} <= 0
2014-11-19 00:25:21 +08:00
*/
void set_retry_inter(int n);
/**
*
* @param ttl {time_t} < 0
* == 0 > 0
*/
void set_idle_ttl(time_t ttl);
/**
* 30
* @param n {int}
*/
void set_check_inter(int n);
2014-11-19 00:25:21 +08:00
/**
*
*
* @param addr {const char*} (ip:port)
2014-11-19 00:25:21 +08:00
*/
2018-12-15 23:32:41 +08:00
void remove(const char* addr);
2014-11-19 00:25:21 +08:00
/**
*
* @param addr {const char*} redis (ip:port)
* @param exclusive {bool} 访
* true
* @param restore {bool}
*
* @return {connect_pool*}
2014-11-19 00:25:21 +08:00
*/
2015-03-08 21:34:00 +08:00
connect_pool* get(const char* addr, bool exclusive = true,
bool restore = false);
2014-11-19 00:25:21 +08:00
/**
*
*
*
*
* @return {connect_pool*}
2014-11-19 00:25:21 +08:00
*/
2018-12-14 20:19:19 +08:00
virtual connect_pool* peek(void);
2014-11-19 00:25:21 +08:00
/**
*
*
* CRC32
* @param key {const char*} NULL
*
* @param exclusive {bool} 访
* true
* @return {connect_pool*}
2014-11-19 00:25:21 +08:00
*/
virtual connect_pool* peek(const char* key, bool exclusive = true);
/**
* peek
2014-11-19 00:25:21 +08:00
*/
2018-12-14 20:19:19 +08:00
void lock(void);
2014-11-19 00:25:21 +08:00
/**
* peek
2014-11-19 00:25:21 +08:00
*/
2018-12-14 20:19:19 +08:00
void unlock(void);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
* @return {std::vector<connect_pool*>&}
*/
2018-12-15 23:32:41 +08:00
std::vector<connect_pool*>& get_pools(void);
2014-11-19 00:25:21 +08:00
/**
*
* @param step {size_t}
* @param left {size_t*}
* @return {size_t}
*/
size_t check_idle(size_t step, size_t* left = NULL);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
* @return {size_t}
*/
2018-12-15 23:32:41 +08:00
size_t size(void) const;
2014-11-19 00:25:21 +08:00
/**
*
* @return {connect_pool*} init default_addr
* NULL
2014-11-19 00:25:21 +08:00
*/
2018-12-14 20:19:19 +08:00
connect_pool* get_default_pool(void)
2014-11-19 00:25:21 +08:00
{
return default_pool_;
}
/**
* redis 访
2014-11-19 00:25:21 +08:00
*/
2018-12-14 20:19:19 +08:00
void statistics(void);
2014-11-19 00:25:21 +08:00
/**
* 线
* @param monitor {connect_monitor*}
* @return {bool} false
* stop_monitor
2014-11-19 00:25:21 +08:00
*/
2015-04-04 15:25:29 +08:00
bool start_monitor(connect_monitor* monitor);
2014-11-19 00:25:21 +08:00
/**
* 线
* @param graceful {bool} 线
*
* false 使线退
* 使线退
* @return {connect_monitor*} start_monitor
* monitor_ NULL
2014-11-19 00:25:21 +08:00
*/
2015-04-04 15:25:29 +08:00
connect_monitor* stop_monitor(bool graceful = true);
2014-11-19 00:25:21 +08:00
/**
*
* @param addr {const char*} ip:port
* @param alive {bool}
2014-11-19 00:25:21 +08:00
*/
void set_pools_status(const char* addr, bool alive);
protected:
/**
*
* @param addr {const char*} ip:port
* @param count {size_t} 0
* @param idx {size_t} ( 0 )
* @return {connect_pool*}
2014-11-19 00:25:21 +08:00
*/
virtual connect_pool* create_pool(const char* addr,
size_t count, size_t idx) = 0;
2014-11-19 00:25:21 +08:00
protected:
2018-12-15 23:32:41 +08:00
typedef std::vector<connect_pool*> pools_t;
typedef pools_t::iterator pools_it;
typedef pools_t::const_iterator pools_cit;
typedef std::map<unsigned long, conns_pools*> manager_t;
typedef manager_t::iterator manager_it;
typedef manager_t::const_iterator manager_cit;
2018-12-15 23:32:41 +08:00
bool thread_binding_; // 用于协程环境中与每个线程绑定
string default_addr_; // 缺省的服务地址
connect_pool* default_pool_; // 缺省的服务连接池
2018-12-15 23:32:41 +08:00
struct conn_config {
2018-12-15 23:32:41 +08:00
string addr;
size_t count;
int conn_timeout;
int rw_timeout;
};
std::map<string, conn_config> addrs_;// 所有的服务端地址
manager_t manager_;
2018-12-15 23:32:41 +08:00
locker lock_; // 访问 pools_ 时的互斥锁
int stat_inter_; // 统计访问量的定时器间隔
int retry_inter_; // 连接池失败后重试的时间间隔
time_t idle_ttl_; // 空闲连接的生命周期
int check_inter_; // 检查空闲连接的时间间隔
connect_monitor* monitor_; // 后台检测线程句柄
2014-11-19 00:25:21 +08:00
// 设置除缺省服务之外的服务器集群
void set_service_list(const char* addr_list, int count,
int conn_timeout, int rw_timeout);
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);
2018-12-15 23:32:41 +08:00
void remove(pools_t& pools, const char* addr);
void set_status(pools_t& pools, const char* addr, bool alive);
unsigned long get_id(void) const;
2018-12-14 20:19:19 +08:00
void get_key(const char* addr, string& key);
void get_addr(const char* key, string& addr);
2018-12-15 23:32:41 +08:00
connect_pool* add_pool(const char* addr);
// 线程局部变量初始化时的回调方法
static void thread_oninit(void);
// 线程退出前需要回调此方法,用来释放内部创建的线程局部变量
static void thread_onexit(void* ctx);
2014-11-19 00:25:21 +08:00
};
} // namespace acl