diff options
author | zhenwei pi <pizhenwei@bytedance.com> | 2022-07-27 12:18:28 +0800 |
---|---|---|
committer | zhenwei pi <pizhenwei@bytedance.com> | 2022-08-22 15:16:08 +0800 |
commit | 0b27cfe37d2d996cdab92629cfe6d6a39a15b464 (patch) | |
tree | f14eae4b629bc8a64bc747762b76cad430e4aed2 /src/socket.c | |
parent | 45617385e72eb814c6548064835cf19cdf466a5b (diff) | |
download | redis-0b27cfe37d2d996cdab92629cfe6d6a39a15b464.tar.gz |
Introduce .listen into connection type
Introduce listen method into connection type, this allows no hard code
of listen logic. Originally, we initialize server during startup like
this:
if (server.port)
listenToPort(server.port,&server.ipfd);
if (server.tls_port)
listenToPort(server.port,&server.tlsfd);
if (server.unixsocket)
anetUnixServer(...server.unixsocket...);
...
if (createSocketAcceptHandler(&server.ipfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.tlsfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.sofd, acceptTcpHandler) != C_OK)
...
If a new connection type gets supported, we have to add more hard code
to setup listener.
Introduce .listen and refactor listener, and Unix socket supports this.
this allows to setup listener arguments and create listener in a loop.
What's more, '.listen' is defined in connection.h, so we should include
server.h to import 'struct socketFds', but server.h has already include
'connection.h'. To avoid including loop(also to make code reasonable),
define 'struct connListener' in connection.h instead of 'struct socketFds'
in server.h. This leads this commit to get more changes.
There are more fields in 'struct connListener', hence it's possible to
simplify changeBindAddr & applyTLSPort() & updatePort() into a single
logic: update the listener config from the server.xxx, and re-create
the listener.
Because of the new field 'priv' in struct connListener, we expect to pass
this to the accept handler(even it's not used currently), this may be used
in the future.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
Diffstat (limited to 'src/socket.c')
-rw-r--r-- | src/socket.c | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/socket.c b/src/socket.c index 387330f97..83000eaf8 100644 --- a/src/socket.c +++ b/src/socket.c @@ -329,6 +329,10 @@ static int connSocketAddr(connection *conn, char *ip, size_t ip_len, int *port, return C_ERR; } +static int connSocketListen(connListener *listener) { + return listenToPort(listener); +} + static int connSocketBlockingConnect(connection *conn, const char *addr, int port, long long timeout) { int fd = anetTcpNonBlockConnect(NULL,addr,port); if (fd == -1) { @@ -382,6 +386,7 @@ static ConnectionType CT_Socket = { .ae_handler = connSocketEventHandler, .accept_handler = connSocketAcceptHandler, .addr = connSocketAddr, + .listen = connSocketListen, /* create/close connection */ .conn_create = connCreateSocket, |