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_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
18 #define SHAREDDATALAYER_REDIS_ASYNCHIREDISCOMMANDDISPATCHER_HPP_
20 #include "private/redis/asynccommanddispatcher.hpp"
27 #include "private/logger.hpp"
28 #include "private/timer.hpp"
33 struct redisAsyncContext;
36 namespace shareddatalayer
43 class HiredisEpollAdapter;
46 class AsyncHiredisCommandDispatcher: public AsyncCommandDispatcher
49 AsyncHiredisCommandDispatcher(const AsyncHiredisCommandDispatcher&) = delete;
51 AsyncHiredisCommandDispatcher& operator = (const AsyncHiredisCommandDispatcher&) = delete;
53 AsyncHiredisCommandDispatcher(Engine& engine,
54 const std::string& address,
56 std::shared_ptr<ContentsBuilder> contentsBuilder,
57 bool usePermanentCommandCallbacks,
58 std::shared_ptr<Logger> logger,
59 bool usedForSentinel);
61 AsyncHiredisCommandDispatcher(Engine& engine,
62 const std::string& address,
64 std::shared_ptr<ContentsBuilder> contentsBuilder,
65 bool usePermanentCommandCallbacks,
66 HiredisSystem& hiredisSystem,
67 std::shared_ptr<HiredisEpollAdapter> adapter,
68 std::shared_ptr<Logger> logger,
69 bool usedForSentinel);
71 ~AsyncHiredisCommandDispatcher() override;
73 void waitConnectedAsync(const ConnectAck& connectAck) override;
75 void registerDisconnectCb(const DisconnectCb& disconnectCb) override;
77 void dispatchAsync(const CommandCb& commandCb, const AsyncConnection::Namespace& ns,
78 const Contents& contents) override;
80 void disableCommandCallbacks() override;
84 void setDisconnected();
86 void handleReply(const CommandCb& commandCb,
87 const std::error_code& error,
88 const redisReply* rr);
90 bool isClientCallbacksEnabled() const;
92 void verifyConnection();
94 void disconnectHiredis();
96 void armConnectionRetryTimer(Timer::Duration duration,
97 std::function<void()> retryAction);
100 enum class ServiceState
102 CONNECTION_VERIFICATION,
106 using Callback = std::function<void(const Reply&)>;
111 std::shared_ptr<ContentsBuilder> contentsBuilder;
112 bool usePermanentCommandCallbacks;
113 HiredisSystem& hiredisSystem;
114 std::shared_ptr<HiredisEpollAdapter> adapter;
115 redisAsyncContext* ac;
116 ConnectAck connectAck;
117 DisconnectCb disconnectCallback;
118 ServiceState serviceState;
119 std::list<CommandCb> cbs;
120 bool clientCallbacksEnabled;
121 Timer connectionRetryTimer;
122 Timer::Duration connectionRetryTimerDuration;
123 Timer::Duration connectionVerificationRetryTimerDuration;
124 std::shared_ptr<Logger> logger;
125 bool usedForSentinel;
129 bool isValidCb(const CommandCb& commandCb);
131 void removeCb(const CommandCb& commandCb);
133 void callCommandCbWithError(const CommandCb& commandCb, const std::error_code& error);
135 void dispatchAsync(const CommandCb& commandCb, const Contents& contents, bool checkConnectionState);
137 void verifyConnectionReply(const std::error_code& error, const redis::Reply& reply);