I am learning redis v7.0 source code, a problem has bothered me for a long time. I think there are two kinds of readable events for redis, one is the listening socket which is waiting for the new connections and another is existing connections which are accepted . When the readable event fires, for listening socket we should accept to create a new connection firstly then read the data, for the existing connections , do not need to accept, we only read the data.
I think it should be this:
num = select(...);
for (i=0; i <= num ; ++i)
{
if (FDS[i] == listen_sd) {
anetTcpAccept(...)
} else {
newSock = recv(...);
}
}
But from the source code, I didn't see making such a distinction. All readable events are handled like this:
acceptTcpHandler --> anetTcpAccept to create a new connection.
How does redis distinguish between listening socket and old connections? Or redis does not need such a distinction?
Comment From: sundb
Each new connection will only be accepted once, what about the old connection that needs to concern it? You may mix server sockets and connection words, when a new connection is connecting, the server socket fd will be readable.
Comment From: NoWait126
@sundb My description was a bit ambiguous, but I've edited it.
Comment From: sundb
@NoWait126 The readable callback of listening socket and connections are not the same. listening socket -> connSocketAcceptHandler(socket), tlsAcceptHandler(tls), connUnixAcceptHandler(unix) connections -> connSocketSetReadHandler(socket), connTLSSetReadHandler(tls), connUnixSetReadHandler(unix)
Callbacks are not always the same for each type of fd, so it's not as you say that there is no distinction.
Comment From: NoWait126
@sundb Thanks
The listening sockets are createSocketAcceptHandler() --> aeCreateFileEvent() --> aeApiAddEvent() add to server.el to wait readable event, its callback function is acceptTcpHandler
When a new connection request is recived, acceptTcpHandler() --> createClient() --> connSetReadHandler() --> connSocketSetReadHandler() --> aeCreateFileEvent --> aeApiAddEvent(),its callback function is readQueryFromClient
So their callback function is different.