EgtExecutor :
- in hiredis aggiunta gestione nodi sentinella e Auth per connessione sincrona.
This commit is contained in:
+52
-8
@@ -60,27 +60,29 @@ static const string KEY_TYPE_JSON = "ReJSON-RL" ; // non usato, disponibile s
|
||||
|
||||
// struttura per parametri della stringa di Connessione
|
||||
static const int DEFAULT_PORT = 6379 ;
|
||||
static const int DEFAULT_SENTINEL_MASTER_PORT = 26379 ;
|
||||
static const int DEFAULT_DATABASE = 0 ;
|
||||
static const int DEFAULT_TIMEOUT = 15000 ; // [ms]
|
||||
static const int DEFAULT_ALIVE_TIME = 180 ; // [s]
|
||||
struct RedisConnectionInfo {
|
||||
|
||||
string sHost, sServiceName ;
|
||||
string sHost, sServiceName, sPassword ;
|
||||
int nPort, nDefaultDataBase, nKeepAlive, nConnectTimeout, nSyncTimeout, nAsyncTimeout ;
|
||||
bool bAbortConnect, bSsl, bAllowAdmin ;
|
||||
|
||||
RedisConnectionInfo() {
|
||||
sHost = "" ; sServiceName = "" ;
|
||||
sHost = "" ; sServiceName = "" ; sPassword = "" ;
|
||||
nPort = DEFAULT_PORT ; nDefaultDataBase = DEFAULT_DATABASE ; nKeepAlive = DEFAULT_ALIVE_TIME ;
|
||||
nConnectTimeout = DEFAULT_TIMEOUT ; nSyncTimeout = DEFAULT_TIMEOUT ; nAsyncTimeout = DEFAULT_TIMEOUT ;
|
||||
bAbortConnect = false ; bSsl = false ; bAllowAdmin = false ;
|
||||
} ;
|
||||
RedisConnectionInfo( string sMyHost, int nMyPort, string sMyServiceName, int nMyDefaultDataBase,
|
||||
int dMyKeepAlive, int dMyConnectTimeout, int dMySyncTimeout,
|
||||
RedisConnectionInfo( string sMyHost, string sMyServiceName, string sMyPassword, int nMyPort,
|
||||
int nMyDefaultDataBase, int dMyKeepAlive, int dMyConnectTimeout, int dMySyncTimeout,
|
||||
int dMyAsyncTimeout, bool bMyAbortConnect, bool bMySsl, bool bMyAllowAdmin)
|
||||
: sHost( sMyHost), nPort( nMyPort), sServiceName( sMyServiceName), nDefaultDataBase( nMyDefaultDataBase),
|
||||
nKeepAlive( dMyKeepAlive), nConnectTimeout( dMyConnectTimeout), nSyncTimeout( dMySyncTimeout),
|
||||
nAsyncTimeout( dMyAsyncTimeout), bAbortConnect( bMyAbortConnect), bSsl( bMySsl), bAllowAdmin( bMyAllowAdmin) {}
|
||||
: sHost( sMyHost), sServiceName( sMyServiceName), sPassword( sMyPassword), nPort( nMyPort),
|
||||
nDefaultDataBase( nMyDefaultDataBase), nKeepAlive( dMyKeepAlive), nConnectTimeout( dMyConnectTimeout),
|
||||
nSyncTimeout( dMySyncTimeout), nAsyncTimeout( dMyAsyncTimeout), bAbortConnect( bMyAbortConnect),
|
||||
bSsl( bMySsl), bAllowAdmin( bMyAllowAdmin) {}
|
||||
} ;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -120,7 +122,8 @@ GetParamsFromConnectionString( const string& sConnection, RedisConnectionInfo& C
|
||||
// Recupero i parametri ( rimuovo tutti gli spazi )
|
||||
sParams.erase( remove( sParams.begin(), sParams.end(), ' '), sParams.end()) ;
|
||||
GetValInNotes( sParams, "serviceName", ",", ConnectionInfo.sServiceName) ;
|
||||
GetValInNotes( sParams, "DefaultDatabase", ",", ConnectionInfo.sServiceName) ;
|
||||
GetValInNotes( sParams, "password", ",", ConnectionInfo.sPassword) ;
|
||||
GetValInNotes( sParams, "DefaultDatabase", ",", ConnectionInfo.nDefaultDataBase) ;
|
||||
GetValInNotes( sParams, "keepAlive", ",", ConnectionInfo.nKeepAlive) ;
|
||||
GetValInNotes( sParams, "connectTimeout", ",", ConnectionInfo.nConnectTimeout) ;
|
||||
GetValInNotes( sParams, "syncTimeout", ",", ConnectionInfo.nSyncTimeout) ;
|
||||
@@ -198,6 +201,47 @@ ExeRedisConnect( const string& sConnection)
|
||||
}
|
||||
LOG_INFO( GetCmdLogger(), "Sync connection to redis !")
|
||||
|
||||
// Verifico se la connessione è di tipo Sentinel
|
||||
redisReply* replySentinel = ( redisReply*)redisCommand( s_pRedisContext, "SENTINEL get-master-addr-by-name %s",
|
||||
ConnectionInfo.sServiceName.c_str()) ;
|
||||
if ( replySentinel == nullptr || replySentinel->type != REDIS_REPLY_ARRAY || replySentinel->elements != 2) {
|
||||
if ( replySentinel != nullptr)
|
||||
freeReplyObject( replySentinel) ;
|
||||
}
|
||||
else if ( replySentinel->element[0] != nullptr && replySentinel->element[0]->str != nullptr &&
|
||||
replySentinel->element[1] != nullptr && replySentinel->element[1]->str != nullptr) {
|
||||
// --- Nodo sentinella
|
||||
string sMasterHost = replySentinel->element[0]->str ;
|
||||
int nMasterPort = DEFAULT_SENTINEL_MASTER_PORT ;
|
||||
FromString( replySentinel->element[1]->str, nMasterPort) ;
|
||||
freeReplyObject( replySentinel) ;
|
||||
|
||||
// Effettuo la connessione al master redis
|
||||
s_pRedisContext = redisConnect( sMasterHost.c_str(), nMasterPort) ;
|
||||
if ( s_pRedisContext == nullptr) {
|
||||
LOG_INFO( GetCmdLogger(), "Error : Can't allocate redis context")
|
||||
return false ;
|
||||
}
|
||||
else if ( s_pRedisContext->err != 0) {
|
||||
LOG_INFO( GetCmdLogger(), ( string{ "Error : "} + s_pRedisContext->errstr).c_str())
|
||||
redisFree( s_pRedisContext) ;
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
// Verifico se richiesta autenticazione
|
||||
if ( ! ConnectionInfo.sPassword.empty()) {
|
||||
redisReply* reply = ( redisReply*)redisCommand( s_pRedisContext, "AUTH %s", ConnectionInfo.sPassword.c_str()) ;
|
||||
if ( reply == nullptr || reply->type == REDIS_REPLY_ERROR) {
|
||||
LOG_INFO(GetCmdLogger(), "Error : Authentication failed") ;
|
||||
if ( reply == nullptr)
|
||||
freeReplyObject( reply) ;
|
||||
redisFree( s_pRedisContext) ;
|
||||
return false ;
|
||||
}
|
||||
freeReplyObject( reply) ;
|
||||
}
|
||||
|
||||
// Seleziono il DataBase
|
||||
redisReply* reply = ( redisReply*)redisCommand( s_pRedisContext, "SELECT %d", ConnectionInfo.nDefaultDataBase) ;
|
||||
if ( reply == nullptr) {
|
||||
|
||||
Reference in New Issue
Block a user