pool.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <functional>
  16. #include <memory>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <mongocxx/options/pool.hpp>
  19. #include <mongocxx/stdx.hpp>
  20. #include <mongocxx/uri.hpp>
  21. #include <mongocxx/config/prelude.hpp>
  22. namespace mongocxx {
  23. MONGOCXX_INLINE_NAMESPACE_BEGIN
  24. class client;
  25. ///
  26. /// A pool of @c client objects associated with a MongoDB deployment.
  27. ///
  28. /// For interoperability with other MongoDB drivers, the minimum and maximum number of connections
  29. /// in the pool is configured using the 'minPoolSize' and 'maxPoolSize' connection string options.
  30. ///
  31. /// @see https://docs.mongodb.com/master/reference/connection-string/#connection-string-options
  32. ///
  33. /// @remark When connecting to a replica set, it is @b much more efficient to use a pool as opposed
  34. /// to manually constructing @c client objects. The pool will use a single background thread per
  35. /// server to monitor the topology of the replica set, all of which are shared between the client
  36. /// objects created by the pool. A standalone client will instead "stop the world" every 60 seconds
  37. /// to check the status of the cluster. Because of this, if multiple threads are available, a
  38. /// connection pool should be used even if the application itself is single-threaded.
  39. ///
  40. class MONGOCXX_API pool {
  41. public:
  42. ///
  43. /// Creates a pool associated with a connection string.
  44. ///
  45. /// @param mongodb_uri
  46. /// A MongoDB URI representing the connection parameters
  47. /// @param options
  48. /// Options to use when connecting to the MongoDB deployment.
  49. ///
  50. /// @throws mongocxx::exception if invalid options are provided (whether from the URI or
  51. /// provided client options).
  52. explicit pool(const uri& mongodb_uri = mongocxx::uri(),
  53. const options::pool& options = options::pool());
  54. ///
  55. /// Destroys a pool.
  56. ///
  57. ~pool();
  58. ///
  59. /// An entry is a handle on a @c client object acquired via the pool. Similar to
  60. /// std::unique_ptr.
  61. ///
  62. /// @note The lifetime of any entry object must be a subset of the pool object
  63. /// from which it was acquired.
  64. ///
  65. class MONGOCXX_API entry {
  66. public:
  67. /// Access a member of the client instance.
  68. client* operator->() const& noexcept;
  69. client* operator->() && = delete;
  70. /// Retrieve a reference to the client.
  71. client& operator*() const& noexcept;
  72. client& operator*() && = delete;
  73. /// Assign nullptr to this entry to release its client to the pool.
  74. entry& operator=(std::nullptr_t) noexcept;
  75. /// Return true if this entry has a client acquired from the pool.
  76. explicit operator bool() const noexcept;
  77. private:
  78. friend class pool;
  79. using unique_client = std::unique_ptr<client, std::function<void MONGOCXX_CALL(client*)>>;
  80. MONGOCXX_PRIVATE explicit entry(unique_client);
  81. unique_client _client;
  82. };
  83. ///
  84. /// Acquires a client from the pool. The calling thread will block until a connection is
  85. /// available.
  86. ///
  87. entry acquire();
  88. ///
  89. /// Acquires a client from the pool. This method will return immediately, but may return a
  90. /// disengaged optional if a client is not available.
  91. ///
  92. stdx::optional<entry> try_acquire();
  93. private:
  94. friend class options::auto_encryption;
  95. MONGOCXX_PRIVATE void _release(client* client);
  96. class MONGOCXX_PRIVATE impl;
  97. const std::unique_ptr<impl> _impl;
  98. };
  99. MONGOCXX_INLINE_NAMESPACE_END
  100. } // namespace mongocxx
  101. #include <mongocxx/config/postlude.hpp>