Prefacec10k problem raises a question, if 1w clients are connected to the server, what are the good scenarios for intermittently sending messages? One scenario is that Redis is undoubtedly a good example of how each thread handles multiple clients, using asynchronous I/O and a ready-to-notify mechanism
the features of Redis and the c10k of probleMemory database, single-threaded support on a W client connection, high concurrency, single-machine support 10w concurrency, low latency, the majority of LAN latency is less than 3ms.
Redis File Event handlers
four key Components
sockets, I/O multiplexing, file event dispatcher, event handlers. This is a typical reactor design pattern, and Redis does not adopt an existing event-driven library, such as Libev, but instead defines an AE driver. Use Epoll to listen to multiple sockets at the same time, and to associate different handlers with different sockets. When the socket being monitored is ready to connect, request, answer, close, and so on, the corresponding file event will be generated, and the file event handler will call the corresponding handler.
implementation of I/O multiplexing and file event dispatcher
epolli/o Multiplexing Packages
ae_epoll.cCreate Epoll instances and event slots Aeapicreate release Epoll instances and event slots Aeapifree Add or modify concern events aeapiaddevent Delete FD-focused events aeapidelevent get executable events Aeapipoll
ae.h
instance of the file event handlertypedef struct aeeventloop { // currently registered maximum descriptor Int maxfd; /* highest file descriptor currently registered */ // the largest descriptor currently tracked int setsize; /* max number of file descriptors tracked */ // for generating Time events id long long timeEventNextId; // Time of the last Execution time event time_t lasttime; /* used to detect system clock skew */ // Registered file Events aefileevent *events; /* registered events */ // Ready File Events aeFiredEvent *fired; /* Fired events */ // Time Event &nbsSwitches for the p; aetimeevent *timeeventhead; // event processor int stop; // private Data void *apidata; /* for multiplexed libraries This is used for polling API specific data */ // the function aebeforesleepproc *beforesleep to execute before handling the event;} aeEventLoop;
implementation of the file event handler and the file event dispatcher
ae.cInitialize file event handler Aecreateeventloop Delete event handler Aedeleteeventloop Stop event handler aestop Create file event handler aecreatefileevent Delete file event handler Aedeletefileevent get Listener Event Type Aegetfileevents file event dispatcher, call Aepoll to get the activated event, and invoke the event corresponding to the file processor to handle these events aeprocessevents
I/O event processors for Redis
To create a connection handlerAecreatefileevent (Server.el, Server.ipfd[j], ae_readable,accepttcphandler,null)
Create a request handlerAecreatefileevent (Server.el,fd,ae_readable,readqueryfromclient, c)
Create an answer handler that unlocks the association of sockets and events when the command repliesAecreatefileevent (Server.el, C->FD, Ae_writable,sendreplytoclient, c)
event types driven by AEReadable # define ae_readable 1//writable # ae_writable 2 When a client initiates a connection, disconnects, or sends a request, the socket generates a Ae_readable event when the socket becomes writable (when the client invokes the read operation), the socket The Ae_writable event is generated by the socket.
Event processing sequence for AE driversThe AE driver allows simultaneous monitoring of both readable and writable events, while processing a readable event before processing a writable event.
Redis File Event handlers