From 1234e3a5628260658adfe9065cb58ec5c1cb5ebe Mon Sep 17 00:00:00 2001 From: zhenwei pi Date: Wed, 27 Jul 2022 10:46:31 +0800 Subject: Fully abstract connection type Abstract common interface of connection type, so Redis can hide the implementation and uplayer only calls connection API without macro. uplayer | connection layer / \ socket TLS Currently, for both socket and TLS, all the methods of connection type are declared as static functions. It's possible to build TLS(even socket) as a shared library, and Redis loads it dynamically in the next step. Also add helper function connTypeOfCluster() and connTypeOfReplication() to simplify the code: link->conn = server.tls_cluster ? connCreateTLS() : connCreateSocket(); -> link->conn = connCreate(connTypeOfCluster()); Signed-off-by: zhenwei pi --- src/cluster.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/cluster.c') diff --git a/src/cluster.c b/src/cluster.c index a7d6f6205..b60b79153 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -119,6 +119,14 @@ dictType clusterNodesBlackListDictType = { NULL /* allow to expand */ }; +static int connTypeOfCluster() { + if (server.tls_cluster) { + return CONN_TYPE_TLS; + } + + return CONN_TYPE_SOCKET; +} + /* ----------------------------------------------------------------------------- * Initialization * -------------------------------------------------------------------------- */ @@ -865,6 +873,7 @@ void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) { int cport, cfd; int max = MAX_CLUSTER_ACCEPTS_PER_CALL; char cip[NET_IP_STR_LEN]; + int require_auth = TLS_CLIENT_AUTH_YES; UNUSED(el); UNUSED(mask); UNUSED(privdata); @@ -882,8 +891,7 @@ void clusterAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) { return; } - connection *conn = server.tls_cluster ? - connCreateAcceptedTLS(cfd, TLS_CLIENT_AUTH_YES) : connCreateAcceptedSocket(cfd); + connection *conn = connCreateAccepted(connTypeOfCluster(), cfd, &require_auth); /* Make sure connection is not in an error state */ if (connGetState(conn) != CONN_STATE_ACCEPTING) { @@ -3969,7 +3977,7 @@ static int clusterNodeCronHandleReconnect(clusterNode *node, mstime_t handshake_ if (node->link == NULL) { clusterLink *link = createClusterLink(node); - link->conn = server.tls_cluster ? connCreateTLS() : connCreateSocket(); + link->conn = connCreate(connTypeOfCluster()); connSetPrivateData(link->conn, link); if (connConnect(link->conn, node->ip, node->cport, server.bind_source_addr, clusterLinkConnectHandler) == -1) { @@ -6175,8 +6183,8 @@ migrateCachedSocket* migrateGetSocket(client *c, robj *host, robj *port, long ti dictDelete(server.migrate_cached_sockets,dictGetKey(de)); } - /* Create the socket */ - conn = server.tls_cluster ? connCreateTLS() : connCreateSocket(); + /* Create the connection */ + conn = connCreate(connTypeOfCluster()); if (connBlockingConnect(conn, host->ptr, atoi(port->ptr), timeout) != C_OK) { addReplyError(c,"-IOERR error or timeout connecting to the client"); -- cgit v1.2.1