Introduce connection layer framework
Use connTypeRegister() to register a connection type into redis, and
query connection by connectionByType() via type.
With this change, we can hide TLS specified methods into connection
type:
- void tlsInit(void);
- void tlsCleanup(void);
- int tlsConfigure(redisTLSContextConfig *ctx_config);
- int isTlsConfigured(void);
Merge isTlsConfigured & tlsConfigure, use an argument *reconfigure*
to distinguish:
tlsConfigure(&server.tls_ctx_config)
-> onnTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 1)
isTlsConfigured() && tlsConfigure(&server.tls_ctx_config)
-> connTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 0)
Finally, we can remove USE_OPENSSL from config.c. If redis is built
without TLS, and still run redis with TLS, then redis reports:
# Missing implement of connection type 1
# Failed to configure TLS. Check logs for more info.
The log can be optimised, let's leave it in the future. Maybe we can
use connection type as a string.
Although uninitialized fields of a static struct are zero, we still
set them as NULL explicitly in socket.c, let them clear to read & maintain:
.init = NULL,
.cleanup = NULL,
.configure = NULL,
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 15:09:59 +08:00
|
|
|
/* ==========================================================================
|
|
|
|
* connection.c - connection layer framework
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
* Copyright (C) 2022 zhenwei pi
|
|
|
|
*
|
|
|
|
* 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 "server.h"
|
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
static ConnectionType *connTypes[CONN_TYPE_MAX];
|
|
|
|
|
|
|
|
int connTypeRegister(ConnectionType *ct) {
|
|
|
|
int type = ct->get_type(NULL);
|
|
|
|
|
|
|
|
/* unknown connection type as a fatal error */
|
|
|
|
if (type >= CONN_TYPE_MAX) {
|
|
|
|
serverPanic("Unsupported connection type %d", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connTypes[type] == ct) {
|
|
|
|
serverLog(LL_WARNING, "Connection type %d already registered", type);
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
serverLog(LL_VERBOSE, "Connection type %d registered", type);
|
|
|
|
connTypes[type] = ct;
|
|
|
|
|
|
|
|
if (ct->init) {
|
|
|
|
ct->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int connTypeInitialize() {
|
|
|
|
/* currently socket connection type is necessary */
|
|
|
|
serverAssert(RedisRegisterConnectionTypeSocket() == C_OK);
|
|
|
|
|
2022-07-27 11:53:56 +08:00
|
|
|
/* currently unix socket connection type is necessary */
|
|
|
|
serverAssert(RedisRegisterConnectionTypeUnix() == C_OK);
|
|
|
|
|
Introduce connection layer framework
Use connTypeRegister() to register a connection type into redis, and
query connection by connectionByType() via type.
With this change, we can hide TLS specified methods into connection
type:
- void tlsInit(void);
- void tlsCleanup(void);
- int tlsConfigure(redisTLSContextConfig *ctx_config);
- int isTlsConfigured(void);
Merge isTlsConfigured & tlsConfigure, use an argument *reconfigure*
to distinguish:
tlsConfigure(&server.tls_ctx_config)
-> onnTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 1)
isTlsConfigured() && tlsConfigure(&server.tls_ctx_config)
-> connTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 0)
Finally, we can remove USE_OPENSSL from config.c. If redis is built
without TLS, and still run redis with TLS, then redis reports:
# Missing implement of connection type 1
# Failed to configure TLS. Check logs for more info.
The log can be optimised, let's leave it in the future. Maybe we can
use connection type as a string.
Although uninitialized fields of a static struct are zero, we still
set them as NULL explicitly in socket.c, let them clear to read & maintain:
.init = NULL,
.cleanup = NULL,
.configure = NULL,
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 15:09:59 +08:00
|
|
|
/* may fail if without BUILD_TLS=yes */
|
|
|
|
RedisRegisterConnectionTypeTLS();
|
|
|
|
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionType *connectionByType(int type) {
|
|
|
|
ConnectionType *ct;
|
|
|
|
|
|
|
|
serverAssert(type < CONN_TYPE_MAX);
|
|
|
|
|
|
|
|
ct = connTypes[type];
|
|
|
|
if (!ct) {
|
|
|
|
serverLog(LL_WARNING, "Missing implement of connection type %d", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connTypeCleanup(int type) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
if (ct && ct->cleanup) {
|
|
|
|
ct->cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void connTypeCleanupAll() {
|
|
|
|
int type;
|
|
|
|
|
|
|
|
for (type = 0; type < CONN_TYPE_MAX; type++) {
|
|
|
|
connTypeCleanup(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int connTypeConfigure(int type, void *priv, int reconfigure) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
if (ct && ct->configure) {
|
|
|
|
return ct->configure(priv, reconfigure);
|
|
|
|
}
|
|
|
|
|
|
|
|
return C_ERR;
|
|
|
|
}
|
2022-07-27 10:39:49 +08:00
|
|
|
|
|
|
|
/* walk all the connection types until has pending data */
|
|
|
|
int connTypeHasPendingData(void) {
|
|
|
|
ConnectionType *ct;
|
|
|
|
int type;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (type = 0; type < CONN_TYPE_MAX; type++) {
|
|
|
|
ct = connTypes[type];
|
|
|
|
if (ct && ct->has_pending_data && (ret = ct->has_pending_data())) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk all the connection types and process pending data for each connection type */
|
|
|
|
int connTypeProcessPendingData(void) {
|
|
|
|
ConnectionType *ct;
|
|
|
|
int type;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (type = 0; type < CONN_TYPE_MAX; type++) {
|
|
|
|
ct = connTypes[type];
|
|
|
|
if (ct && ct->process_pending_data) {
|
|
|
|
ret += ct->process_pending_data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2022-06-14 19:17:28 +08:00
|
|
|
|
|
|
|
void *connTypeGetCtx(int type) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
if (ct && ct->get_ctx) {
|
|
|
|
return ct->get_ctx();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *connTypeGetClientCtx(int type) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
if (ct && ct->get_client_ctx) {
|
|
|
|
return ct->get_client_ctx();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-07-27 10:46:31 +08:00
|
|
|
connection *connCreate(int type) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
serverAssert(ct && ct->conn_create);
|
|
|
|
|
|
|
|
return ct->conn_create();
|
|
|
|
}
|
|
|
|
|
|
|
|
connection *connCreateAccepted(int type, int fd, void *priv) {
|
|
|
|
ConnectionType *ct = connectionByType(type);
|
|
|
|
|
|
|
|
serverAssert(ct && ct->conn_create_accepted);
|
|
|
|
|
|
|
|
return ct->conn_create_accepted(fd, priv);
|
|
|
|
}
|