forked fromCylix/cpp_redis
- Notifications
You must be signed in to change notification settings - Fork207
Introduce client pool#83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsBUILD.bazel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionexamples/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletionsexamples/cpp_redis_client_pool.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // The MIT License (MIT) | ||
| // | ||
| // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com> | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
| #include "cpp_redis/core/client.hpp" | ||
| #include "cpp_redis/core/client_pool.hpp" | ||
| #include "cpp_redis/core/reply.hpp" | ||
| #include "cpp_redis/core/types.hpp" | ||
| #include <cpp_redis/core/reply.hpp> | ||
| #include <cpp_redis/cpp_redis> | ||
| #include "winsock_initializer.h" | ||
| #include <future> | ||
| #include <iostream> | ||
| #include <list> | ||
| static void | ||
| sync_work_chunk(cpp_redis::client_pool& pool, std::string const& key, std::size_t offset) { | ||
| //! Perform offset number of synchronous increments to the given key. | ||
| while (offset--) { | ||
| pool.run([&key](cpp_redis::client* c) { | ||
| c->incr(key); | ||
| c->sync_commit(); | ||
| }); | ||
| } | ||
| } | ||
| static void | ||
| async_work_chunk(cpp_redis::client_pool& pool, std::string const& key, std::size_t offset) { | ||
| std::list<std::future<cpp_redis::reply_t>> replies; | ||
| //! Perform offset number of asynchronous increments to the given key. | ||
| while (offset--) { | ||
| pool.run([&key, &replies](cpp_redis::client* c) { | ||
| replies.push_back(c->incr(key)); | ||
| c->commit(); | ||
| }); | ||
| } | ||
| //! Consume the replies in the order of production. | ||
| while (!replies.empty()) { | ||
| replies.front().wait(); | ||
| replies.pop_front(); | ||
| } | ||
| } | ||
| const std::string key{"number"}; | ||
| int | ||
| main() { | ||
| winsock_initializer winsock_init; | ||
| //! Enable logging | ||
| cpp_redis::active_logger = std::unique_ptr<cpp_redis::logger>(new cpp_redis::logger); | ||
| //! Offset to assign to each work chunk. | ||
| std::size_t ofs1{12}, ofs2{14}, ofs3{1}, ofs4{11}, ofs5{9}, ofs6{7}; | ||
| //! Create a pool of four clients to handle requests in a thread safe manner. | ||
| cpp_redis::client_pool pool(4, "127.0.0.1", 26379); | ||
| //! Assign work to the pool. | ||
| auto f1 = std::async([&pool, ofs1] { | ||
| sync_work_chunk(pool, key, ofs1); | ||
| }); | ||
| auto f2 = std::async([&pool, ofs2] { | ||
| sync_work_chunk(pool, key, ofs2); | ||
| }); | ||
| auto f3 = std::async([&pool, ofs3] { | ||
| async_work_chunk(pool, key, ofs3); | ||
| }); | ||
| auto f4 = std::async([&pool, ofs4] { | ||
| async_work_chunk(pool, key, ofs4); | ||
| }); | ||
| auto f5 = std::async([&pool, ofs5] { | ||
| sync_work_chunk(pool, key, ofs5); | ||
| }); | ||
| async_work_chunk(pool, key, ofs6); | ||
| //! Wait for all threads to finish. | ||
| f1.wait(), f2.wait(), f3.wait(), f4.wait(), f5.wait(); | ||
| //! Retrieve the number stored in the server. | ||
| std::future<cpp_redis::reply_t> result; | ||
| pool.run([&result](cpp_redis::client* c) { | ||
| result = c->get(key); | ||
| }); | ||
| //! Expected outcome of all the transactions. | ||
| int expected = ofs1 + ofs2 + ofs3 + ofs4 + ofs5 + ofs6; | ||
| std::cout << "Expected result=" << expected << std::endl; | ||
| std::cout << "Computed result=" << result.get().as_string() << std::endl; | ||
| return 0; | ||
| } | ||
129 changes: 129 additions & 0 deletionsincludes/cpp_redis/core/client_pool.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // The MIT License (MIT) | ||
| // | ||
| // Copyright (c) 2015-2017 Simon Ninon <simon.ninon@gmail.com> | ||
| // | ||
| // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| // of this software and associated documentation files (the "Software"), to deal | ||
| // in the Software without restriction, including without limitation the rights | ||
| // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| // copies of the Software, and to permit persons to whom the Software is | ||
| // furnished to do so, subject to the following conditions: | ||
| // | ||
| // The above copyright notice and this permission notice shall be included in all | ||
| // copies or substantial portions of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| // SOFTWARE. | ||
| #ifndef CPP_REDIS_CORE_CLIENT_POOL_HPP_ | ||
| #define CPP_REDIS_CORE_CLIENT_POOL_HPP_ | ||
| #include <atomic> | ||
| #include <condition_variable> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <tuple> | ||
| #include <vector> | ||
| #include "cpp_redis/core/reply.hpp" | ||
| #include <cpp_redis/core/client.hpp> | ||
| namespace cpp_redis { | ||
| namespace detail { | ||
| class idle_client { | ||
| std::condition_variable& m_bell; | ||
| std::vector<std::atomic_bool>& m_state; | ||
| std::mutex& m_mtx; | ||
| public: | ||
| idle_client(std::condition_variable& cv, std::vector<std::atomic_bool>& m_state, std::mutex& mtx); | ||
| ~idle_client(); | ||
| int m_index; | ||
| }; | ||
| } // namespace detail | ||
| class client_pool { | ||
| public: | ||
| struct con_details { | ||
| std::string m_host; | ||
| std::size_t m_port; | ||
| connect_callback_t m_connect_callback; | ||
| std::uint32_t m_timeout_ms; | ||
| std::int32_t m_max_reconnects; | ||
| std::uint32_t m_reconnect_interval_ms; | ||
| }; | ||
| /** | ||
| * Constructor | ||
| * | ||
| * @detail Creates a pool of clients, usable in a thread safe manner. | ||
| * | ||
| * @param host host to be connected to | ||
| * @param port port to be connected to | ||
| * @param connect_callback connect handler to be called on connect events (may be null) | ||
| * @param timeout_ms maximum time to connect | ||
| * @param max_reconnects maximum attempts of reconnection if connection dropped | ||
| * @param reconnect_interval_ms time between two attempts of reconnection | ||
| * | ||
| */ | ||
| client_pool( | ||
| std::size_t pool_size, | ||
| const std::string& host = "127.0.0.1", | ||
| std::size_t port = 6379, | ||
| const connect_callback_t& connect_callback = nullptr, | ||
| std::uint32_t timeout_ms = 0, | ||
| std::int32_t max_reconnects = 0, | ||
| std::uint32_t reconnect_interval_ms = 0); | ||
| /** | ||
| * @brief Use a client from the pool in a thread safe fashion. | ||
| * | ||
| * @tparam F Callable type equivalent to <Ret(client*)> | ||
| * @param fun Operation to perform. | ||
| */ | ||
| template <class F> | ||
| cpp_redis::reply_t run(F&& fun); | ||
| private: | ||
| detail::idle_client get_idle_client(); | ||
| con_details m_connection_details; | ||
| std::vector<std::unique_ptr<client>> m_clients; | ||
| std::vector<std::atomic_bool> m_occupancy; | ||
| mutable std::condition_variable m_task_done; | ||
| mutable std::mutex m_mtx; | ||
| }; | ||
| namespace detail { | ||
| bool client_is_operable(std::unique_ptr<cpp_redis::client>& cl, client_pool::con_details const& details); | ||
| } | ||
| template <class F> | ||
| cpp_redis::reply_t | ||
| client_pool::run(F&& fun) { | ||
| detail::idle_client work(m_task_done, m_occupancy, m_mtx); | ||
| auto& cl = m_clients[work.m_index]; | ||
| if (detail::client_is_operable(cl, m_connection_details)) { | ||
| std::forward<F>(fun)(cl.get()); | ||
| return reply_t("OK", reply_t::string_type::simple_string); | ||
| } | ||
| return reply_t("disconnected", reply_t::string_type::error); | ||
| } | ||
| } // namespace cpp_redis | ||
| #endif | ||
1 change: 1 addition & 0 deletionsincludes/cpp_redis/cpp_redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.