Some experiences related to Linux socket and epoll

Source: Internet
Author: User
There are not many profound things that are well documented. Although simple, it is easy to get confused if you haven't done any tests.

Int nrecvbuf = 32*1024; // set it to 32 K
Setsockopt (S, sol_socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT ));
1. The preceding statement can be used to set the buffer size. The test proves that the buffer size will be triggered again only when all data sent in a single request is read from the buffer, if data is sent multiple times but not read, when the buffer is not full, the data will not be lost and will be accumulated to the end.
2. If the buffer zone is not full, the epollin event will be received multiple times when the same connection sends data multiple times.
Data sent at a time> data in the socket buffer size will be blocked and sent in multiple times. Therefore, the error can be determined by enlige for cyclic reception.
3. If the buffer zone is full, the newly sent data will not trigger the epoll event (no exception), and each Recv will free up space for the buffer zone, the epollin event can be triggered again only when the buffer is idle.
When the receiving size is 0, the client is disconnected (it is impossible to trigger epollin with 0 data packets),-1 indicates an exception, and errorno determines whether it is a reasonable exception or an exception that needs to be terminated.> 0, not equal to the buffer size, indicates that a single sending ends.
4. If the size of the receiving cache area is temporarily adjusted in the middle and the user space is not fully received in the last time, the data will not be lost and will be accumulated together

To sum up, the system still ensures data integrity. As for stability, no further test is conducted.

New:
5. If the soctet FD monitored by the primary accept is also set to non-blocking, the server model driven by the epoll event alone will be faulty and found at the concurrency pressure, each accept only gets the first one from the system. Therefore, if multiple connections are used to trigger the epollin event of server FD at the same time, the returned event array does not display any loss event, so when we use a tool such as AB, we will find that the last few pieces of information will not be processed each time. The reason is that my current solution is to remove the server FD listener, when a thread is used to block the listener, the accept will successfully process the detection client FD, and then listen to the client events cyclically in the main thread. In this way, the epoll has a low probability of errors in the edge mode, and the test shows that the effect is obvious.
6. Block some signals of Sig, otherwise normal events such as socket interruption will cause the entire service to exit.
7. sendfile (FD, F-> sl-> sendbuffer. infd, (off_t *) & F-> sl-> sendbuffer. offset, size_need); note that the three variables in the sendfile function are the transfer address, and the offset will automatically increase. You do not need to manually increase the offset. Otherwise, file transfer will be lost.
8. misunderstanding of the single-thread epoll Driver Model: I used to think that a single thread cannot handle services with severe network latency such as web servers, but excellent servers such as nginx are both opportunity event-driven models, at first, I was worried about these problems. Later, the test found that when the client socket is set to non-blocking mode, from reading data to parsing the HTTP protocol, data transmission speed is very fast under the epoll drive, and there is no need to use multithreading. My Single-core CPU (Ben San) can reach 10000 page/second, this is far from reaching a number on the public network (more serious network latency), so the data processing capability of a single thread is already high, and multithreading is not required, what's different is that you need to split all the blocked parts in the architecture server. When epoll notifies you that you can read the data, part of the data has actually reached the socket buffer, the data you read and use is copied from the kernel space to the user space. Similarly, writing is the same, therefore, epoll performs similar asynchronous processing on the two latencies. For more complex services, a single thread is sufficient to meet the highest requirement of M Nic. This is the significance of a single thread.
The Web server I built previously did not understand epoll. After being triggered by the edge of epoll, I was afraid of event loss or single thread blocking. So I built a task scheduler with multiple threads, all the received events are pushed to any non-scheduler, and then processed in multiple tasks. I also used the Read and Write schedulers to process them separately, I plan to add a set of schedulers if special time-consuming processing is required in the middle, and use a small number of threads + epoll to achieve high performance. Later I found that some schedulers of read and write are redundant, epoll is originally an event scheduler. It is better to set epoll as a horizontal mode to cache event segments later.
As mentioned above, if there is time-consuming work in the middle, such as database read/write, external resource requests (files, sockets), and so on, these operations cannot be blocked in the main thread, therefore, the task scheduler I designed is useful. epoll is used for the event-driven part that can be handled by epoll, and modular design is used for the intermediate part, using the function pointer to implement the "delegate" function in the face object language can meet different needs and add the task (FD mark) to the Scheduler for multi-thread execution in a loop, if a blocking occurs again in the middle, the User-Defined timer will be added again. After the detection is completed, the user will be added to the scheduler again, so that multiple complex tasks can be divided, it is equivalent to purchasing an epoll-like event drive at the intermediate stage of processing.
9. multi-system compatibility: I think it is better to build a specific system than to build a server that supports multiple operating systems. If you want to migrate and change it again, once multiple systems are taken into account, the complexity of the system will be greatly increased and the performance cannot be optimized. Each system has its own unique optimization options, therefore, I think the migration workload is far less than both.
10 Modular programming, although C still requires some modular design, I have now discovered that almost all the advanced features that can be achieved by almost all relatively desired languages have corresponding solutions in C (except Operator overloading ), it is also a pleasure for all the friends who have learned the advanced face object language to use C instead of the mode. It is easy to maintain and read by themselves.
11. develop a good habit of commenting

 

Reference: http://hi.baidu.com/netpet/blog/item/9a2cf83643355c370b55a9be.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.