Redis file event and time event

A: file event

1: File event handlers

Redis based Reactor pattern has developed its own network time Processor: This processor is called the document an event handler.

1: File IO multiplexing using event handlers to simultaneously monitor multiple sockets, and according to the task currently performed by the socket to socket associated with different event handlers

While being monitored before the socket connection is ready to execute a response, read, write, shut down other operations, and the operation should document the event will have a relative, then file event handler is called socket: 2 good event associated processor to handle these events.

 

File event the processor has four components, which are sockets, I / O multiplexing program, file event and the event dispatcher processor

File event is an abstract socket operation, whenever a socket is ready for execution accept, read, write and close other operations, it will generate a file event. Redis because usually connect multiple sockets, multiple files concurrent events likely to occur.

I / O multiplexing program responsible for monitoring multiple sockets, socket and transfer those files generated event event dispatcher.

I / O multiplexing program will always produce all the sockets are placed in the same queue (that is, the aeEventLoop described later fired ready event table) inside, then file event handler in an orderly, synchronous, single socket handles the queue socket, which is handling the event-ready files.

File event dispatcher program receives the transmitted multiplexed IO socket, and the socket according to the type of the generated event, call the corresponding event handler. The event is a functional processor that defines the operation when an event occurs, the server should perform.

 

2: Type of file events

1: When the socket becomes readable (client performs write operations on a socket, or close operation), or a new response available sockets, socket generating event AE_READBLE

2: When the client performs read operation on a socket, the socket becomes writable socket generate AE_WRITABLE event

If a socket while producing these two events, file event distributor will give priority to AE_READBLE event, dealt with before dealing with AE_WRITABLE event

 

3: The kind of event handlers file

Answer processor connection

networking.c / acceptTcpHandler Redis connection response function is a processor, the processor is configured to connect the server to the client listening socket to respond, embodied as sys / socket.h Packaging / accept function. When Redis server initialization, the program will answer this connection processor and server listening socket AE_READABLE event associate, when there is a client listening socket with sys / socket.h / connect function when connecting to the server, sets sockets AE_READABLE event is generated, causing the processor to perform connection response, and the response operation performs a corresponding socket

Command requesting processor

networking.c / readQueryFromClient Redis command requests function is a processor, the command processor reads the client is responsible for sending the requested content from the socket, embodied as packaging unistd.h / read function.

When answering a client by connecting the processor to successfully connect to the server, the server sends the client socket request command processor AE_READABLE events and associate, when a client sends a request command to the server, the socket will generating AE_READABLE event trigger command request processor, and performs a corresponding socket read operation. Throughout the process the client connection to the server, the server would have been a client socket AE_READABLE event correlation request command processor.

Reply command processor

networking.c / sendReplyToClient function is Redis commands reply processor, this processor is responsible for the server to get the commands have been executed command reply back to the client through the socket, embodied as unistd.h packaging / write functions.

When the server needs to have command reply to the client, the server sends the client socket AE_WRITABLE events and commands reply processor associate, when the client is ready to receive server returns the command reply, it will have AE_WRITABLE event initiator command reply processor, and performs a write operation corresponding socket. When the command reply has been sent, the server will respond to commands to lift an association between AE_WRITABLE event handler with the client socket.

A complete client-server connection event Example

Let's trace a Redis client and server connection and send commands the entire process to see what events in the process will produce, and these events is how they are handled.

Suppose a Redis server is in operation, then the listening socket of the server AE_READABLE event should be living under a listening state, and the event processor corresponding to the connection acknowledgment processor.

If then there is a Redis client initiates a connection to the server, then the listening socket will produce AE_READABLE events trigger responses processor executes connection: connection request processor client will respond, and then create a client socket, and client state, and the client socket AE_READABLE event associated with the command request processor, so that the client may send a command request to the primary server.

Thereafter, assuming the client sends a command request to the primary server, the client socket AE_READABLE generated event trigger command execution requesting processor, the processor reads the contents of the command of the client, and then passed to the execution procedures.

Execute commands will generate a corresponding command reply, in order to respond to the commands sent back to the client, server sends the client socket AE_WRITABLE events associated with the command reply processor: When a client attempts to read command response, and customers end socket will produce AE_WRITABLE event, triggering command reply processor, the processor will command when the command reply after reply all written to the socket, the server will be lifted client socket AE_WRITABLE events and reply command processor the association between.

 

II: Time Event

Redis time events are divided into two categories:

Timed Events: make a program executed once after a specified time.

Periodic time, so that a program is executed once every specified time.

Time Event main attributes:

when: format to UNIX timestamp in milliseconds units, recording a time of arrival time of the event.

timeProc: Time event handler, a function. When the time arrives event will be called.

id: server globally unique ID to create the time of the event, from small to large ascending order. The new time is larger than the ID number of the ID number of the old event.

next: Time points to the next event, disordered chain structure, which is not reflected in the disordered sorted by time of arrival. New time event is inserted in the header of the position of the list

 

A return value of the timing event is a time event or time-dependent periodic event processor:

If the return value is AE_NOMORE, then this event is a timed event that deleted after reached after will not be repeated.

If the return value is non-AE_NOMORE, then the event is a recurring event, a time when the event arrives, the server will be based on the return value of processor time, when the properties of the time of the event to be updated, so this event again after a period of time achieve.

Redis all time events are placed in an unordered list, each Redis will traverse the entire list, look for all the time the event has been reached, and calls the appropriate event handler.

 

Commonly used API:

1, aeCreateTimeEvent: add a new event to the list header

2, aeDeleteTimeEvent: to delete the time of the event given id

3, aeSearchNearestTimer: look for the most recent time from the current time event

4, processTimeEvents: deal with all the time the event has arrived

 

Published 50 original articles · won praise 2 · Views 2270

Guess you like

Origin blog.csdn.net/eafun_888/article/details/104714298
Recommended