https_client_pool.h
1 // Copyright 2019, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 
5 #pragma once
6 
7 #include <boost/asio/ssl/context.hpp>
8 #include <deque>
9 #include <memory>
10 
11 namespace util {
12 
13 class IoContext;
14 
15 namespace http {
16 
17 class HttpsClient;
18 
19 // IoContext specific, thread-local pool that manages a set of https connections.
21  public:
22  class HandleGuard {
23  public:
24  HandleGuard(HttpsClientPool* pool = nullptr) : pool_(pool) {}
25 
26  void operator()(HttpsClient* client);
27 
28  private:
29  HttpsClientPool* pool_;
30  };
31 
32  using ClientHandle = std::unique_ptr<HttpsClient, HandleGuard>;
33 
34  HttpsClientPool(const std::string& domain, ::boost::asio::ssl::context* ssl_ctx,
35  IoContext* io_cntx);
36 
37  ~HttpsClientPool();
38 
46  ClientHandle GetHandle();
47 
48  void set_connect_timeout(unsigned msec) { connect_msec_ = msec; }
49 
51  void set_retry_count(uint32_t cnt) { retry_cnt_ = cnt; }
52 
53  IoContext& io_context() { return io_cntx_; }
54 
56  unsigned handles_count() const { return existing_handles_; }
57 
58  const std::string domain() const { return domain_; }
59 
60  private:
61  using SslContext = ::boost::asio::ssl::context;
62 
63  SslContext& ssl_cntx_;
64  IoContext& io_cntx_;
65  std::string domain_;
66  unsigned connect_msec_ = 1000, retry_cnt_ = 1;
67  int existing_handles_ = 0;
68 
69  std::deque<HttpsClient*> available_handles_; // Using queue to allow round-robin access.
70 };
71 
72 } // namespace http
73 } // namespace util
void set_retry_count(uint32_t cnt)
Sets number of retries for https client handles.
unsigned handles_count() const
Number of existing handles created by this pool.
ClientHandle GetHandle()
Returns https client connection from the pool.