2 Copyright (c) 2018-2019 Nokia.
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 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #ifndef SHAREDDATALAYER_REDIS_ASYNCHIREDISCLUSTERCOMMANDDISPATCHER_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCHIREDISCLUSTERCOMMANDDISPATCHER_HPP_
20 #include "private/redis/asynccommanddispatcher.hpp"
21 #include "private/databaseconfiguration.hpp"
22 #include "private/logger.hpp"
23 #include "private/timer.hpp"
31 #include <boost/optional.hpp>
36 struct redisClusterAsyncContext;
37 struct redisAsyncContext;
40 namespace shareddatalayer
46 class HiredisClusterSystem;
47 class HiredisClusterEpollAdapter;
50 class AsyncHiredisClusterCommandDispatcher: public AsyncCommandDispatcher
53 AsyncHiredisClusterCommandDispatcher(const AsyncHiredisClusterCommandDispatcher&) = delete;
55 AsyncHiredisClusterCommandDispatcher& operator = (const AsyncHiredisClusterCommandDispatcher&) = delete;
57 AsyncHiredisClusterCommandDispatcher(Engine& engine,
58 const boost::optional<std::string>& ns,
59 const DatabaseConfiguration::Addresses& addresses,
60 std::shared_ptr<ContentsBuilder> contentsBuilder,
61 bool usePermanentCommandCallbacks,
62 std::shared_ptr<Logger> logger);
64 AsyncHiredisClusterCommandDispatcher(Engine& engine,
65 const boost::optional<std::string>& ns,
66 const DatabaseConfiguration::Addresses& addresses,
67 std::shared_ptr<ContentsBuilder> contentsBuilder,
68 bool usePermanentCommandCallbacks,
69 HiredisClusterSystem& hiredisClusterSystem,
70 std::shared_ptr<HiredisClusterEpollAdapter> adapter,
71 std::shared_ptr<Logger> logger);
73 ~AsyncHiredisClusterCommandDispatcher() override;
75 void waitConnectedAsync(const ConnectAck& connectAck) override;
77 void registerDisconnectCb(const DisconnectCb& disconnectCb) override;
79 void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns, const Contents& contents) override;
81 void disableCommandCallbacks() override;
83 void handleReply(const CommandCb& commandCb, const std::error_code& error, const redisReply* rr);
85 bool isClientCallbacksEnabled() const;
87 void handleDisconnect(const redisAsyncContext* ac);
90 enum class ServiceState
96 using Callback = std::function<void(const Reply&)>;
99 const boost::optional<std::string> initialNamespace;
100 const DatabaseConfiguration::Addresses addresses;
101 std::shared_ptr<ContentsBuilder> contentsBuilder;
102 bool usePermanentCommandCallbacks;
103 HiredisClusterSystem& hiredisClusterSystem;
104 std::shared_ptr<HiredisClusterEpollAdapter> adapter;
105 redisClusterAsyncContext* acc;
106 ConnectAck connectAck;
107 DisconnectCb disconnectCallback;
108 ServiceState serviceState;
109 std::list<CommandCb> cbs;
110 bool clientCallbacksEnabled;
111 Timer connectionRetryTimer;
112 Timer::Duration connectionRetryTimerDuration;
113 std::shared_ptr<Logger> logger;
117 bool isValidCb(const CommandCb& commandCb);
119 void removeCb(const CommandCb& commandCb);
121 void callCommandCbWithError(const CommandCb& commandCb, const std::error_code& error);
123 void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns, const Contents& contents, bool checkConnectionState);
125 void verifyConnection();
127 void verifyConnectionReply(const std::error_code& error, const redis::Reply& reply);
131 void armConnectionRetryTimer();
133 void disconnectHiredisCluster();