AWS SDK for C++  0.14.3
AWS SDK for C++
WinConnectionPoolMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <aws/core/Core_EXPORTS.h>
19 
23 
24 #include <utility>
25 #include <mutex>
26 #include <condition_variable>
27 
28 namespace Aws
29 {
30  namespace Http
31  {
32 
38  {
39  public:
40  /*
41  * Constructs instance, using iOpenHandle from InternetOpen api call, and maxConnectionsPerHost.
42  */
43  WinConnectionPoolMgr(void* iOpenHandle, unsigned maxConnectionsPerHost, long requestTimeout, long connectTimeout);
44 
45  /*
46  * Cleans up all connections that have been allocated (might take a while).
47  */
48  virtual ~WinConnectionPoolMgr();
49 
50  /*
51  * Aquires a connection for host and port from pool, or adds connections to pool until pool has reached max size
52  * If no connections are available and the pool is at its maximum size, then this call will block until connections
53  * become available.
54  */
55  void* AquireConnectionForHost(const Aws::String& host, uint16_t port);
56 
57  /*
58  * Releases a connection to host and port back to the pool, if another thread is blocked in Aquire, then the top queued item
59  * will be returned a connection and signaled to continue.
60  */
61  void ReleaseConnectionForHost(const Aws::String& host, unsigned port, void* connection);
62 
66  virtual const char* GetLogTag() const { return "ConnectionPoolMgr"; }
70  virtual void DoCloseHandle(void* handle) const = 0;
71  protected:
76  {
77  public:
78  uint16_t port;
80  unsigned currentPoolSize;
81  std::mutex connectionsMutex;
82  std::condition_variable conditionVariable;
83  };
84 
88  void* GetOpenHandle() const { return m_iOpenHandle; }
89 
93  long GetRequestTimeout() const { return m_requestTimeoutMs; }
97  long GetConnectTimeout() const { return m_connectTimeoutMs; }
101  void DoCleanup();
102 
103  private:
104 
105  virtual void* CreateNewConnection(const Aws::String& host, HostConnectionContainer& connectionContainer) const = 0;
106 
108  const WinConnectionPoolMgr& operator = (const WinConnectionPoolMgr&) = delete;
109  WinConnectionPoolMgr(const WinConnectionPoolMgr&&) = delete;
110  const WinConnectionPoolMgr& operator = (const WinConnectionPoolMgr&&) = delete;
111 
112  bool CheckAndGrowPool(const Aws::String& host, HostConnectionContainer& connectionContainer);
113 
114  void* m_iOpenHandle;
116  std::mutex m_hostConnectionsMutex;
117  unsigned m_maxConnectionsPerHost;
118  long m_requestTimeoutMs;
119  long m_connectTimeoutMs;
120  };
121 
122  } // namespace Http
123 } // namespace Aws
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition: AWSMap.h:28
std::stack< T, Deque< T > > Stack
Definition: AWSStack.h:29
virtual const char * GetLogTag() const
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
#define AWS_CORE_API
Definition: Core_EXPORTS.h:35
JSON (JavaScript Object Notation).