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

251 lines
7.1 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <list>
2017-06-02 14:47:24 +08:00
#include "../stdlib/locker.hpp"
#include "../stdlib/noncopyable.hpp"
2014-11-19 00:25:21 +08:00
namespace acl
{
class connect_manager;
class connect_client;
/**
*
* create_connect
* set_delay_destroy()
*
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API connect_pool : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
/**
*
* @param addr {const char*} ip:port(domain:port)
* @param max {size_t} 0
*
* @param idx {size_t} ( 0 )
2014-11-19 00:25:21 +08:00
*/
connect_pool(const char* addr, size_t max, size_t idx = 0);
2014-11-19 00:25:21 +08:00
/**
*
2014-11-19 00:25:21 +08:00
*/
virtual ~connect_pool();
/**
*
* @param conn_timeout {int} ()
* @param rw_timeout {int} IO ()
*/
connect_pool& set_timeout(int conn_timeout, int rw_timeout);
2014-11-19 00:25:21 +08:00
/**
*
* @param retry_inter {int} ()
* <= 0
* 1
2014-11-19 00:25:21 +08:00
* @return {connect_pool&}
*/
connect_pool& set_retry_inter(int retry_inter);
/**
*
* @param ttl {time_t} < 0
* == 0 > 0
2014-11-19 00:25:21 +08:00
* @return {connect_pool&}
*/
connect_pool& set_idle_ttl(time_t ttl);
2015-08-12 16:48:24 +08:00
/**
* 30
* @param n {int}
2015-08-12 16:48:24 +08:00
* @return {connect_pool&}
*/
connect_pool& set_check_inter(int n);
2014-11-19 00:25:21 +08:00
/**
*
* NULL
*
* @param on {bool}
* false
* @return {connect_client*}
2014-11-19 00:25:21 +08:00
*/
2018-12-19 13:31:46 +08:00
connect_client* peek(bool on = true);
2014-11-19 00:25:21 +08:00
/**
* 使
*
2018-12-19 13:31:46 +08:00
* @param conn {redis_client*}
*/
void bind_one(connect_client* conn);
/**
*
*
2014-11-19 00:25:21 +08:00
* @param conn {redis_client*}
* @param keep {bool}
2014-11-19 00:25:21 +08:00
*/
void put(connect_client* conn, bool keep = true);
/**
*
* @param ttl {time_t}
* @param exclusive {bool}
* @return {int}
2014-11-19 00:25:21 +08:00
*/
int check_idle(time_t ttl, bool exclusive = true);
/**
*
* @param ok {bool}
2014-11-19 00:25:21 +08:00
*/
void set_alive(bool ok /* true | false */);
/**
*
*
* @return {bool} true
*
2014-11-19 00:25:21 +08:00
*/
bool aliving();
/**
*
* @return {const char*}
2014-11-19 00:25:21 +08:00
*/
const char* get_addr() const
{
return addr_;
}
/**
* 0
* @return {size_t}
2014-11-19 00:25:21 +08:00
*/
size_t get_max() const
2015-08-12 16:48:24 +08:00
{
return max_;
}
/**
*
* @return {size_t}
2015-08-12 16:48:24 +08:00
*/
size_t get_count() const
2014-11-19 00:25:21 +08:00
{
return count_;
}
/**
*
2014-11-19 00:25:21 +08:00
* @return {size_t}
*/
size_t get_idx() const
{
return idx_;
}
/**
*
* @param inter {int}
2014-11-19 00:25:21 +08:00
*/
void reset_statistics(int inter);
2014-11-19 00:25:21 +08:00
/**
* 使
2014-11-19 00:25:21 +08:00
*/
unsigned long long get_total_used() const
{
return total_used_;
}
/**
* 使
2014-11-19 00:25:21 +08:00
* @return {unsigned long long}
*/
unsigned long long get_current_used() const
{
return current_used_;
}
2018-12-14 20:19:19 +08:00
public:
void set_key(const char* key);
const char* get_key(void) const
{
return key_;
}
2014-11-19 00:25:21 +08:00
protected:
2015-08-12 16:48:24 +08:00
/**
*
2015-08-12 16:48:24 +08:00
* @return {connect_client*}
*/
2014-11-19 00:25:21 +08:00
virtual connect_client* create_connect() = 0;
friend class connect_manager;
/**
* 0
2014-11-19 00:25:21 +08:00
*/
void set_delay_destroy();
protected:
bool alive_; // 是否属正常
bool delay_destroy_; // 是否设置了延迟自销毁
// 有问题的服务器的可以重试的时间间隔,不可用连接池对象再次被启用的时间间隔
2014-11-19 00:25:21 +08:00
int retry_inter_;
time_t last_dead_; // 该连接池对象上次不可用时的时间截
char key_[256]; // 与该连接池相关的 key
char addr_[256]; // 连接池对应的服务器地址IP:PORT
int conn_timeout_; // 网络连接超时时间(秒)
int rw_timeout_; // 网络 IO 超时时间(秒)
size_t idx_; // 该连接池对象在集合中的下标位置
size_t max_; // 最大连接数
size_t count_; // 当前的连接数
time_t idle_ttl_; // 空闲连接的生命周期
time_t last_check_; // 上次检查空闲连接的时间截
int check_inter_; // 检查空闲连接的时间间隔
locker lock_; // 访问 pool_ 时的互斥锁
unsigned long long total_used_; // 该连接池的所有访问量
unsigned long long current_used_; // 某时间段内的访问量
time_t last_; // 上次记录的时间截
std::list<connect_client*> pool_; // 连接池集合
2014-11-19 00:25:21 +08:00
};
class ACL_CPP_API connect_guard : public noncopyable
{
public:
connect_guard(connect_pool& pool)
2018-12-19 13:31:46 +08:00
: keep_(true), pool_(pool), conn_(NULL)
{
}
virtual ~connect_guard(void)
{
if (conn_)
pool_.put(conn_, keep_);
}
void set_keep(bool keep)
{
keep_ = keep;
}
connect_client* peek(void)
{
conn_ = pool_.peek();
return conn_;
}
protected:
bool keep_;
connect_pool& pool_;
connect_client* conn_;
};
2014-11-19 00:25:21 +08:00
} // namespace acl