mirror of
https://github.com/Icinga/icinga2.git
synced 2025-09-30 13:08:44 +02:00
There should be more atomic commits but the whole thing was a mess. This commit changes the synchrounous Redis connection into an asynchronous one in its own class RedisConnection. The RedisConnection uses a Workqueue with commands to fire against the Redis server. When a response is required a callback must be supplied, refer to RedisWriter::RedisGet(). Known Issues: - Authentication has no error handling and can break the connection - Error handling in general is iffy due to the nature of the async redis connection - Getting a reply out of RedisConnection is not trivial - HandleRW... sunt dracones
86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
/******************************************************************************
|
|
* Icinga 2 *
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
|
* *
|
|
* This program is free software; you can redistribute it and/or *
|
|
* modify it under the terms of the GNU General Public License *
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
* of the License, or (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
******************************************************************************/
|
|
|
|
#ifndef REDISCONNECTION_H
|
|
#define REDISCONNECTION_H
|
|
|
|
#include <third-party/hiredis/async.h>
|
|
#include "base/object.hpp"
|
|
#include "base/workqueue.hpp"
|
|
|
|
namespace icinga
|
|
{
|
|
/**
|
|
* An Async Redis connection.
|
|
*
|
|
* @ingroup redis
|
|
*/
|
|
class RedisConnection final : public Object
|
|
{
|
|
public:
|
|
DECLARE_PTR_TYPEDEFS(RedisConnection);
|
|
|
|
RedisConnection(const String host, const int port, const String path, const String password = "", const int db = 0);
|
|
|
|
void Start();
|
|
|
|
void Connect();
|
|
|
|
void Disconnect();
|
|
|
|
bool IsConnected();
|
|
|
|
void ExecuteQuery(const std::vector<String>& query, redisCallbackFn *fn = NULL, void *privdata = NULL);
|
|
|
|
void ExecuteQueries(const std::vector<std::vector<String> >& queries, redisCallbackFn *fn = NULL,
|
|
void *privdata = NULL);
|
|
|
|
private:
|
|
static void StaticInitialize();
|
|
|
|
void SendMessageInternal(const std::vector<String>& query, redisCallbackFn *fn, void *privdata);
|
|
|
|
void AssertOnWorkQueue();
|
|
|
|
void HandleRW();
|
|
|
|
static void DisconnectCallback(const redisAsyncContext *c, int status);
|
|
|
|
WorkQueue m_RedisConnectionWorkQueue{100000};
|
|
Timer::Ptr m_EventLoop;
|
|
|
|
redisAsyncContext *m_Context;
|
|
|
|
String m_Path;
|
|
String m_Host;
|
|
int m_Port;
|
|
String m_Password;
|
|
int m_DbIndex;
|
|
|
|
boost::mutex m_CMutex;
|
|
};
|
|
|
|
struct redis_error : virtual std::exception, virtual boost::exception { };
|
|
|
|
struct errinfo_redis_query_;
|
|
typedef boost::error_info<struct errinfo_redis_query_, std::string> errinfo_redis_query;
|
|
}
|
|
|
|
#endif //REDISCONNECTION_H
|