diff options
author | zhenwei pi <pizhenwei@bytedance.com> | 2022-08-22 15:09:59 +0800 |
---|---|---|
committer | zhenwei pi <pizhenwei@bytedance.com> | 2022-08-22 15:09:59 +0800 |
commit | 8234a5123d8727a0b72c7b1a2524edd113ffe016 (patch) | |
tree | 1418f61000d9be9a0c7d2d616be02b7cd346ae78 /src/connection.c | |
parent | bff7ecc7864716c14fbb399f19acaee364975b29 (diff) | |
download | redis-8234a5123d8727a0b72c7b1a2524edd113ffe016.tar.gz |
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>
Diffstat (limited to 'src/connection.c')
-rw-r--r-- | src/connection.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/connection.c b/src/connection.c new file mode 100644 index 000000000..958c9b818 --- /dev/null +++ b/src/connection.c @@ -0,0 +1,102 @@ +/* ========================================================================== + * 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); + + /* 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; +} |