SWC-DB  v0.5.9 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
ClientConnQueue.h
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 #ifndef swcdb_core_comm_ClientConnQueue_h
7 #define swcdb_core_comm_ClientConnQueue_h
8 
9 
12 #include <queue>
13 #include <unordered_set>
14 
15 
16 namespace SWC { namespace Comm { namespace client {
17 
18 
19 class ConnQueue;
20 typedef std::shared_ptr<ConnQueue> ConnQueuePtr;
21 
23  public:
24 
25  typedef std::shared_ptr<ConnQueueReqBase> Ptr;
28 
30  ConnQueueReqBase(Buffers::Ptr&& a_cbp) noexcept
31  : cbp(std::move(a_cbp)), queue(nullptr) { }
32 
35  const ConnQueuePtr& a_queue) noexcept
36  : cbp(std::move(a_cbp)), queue(a_queue) { }
37 
39  Ptr req() noexcept {
40  return std::dynamic_pointer_cast<ConnQueueReqBase>(shared_from_this());
41  }
42 
43  virtual bool insistent() noexcept { return false; };
44 
45  void request_again();
46 
47  void print(std::ostream& out);
48 
49  protected:
50 
51  virtual ~ConnQueueReqBase() noexcept { }
52 
53 };
54 
55 
56 
57 class ConnQueue :
58  private std::queue<ConnQueueReqBase::Ptr>,
59  public std::enable_shared_from_this<ConnQueue> {
60  public:
61 
63 
65  ConnQueue(const IoContextPtr& ioctx,
66  const Config::Property::Value_int32_g::Ptr keepalive_ms=nullptr,
67  const Config::Property::Value_int32_g::Ptr again_delay_ms=nullptr)
68  : cfg_keepalive_ms(keepalive_ms),
69  cfg_again_delay_ms(again_delay_ms),
70  m_ioctx(ioctx), m_conn(nullptr),
71  m_connecting(false),
73  ? new asio::high_resolution_timer(m_ioctx->executor())
74  : nullptr) {
75  }
76 
77  virtual ~ConnQueue() noexcept {
78  delete m_timer;
79  }
80 
81  virtual bool connect() {
82  return false; // not implemented by default
83  }
84 
85  virtual void close_issued() { }
86 
87  void stop();
88 
89  EndPoint get_endpoint_remote() noexcept;
90 
91  EndPoint get_endpoint_local() noexcept;
92 
93  void put(const ReqBase::Ptr& req);
94 
95  void set(const ConnHandlerPtr& conn);
96 
97  void delay(ReqBase::Ptr&& req);
98 
99  void print(std::ostream& out);
100 
101  protected:
102 
103  const Config::Property::Value_int32_g::Ptr cfg_keepalive_ms;
104  const Config::Property::Value_int32_g::Ptr cfg_again_delay_ms;
105 
106  private:
107 
108  void exec_queue();
109 
110  void run_queue();
111 
112  void schedule_close(bool closing);
113 
114  Core::MutexSptd m_mutex;
118  Core::StateRunning m_q_state;
119  asio::high_resolution_timer* m_timer;
120  std::unordered_set<asio::high_resolution_timer*> m_delayed;
121 
122 
123 };
124 
125 
126 }}} //namespace SWC::Comm::client
127 
128 
129 
130 #ifdef SWC_IMPL_SOURCE
132 #endif
133 
134 
135 #endif // swcdb_core_comm_ClientConnQueue_h
SWC::Comm::client::ConnQueue::m_delayed
std::unordered_set< asio::high_resolution_timer * > m_delayed
Definition: ClientConnQueue.h:120
SWC::Comm::client::ConnQueue::get_endpoint_remote
EndPoint get_endpoint_remote() noexcept
Definition: ClientConnQueue.cc:63
SWC::Comm::client::ConnQueueReqBase::queue
ConnQueuePtr queue
Definition: ClientConnQueue.h:27
SWC::Comm::client::ConnQueueReqBase::req
SWC_CAN_INLINE Ptr req() noexcept
Definition: ClientConnQueue.h:39
SWC::Comm::Buffers::Ptr
std::shared_ptr< Buffers > Ptr
Definition: Buffers.h:23
SWC::Comm::EndPoint
asio::ip::tcp::endpoint EndPoint
Definition: Resolver.h:19
ConnHandler.h
SWC::Comm::client::ConnQueue::stop
void stop()
Definition: ClientConnQueue.cc:26
SWC::Comm::client::ConnQueue::set
void set(const ConnHandlerPtr &conn)
Definition: ClientConnQueue.cc:91
SWC::Comm::client::ConnQueueReqBase
Definition: ClientConnQueue.h:22
SWC::Comm::IoContextPtr
std::shared_ptr< IoContext > IoContextPtr
Definition: IoContext.h:16
SWC::Comm::client::ConnQueue::ReqBase
ConnQueueReqBase ReqBase
Definition: ClientConnQueue.h:62
SWC::Comm::client::ConnQueueReqBase::~ConnQueueReqBase
virtual ~ConnQueueReqBase() noexcept
Definition: ClientConnQueue.h:51
SWC::Comm::client::ConnQueue
Definition: ClientConnQueue.h:59
IoContext.h
SWC::Comm::client::ConnQueueReqBase::request_again
void request_again()
Definition: ClientConnQueue.cc:12
SWC::Comm::client::ConnQueue::run_queue
void run_queue()
Definition: ClientConnQueue.cc:167
SWC::Comm::client::ConnQueue::print
void print(std::ostream &out)
Definition: ClientConnQueue.cc:136
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Comm::client::ConnQueue::cfg_again_delay_ms
const Config::Property::Value_int32_g::Ptr cfg_again_delay_ms
Definition: ClientConnQueue.h:104
SWC::Comm::client::ConnQueue::put
void put(const ReqBase::Ptr &req)
Definition: ClientConnQueue.cc:73
SWC::Comm::client::ConnQueue::exec_queue
void exec_queue()
Definition: ClientConnQueue.cc:147
SWC::Comm::client::ConnQueueReqBase::ConnQueueReqBase
SWC_CAN_INLINE ConnQueueReqBase(Buffers::Ptr &&a_cbp) noexcept
Definition: ClientConnQueue.h:30
SWC::Comm::client::ConnQueue::m_timer
asio::high_resolution_timer * m_timer
Definition: ClientConnQueue.h:119
SWC::Comm::client::ConnQueue::m_q_state
Core::StateRunning m_q_state
Definition: ClientConnQueue.h:118
SWC::Comm::client::ConnQueue::schedule_close
void schedule_close(bool closing)
Definition: ClientConnQueue.cc:203
SWC::Comm::client::ConnQueue::delay
void delay(ReqBase::Ptr &&req)
Definition: ClientConnQueue.cc:100
SWC::Comm::client::ConnQueue::get_endpoint_local
EndPoint get_endpoint_local() noexcept
Definition: ClientConnQueue.cc:68
SWC::Comm::ConnHandlerPtr
std::shared_ptr< ConnHandler > ConnHandlerPtr
Definition: AppContext.h:17
SWC::Comm::client::ConnQueueReqBase::print
void print(std::ostream &out)
Definition: ClientConnQueue.cc:19
SWC::Comm::DispatchHandler
Definition: DispatchHandler.h:17
SWC::Comm::client::ConnQueue::close_issued
virtual void close_issued()
Definition: ClientConnQueue.h:85
SWC::Comm::client::ConnQueueReqBase::cbp
Buffers::Ptr cbp
Definition: ClientConnQueue.h:26
ClientConnQueue.cc
SWC::Comm::client::ConnQueue::ConnQueue
SWC_CAN_INLINE ConnQueue(const IoContextPtr &ioctx, const Config::Property::Value_int32_g::Ptr keepalive_ms=nullptr, const Config::Property::Value_int32_g::Ptr again_delay_ms=nullptr)
Definition: ClientConnQueue.h:65
SWC::Comm::client::ConnQueueReqBase::ConnQueueReqBase
SWC_CAN_INLINE ConnQueueReqBase(Buffers::Ptr &&a_cbp, const ConnQueuePtr &a_queue) noexcept
Definition: ClientConnQueue.h:34
SWC::Comm::client::ConnQueue::~ConnQueue
virtual ~ConnQueue() noexcept
Definition: ClientConnQueue.h:77
SWC::Comm::client::ConnQueue::m_mutex
Core::MutexSptd m_mutex
Definition: ClientConnQueue.h:114
SWC::Comm::client::ConnQueue::m_connecting
bool m_connecting
Definition: ClientConnQueue.h:117
SWC::Comm::client::ConnQueueReqBase::Ptr
std::shared_ptr< ConnQueueReqBase > Ptr
Definition: ClientConnQueue.h:25
SWC::Comm::client::ConnQueueReqBase::insistent
virtual bool insistent() noexcept
Definition: ClientConnQueue.h:43
SWC::Comm::client::ConnQueue::m_conn
ConnHandlerPtr m_conn
Definition: ClientConnQueue.h:116
SWC::Comm::client::ConnQueue::cfg_keepalive_ms
const Config::Property::Value_int32_g::Ptr cfg_keepalive_ms
Definition: ClientConnQueue.h:103
SWC::Config::Property::Value_int32_g
Definition: Property.h:581
SWC::Comm::client::ConnQueuePtr
std::shared_ptr< ConnQueue > ConnQueuePtr
Definition: ClientConnQueue.h:19
SWC::Comm::client::ConnQueue::m_ioctx
IoContextPtr m_ioctx
Definition: ClientConnQueue.h:115
SWC::Comm::client::ConnQueue::connect
virtual bool connect()
Definition: ClientConnQueue.h:81