C/C++编程:连接池类connect_pool

£神魔★判官ぃ 2022-09-04 02:57 445阅读 0赞

前置准备

  • C/C++编程:连接客户端类connect_client
  • 连接池指的是针对某一个服务器地址可以有多个连接

知识点:

1:纯虚类:不能创建实例,只能继承,而且子类必须实现其纯虚函数

  1. class connect_pool : public noncopyable{
  2. protected:
  3. /**
  4. * 纯虚函数,需要子类实现
  5. * @return {connect_client*}
  6. */
  7. virtual connect_client* create_connect() = 0;
  8. }
  9. class connect_pool : public oceanstar::connect_pool{
  10. // 基类纯虚函数的实现
  11. oceanstar::connect_client* create_connect(){
  12. return new connect_client(addr_.c_str(), conn_timeout_, conn_timeout_);
  13. }
  14. }

2. 如果父类没有默认构造函数,那么子类必须显式调用其父类的构造函数

  1. class connect_pool : public noncopyable{
  2. public:
  3. /**
  4. * 构造函数(因为这里显式写了一个构造函数,所以编译器不会默认生成无参构造函数)
  5. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  6. * @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
  7. */
  8. connect_pool(const char* addr, size_t max): max_(max){
  9. strncpy(addr_, addr, sizeof(addr_));
  10. }
  11. class connect_pool : public oceanstar::connect_pool{
  12. public:
  13. connect_pool(const char* addr, size_t count)
  14. : oceanstar::connect_pool(addr, count){
  15. //必须实现这种构造函数,否则:使用了被删除的函数‘connect_pool::connect_pool()’
  16. }

版本1

源码(纯虚类)

  1. //
  2. // Created by oceanstar on 2021/8/10.
  3. //
  4. #ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
  5. #define OCEANSTAR_HTTP_CONNECT_POOL_H
  6. #include <list>
  7. #include <noncopyable.h>
  8. #include "connect_client.h"
  9. #include <cstring>
  10. namespace oceanstar{
  11. class connect_client;
  12. /**
  13. * 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
  14. * 纯虚函数 create_connect 用于创建与服务端的一个连接
  15. */
  16. class connect_pool : public noncopyable{
  17. public:
  18. /**
  19. * 构造函数
  20. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  21. * @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
  22. */
  23. connect_pool(const char* addr, size_t max): max_(max){
  24. strncpy(addr_, addr, sizeof(addr_));
  25. }
  26. ~connect_pool(void)
  27. {
  28. std::list<connect_client*>::iterator it = pool_.begin();
  29. for (; it != pool_.end(); ++it) {
  30. delete *it;
  31. }
  32. }
  33. protected:
  34. /**
  35. * 纯虚函数,需要子类实现
  36. * @return {connect_client*}
  37. */
  38. virtual connect_client* create_connect() = 0;
  39. protected:
  40. char addr_[256]; // 连接池对应的服务器地址,IP:PORT
  41. size_t max_; // 最大连接数
  42. std::list<connect_client*> pool_; // 连接池集合
  43. };
  44. }
  45. #endif //OCEANSTAR_HTTP_CONNECT_POOL_H

使用

  1. class connect_pool : public oceanstar::connect_pool{
  2. public:
  3. connect_pool(const char* addr, size_t max)
  4. : oceanstar::connect_pool(addr, max){
  5. //必须实现这种构造函数,否则:使用了被删除的函数‘connect_pool::connect_pool()’
  6. }
  7. protected:
  8. // 基类纯虚函数的实现
  9. oceanstar::connect_client* create_connect(){
  10. return new connect_client(addr_.c_str(), conn_timeout_, rw_timeout_);
  11. }
  12. protected:
  13. std::string addr_;
  14. int conn_timeout_;
  15. int rw_timeout_;
  16. };

版本:添加key

因为连接池总是绑定到某一个地址【1V1】,所以可以为这个连接池设置一个key,用来标识其身份。最好的key是addr

  1. //
  2. // Created by oceanstar on 2021/8/10.
  3. //
  4. #ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
  5. #define OCEANSTAR_HTTP_CONNECT_POOL_H
  6. #include <list>
  7. #include <noncopyable.h>
  8. #include "connect_client.h"
  9. #include "acl_mystring.h"
  10. #include <cstring>
  11. namespace oceanstar{
  12. class connect_client;
  13. /**
  14. * 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
  15. * 纯虚函数 create_connect 用于创建与服务端的一个连接
  16. */
  17. class connect_pool : public noncopyable{
  18. public:
  19. /**
  20. * 构造函数
  21. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  22. * @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
  23. */
  24. connect_pool(const char* addr, size_t max): max_(max){
  25. strncpy(addr_, addr, sizeof(addr_));
  26. strncpy(key_, addr_, sizeof(key_));
  27. acl_lowercase(key_);
  28. }
  29. ~connect_pool(void)
  30. {
  31. std::list<connect_client*>::iterator it = pool_.begin();
  32. for (; it != pool_.end(); ++it) {
  33. delete *it;
  34. }
  35. }
  36. public:
  37. void set_key(const char* key){
  38. strncpy(key_, key, sizeof(key_));
  39. acl_lowercase(key_);
  40. }
  41. const char* get_key(void) const
  42. {
  43. return key_;
  44. }
  45. protected:
  46. /**
  47. * 纯虚函数,需要子类实现
  48. * @return {connect_client*}
  49. */
  50. virtual connect_client* create_connect() = 0;
  51. protected:
  52. char key_[256]; // 与该连接池相关的 key
  53. char addr_[256]; // 连接池对应的服务器地址,IP:PORT
  54. size_t max_; // 最大连接数
  55. std::list<connect_client*> pool_; // 连接池集合
  56. };
  57. }
  58. #endif //OCEANSTAR_HTTP_CONNECT_POOL_H

版本:设置连接池是否可用

  1. //
  2. // Created by oceanstar on 2021/8/10.
  3. //
  4. #ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
  5. #define OCEANSTAR_HTTP_CONNECT_POOL_H
  6. #include <list>
  7. #include <noncopyable.h>
  8. #include "connect_client.h"
  9. #include "acl_mystring.h"
  10. #include "thread_mutex.h"
  11. #include <cstring>
  12. namespace oceanstar{
  13. class connect_client;
  14. /**
  15. * 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
  16. * 纯虚函数 create_connect 用于创建与服务端的一个连接
  17. */
  18. class connect_pool : public noncopyable{
  19. public:
  20. /**
  21. * 构造函数
  22. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  23. * @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
  24. */
  25. connect_pool(const char* addr, size_t max): max_(max), alive_(true), last_dead_(0){
  26. strncpy(addr_, addr, sizeof(addr_));
  27. strncpy(key_, addr_, sizeof(key_));
  28. acl_lowercase(key_);
  29. }
  30. ~connect_pool(void)
  31. {
  32. std::list<connect_client*>::iterator it = pool_.begin();
  33. for (; it != pool_.end(); ++it) {
  34. delete *it;
  35. }
  36. }
  37. public:
  38. void set_key(const char* key){
  39. strncpy(key_, key, sizeof(key_));
  40. acl_lowercase(key_);
  41. }
  42. const char* get_key(void) const
  43. {
  44. return key_;
  45. }
  46. /**
  47. * 设置连接池的存活状态
  48. */
  49. void set_alive(bool yes /* true | false */){
  50. lock_.lock();
  51. alive_ = yes;
  52. if (yes == false) {
  53. time(&last_dead_);
  54. }
  55. lock_.unlock();
  56. }
  57. /**
  58. * 检查连接池是否正常
  59. * @return {bool} 返回 true 表示当前连接池处于正常状态,否则表示当前
  60. * 连接池不可用
  61. */
  62. bool aliving(){
  63. return alive_;
  64. }
  65. protected:
  66. /**
  67. * 纯虚函数,需要子类实现
  68. * @return {connect_client*}
  69. */
  70. virtual connect_client* create_connect() = 0;
  71. protected:
  72. char key_[256]; // 与该连接池相关的 key
  73. char addr_[256]; // 连接池对应的服务器地址,IP:PORT
  74. size_t max_; // 最大连接数
  75. std::list<connect_client*> pool_; // 连接池集合
  76. bool alive_; // 是否属正常
  77. time_t last_dead_; // 该连接池对象上次不可用时的时间截
  78. thread_mutex lock_; // 访问 pool_ 时的互斥锁
  79. };
  80. }
  81. #endif //OCEANSTAR_HTTP_CONNECT_POOL_H

版本:添加超时时间

  1. //
  2. // Created by oceanstar on 2021/8/10.
  3. //
  4. #ifndef OCEANSTAR_HTTP_CONNECT_POOL_H
  5. #define OCEANSTAR_HTTP_CONNECT_POOL_H
  6. #include <list>
  7. #include <noncopyable.h>
  8. #include "connect_client.h"
  9. #include "acl_mystring.h"
  10. #include "thread_mutex.h"
  11. #include <cstring>
  12. namespace oceanstar{
  13. class connect_client;
  14. /**
  15. * 客户端连接池类,实现对连接池的动态管理,该类为纯虚类,需要子类实现
  16. * 纯虚函数 create_connect 用于创建与服务端的一个连接
  17. */
  18. class connect_pool : public noncopyable{
  19. public:
  20. /**
  21. * 构造函数
  22. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  23. * @param max {size_t} 连接池最大连接个数限制,如果该值设为 0,则不设置连接池的连接上限
  24. */
  25. connect_pool(const char* addr, size_t max): max_(max), alive_(true), last_dead_(0){
  26. strncpy(addr_, addr, sizeof(addr_));
  27. strncpy(key_, addr_, sizeof(key_));
  28. acl_lowercase(key_);
  29. }
  30. ~connect_pool(void)
  31. {
  32. std::list<connect_client*>::iterator it = pool_.begin();
  33. for (; it != pool_.end(); ++it) {
  34. delete *it;
  35. }
  36. }
  37. /**
  38. * 此接口用来设置超时时间
  39. * @param conn_timeout {int} 网络连接超时时间(秒)
  40. * @param rw_timeout {int} 网络 IO 超时时间(秒)
  41. */
  42. connect_pool& set_timeout(int conn_timeout, int rw_timeout){
  43. conn_timeout_ = conn_timeout;
  44. rw_timeout_ = rw_timeout;
  45. return *this;
  46. }
  47. public:
  48. void set_key(const char* key){
  49. strncpy(key_, key, sizeof(key_));
  50. acl_lowercase(key_);
  51. }
  52. const char* get_key(void) const
  53. {
  54. return key_;
  55. }
  56. /**
  57. * 设置连接池的存活状态
  58. */
  59. void set_alive(bool yes /* true | false */){
  60. lock_.lock();
  61. alive_ = yes;
  62. if (yes == false) {
  63. time(&last_dead_);
  64. }
  65. lock_.unlock();
  66. }
  67. /**
  68. * 检查连接池是否正常
  69. * @return {bool} 返回 true 表示当前连接池处于正常状态,否则表示当前
  70. * 连接池不可用
  71. */
  72. bool aliving(){
  73. return alive_;
  74. }
  75. protected:
  76. /**
  77. * 纯虚函数,需要子类实现
  78. * @return {connect_client*}
  79. */
  80. virtual connect_client* create_connect() = 0;
  81. protected:
  82. char key_[256]; // 与该连接池相关的 key
  83. char addr_[256]; // 连接池对应的服务器地址,IP:PORT
  84. size_t max_; // 最大连接数
  85. std::list<connect_client*> pool_; // 连接池集合
  86. bool alive_; // 是否属正常
  87. time_t last_dead_; // 该连接池对象上次不可用时的时间截
  88. thread_mutex lock_; // 访问 pool_ 时的互斥锁
  89. int conn_timeout_ = 30; // 网络连接超时时间(秒)
  90. int rw_timeout_ = 30; // 网络 IO 超时时间(秒)
  91. };
  92. }
  93. #endif //OCEANSTAR_HTTP_CONNECT_POOL_H

使用

  1. //
  2. // Created by oceanstar on 2021/8/18.
  3. //
  4. #ifndef OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H
  5. #define OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H
  6. #include <connpool/connect_pool.h>
  7. #include "http_request.h"
  8. namespace oceanstar{
  9. class http_request_pool : public connect_pool{
  10. public:
  11. /**
  12. * 构造函数
  13. * @param addr {const char*} 服务器监听地址,格式:ip:port(domain:port)
  14. * @param count {size_t} 连接池最大连接个数限制,当该值为 0 时则没有限制
  15. * @param idx {size_t} 该连接池对象在集合中的下标位置(从 0 开始)
  16. */
  17. http_request_pool(const char* addr, size_t count) : connect_pool(addr, count){
  18. }
  19. ~http_request_pool() = default;;
  20. protected:
  21. // 基类纯虚函数,该函数返回后由基类设置该连接池的网络连接及网络 IO 超时时间
  22. virtual connect_client* create_connect(){
  23. http_request* req = new http_request(addr_, conn_timeout_, rw_timeout_);
  24. return req;
  25. }
  26. };
  27. }
  28. #endif //OCEANSTAR_HTTP_HTTP_REQUEST_POOL_H

发表评论

表情:
评论列表 (有 0 条评论,445人围观)

还没有评论,来说两句吧...

相关阅读

    相关 druid连接_工具

    Druid连接池 Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。 在功能、性能、扩展性方面,都超过其他数据库