NCBI C++ ToolKit
srv_connections_impl.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CONNECT_SERVICES___SRV_CONNECTIONS_IMPL__HPP
2 #define CONNECT_SERVICES___SRV_CONNECTIONS_IMPL__HPP
3 
4 /* $Id: srv_connections_impl.hpp 100701 2023-08-31 19:21:12Z lavr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Dmitry Kazimirov
30  *
31  * File Description:
32  *
33  */
34 
35 #include "netservice_params.hpp"
36 
38 
39 #include <corelib/ncbimtx.hpp>
40 
41 #include <util/ncbi_url.hpp>
42 
43 #include <bitset>
44 
46 
48 {
50  const CNetServer::SExecResult& exec_result) :
51  m_Connection(exec_result.conn),
52  m_FirstOutputLine(exec_result.response),
56  {
57  }
58 
60 
62  bool ReadLine(string& output);
63 
64 private:
66 
69 
72 };
73 
75 {
76 };
77 
79 {
80  using TPropCreator = function<INetServerProperties*()>;
82 
83  virtual TPropCreator GetPropCreator() const;
85 
86  // Event handlers.
87  virtual void OnConnected(CNetServerConnection& connection) = 0;
88 
89  void OnError(const string& err_msg, CNetServer& server);
90  void OnWarning(const string& warn_msg, CNetServer& server);
91 
92  void SetErrorHandler(TEventHandler error_handler);
93  void SetWarningHandler(TEventHandler warning_handler);
94 
95 protected:
99 
100 private:
101  virtual void OnErrorImpl(const string& err_msg, CNetServer& server) = 0;
102  virtual void OnWarningImpl(const string& warn_msg, CNetServer& server) = 0;
103 
106 };
107 
109 {
111 
112  // Return this connection to the pool.
113  virtual void DeleteThis();
114 
115  void WriteLine(const string& line);
116  void ReadCmdOutputLine(string& result,
117  bool multiline_output);
118 
119  void Close();
120  void Abort();
121 
122  virtual ~SNetServerConnectionImpl();
123 
124  // The server this connection is connected to.
128 
130 };
131 
133 {
134 public:
136 
137  virtual void Exec(CNetServerConnection::TInstance conn_impl,
138  const STimeout* timeout) = 0;
139 };
140 
142 {
143 public:
145 
146  virtual void OnExec(CNetServerConnection::TInstance conn_impl,
147  const string& cmd) = 0;
148 };
149 
151 {
152  // The parameters below describe different conditions that trigger server throttling.
154 
155  // Connection failure rate (numerator/denominator), which is when reached, triggers server throttling.
157  {
158  size_t numerator = 0;
159  size_t denominator = 1;
160  constexpr static size_t kMaxDenominator = 128;
161 
162  void Init(CSynRegistry& registry, const SRegSynonyms& sections);
164 
165  // How many seconds the API should wait before attempting to connect to a misbehaving server again.
166  // Throttling is off if period is less or equal to zero.
168 
169  // Whether to check with LBSMD before re-enabling the server.
171 
172  // Whether to throttle on connection failures only
174 
175  void Init(CSynRegistry& registry, const SRegSynonyms& sections);
176 };
177 
179 {
180  SThrottleStats(SThrottleParams params) : m_Params(std::move(params)) { Reset(); }
181 
182  void Adjust(SNetServerImpl* server_impl, int err_code);
183  void Check(SNetServerImpl* server_impl);
184  void Discover();
185 
186 private:
187  void Reset();
188 
191  pair<bitset<SThrottleParams::SIOFailureThreshold::kMaxDenominator>, size_t> m_IOFailureRegister;
197 };
198 
199 struct SNetServerInPool : public CObject
200 {
202  INetServerProperties* server_properties, SThrottleParams throttle_params);
203 
204  // Releases a reference to the parent service object,
205  // and if that was the last reference, the service object
206  // will be deleted, which in turn will lead to this server
207  // object being deleted too (along with all other server
208  // objects that the parent service object may contain).
209  virtual void DeleteThis();
210 
211  virtual ~SNetServerInPool();
212 
213  void TryExec(SNetServerImpl* server, INetServerExecHandler& handler, const STimeout* timeout);
214 
216  CNetServerConnection Connect(SNetServerImpl* server, const STimeout* timeout);
217 
218 public:
219  // A smart pointer to the server pool object that contains
220  // this NetServer. Valid only when this object is returned
221  // to an outside caller via ReturnServer() or GetServer().
223 
225 
226  // API-specific server properties.
228 
233 
236 };
237 
239 {
240  SNetServerInfoImpl(const string& version_string);
241  bool GetNextAttribute(string& attr_name, string& attr_value);
242 
243 private:
245 
248 };
249 
250 struct SNetServerImpl : public CObject
251 {
252  struct SConnectDeadline;
253 
255  SNetServerInPool* server_in_pool) :
256  m_Service(service),
257  m_ServerInPool(server_in_pool)
258  {
259  }
260 
262  const STimeout* timeout = NULL);
263 
265  bool multiline_output,
266  bool retry_on_exception = false);
267 
268  void ConnectAndExec(const string& cmd,
269  bool multiline_output,
270  CNetServer::SExecResult& exec_result,
271  const STimeout* timeout = NULL,
272  INetServerExecListener* exec_listener = NULL);
273 
274  static void ConnectImpl(CSocket&, SConnectDeadline&, const SSocketAddress&,
275  const SSocketAddress&);
276 
277  template <class TProperties>
279  {
281  return CRef<TProperties>(static_cast<TProperties*>(properties));
282  }
283 
286 };
287 
289 {
290 public:
291  CTimeoutKeeper(CSocket* sock, const STimeout* timeout)
292  {
293  if (timeout == NULL)
294  m_Socket = NULL;
295  else {
296  m_Socket = sock;
299  sock->SetTimeout(eIO_ReadWrite, timeout);
300  }
301  }
302 
304  {
305  if (m_Socket != NULL) {
308  }
309  }
310 
314 };
315 
317 
318 #endif /* CONNECT_SERVICES___SRV_CONNECTIONS_IMPL__HPP */
CAtomicCounter –.
Definition: ncbicntr.hpp:71
CFastMutex –.
Definition: ncbimtx.hpp:667
function< bool(const string &, CNetServer)> TEventHandler
CObject –.
Definition: ncbiobj.hpp:180
CSocket::
CTime –.
Definition: ncbitime.hpp:296
CTimeoutKeeper(CSocket *sock, const STimeout *timeout)
virtual void Exec(CNetServerConnection::TInstance conn_impl, const STimeout *timeout)=0
virtual void OnExec(CNetServerConnection::TInstance conn_impl, const string &cmd)=0
container_type::const_iterator const_iterator
Definition: map.hpp:53
static CMemoryRegistry registry
Definition: cn3d_tools.cpp:81
void(*)(CSeq_entry_Handle seh, IWorkbench *wb, const CSerialObject &obj) handler
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
static CS_CONNECTION * conn
Definition: ct_dynamic.c:25
#define false
Definition: bool.h:36
static SQLCHAR output[256]
Definition: print.c:5
#define NULL
Definition: ncbistd.hpp:225
TNCBIAtomicValue TValue
Alias TValue for TNCBIAtomicValue.
Definition: ncbicntr.hpp:73
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
TObjectType * GetPointerOrNull(void) THROWS_NONE
Get pointer value.
Definition: ncbiobj.hpp:986
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const STimeout * GetTimeout(EIO_Event event) const
Get timeout for I/O in the specified direction.
EIO_Status SetTimeout(EIO_Event event, const STimeout *timeout)
Set timeout for I/O in the specified direction.
list< TArg > TArgs
Definition: ncbi_url.hpp:276
@ eIO_Write
write
Definition: ncbi_core.h:121
@ eIO_ReadWrite
eIO_Read | eIO_Write (also, eCONN_OnFlush)
Definition: ncbi_core.h:122
@ eIO_Read
read
Definition: ncbi_core.h:120
#define NCBI_XCONNECT_EXPORT
Multi-threading – mutexes; rw-locks; semaphore.
function< INetServerProperties *()> TPropCreator
virtual void OnErrorImpl(const string &err_msg, CNetServer &server)=0
virtual INetServerConnectionListener * Clone()=0
CNetService::TEventHandler TEventHandler
virtual void OnConnected(CNetServerConnection &connection)=0
virtual void OnWarningImpl(const string &warn_msg, CNetServer &server)=0
SNetServerConnectionImpl(SNetServerImpl *pool)
SNetServerConnectionImpl * m_NextFree
CAtomicCounter::TValue m_Generation
void ReadCmdOutputLine(string &result, bool multiline_output)
void WriteLine(const string &line)
virtual void DeleteThis()
Virtual method "deleting" this object.
CRef< SNetServerInPool > m_ServerInPool
static void ConnectImpl(CSocket &, SConnectDeadline &, const SSocketAddress &, const SSocketAddress &)
SNetServerImpl(CNetService::TInstance service, SNetServerInPool *server_in_pool)
CRef< TProperties > Get()
void TryExec(INetServerExecHandler &handler, const STimeout *timeout=NULL)
CNetServer::SExecResult ConnectAndExec(const string &cmd, bool multiline_output, bool retry_on_exception=false)
SNetServerInPool(SSocketAddress address, INetServerProperties *server_properties, SThrottleParams throttle_params)
SThrottleStats m_ThrottleStats
virtual ~SNetServerInPool()
CRef< INetServerProperties > m_ServerProperties
void TryExec(SNetServerImpl *server, INetServerExecHandler &handler, const STimeout *timeout)
SNetServerConnectionImpl * m_FreeConnectionListHead
CNetServerPool m_ServerPool
virtual void DeleteThis()
Virtual method "deleting" this object.
CNetServerConnection GetConnectionFromPool(SNetServerImpl *server)
CAtomicCounter m_CurrentConnectionGeneration
CFastMutex m_FreeConnectionListLock
CNetServerConnection Connect(SNetServerImpl *server, const STimeout *timeout)
bool GetNextAttribute(string &attr_name, string &attr_value)
SNetServerInfoImpl(const string &version_string)
TAttributes::const_iterator m_NextAttribute
SNetServerMultilineCmdOutputImpl(const CNetServer::SExecResult &exec_result)
void Init(CSynRegistry &registry, const SRegSynonyms &sections)
struct SThrottleParams::SIOFailureThreshold io_failure_threshold
void Init(CSynRegistry &registry, const SRegSynonyms &sections)
void Check(SNetServerImpl *server_impl)
const SThrottleParams m_Params
void Adjust(SNetServerImpl *server_impl, int err_code)
pair< bitset< SThrottleParams::SIOFailureThreshold::kMaxDenominator >, size_t > m_IOFailureRegister
SThrottleStats(SThrottleParams params)
Timeout structure.
Definition: ncbi_types.h:76
else result
Definition: token2.c:20
Modified on Tue May 28 05:50:08 2024 by modify_doxy.py rev. 669887