# Architecture # redesign the SOCKS5 Proxy code framework

Source: Internet
Author: User

I have previously written the SOCKS5 proxy. For details, please read this article.

Write anyCodeBefore the code, I always thought that only after the corresponding data structure is designed can the data be well written.AlgorithmAnd framework. From the previous code, the data structure definition of the SOCKS5 protocol is good and readable. As mentioned before, the previous version is too simple. Although it is based on multiple threads, the performance is not very good, because we all know that to create a thread for each request, additional system resources are required. If there are many request requests, too many threads may be opened up, resulting in resource depletion and downtime.

In addition, the previous version does not properly process the TCP status. In Linux, The netstat status shows that some socket sockets are in the close_wait status, which means that when no data is sent, the server did not take the initiative to close the socket.

Today, I re-designed the SOCKS5 server framework.

The main design ideas are as follows:

Maintain three Queues: wait_queue, ready_queue, and read_queue, which are used to store the socket for processing the select function and the socket for receiving data respectively;

In thread A, there is a loop that constantly accepts requests from the client and pushes the generated socket into wait_queue;

In thread B, there is also a loop that constantly select or epoll the socket in ready_queue, and then press the data-readable socket (to see if the other side has closed) into read_queue;

In thread group C, you can use multiple threads to process read_queue. The thread group size can be set,ProgramThe corresponding number of threads will be opened during the runtime, so that no threads will be opened for each request in the future;

Among them, thread B and thread group C are the production and consumption model; that is, when read_queue is not 0, the thread group will process data; When read_queue is 0, thread B will update the value of read_queue again.

Note that many data synchronization problems are involved, so mutex is required, and pthread_cond_t is required for the production and consumption model.

Come on, let's verify the performance of this framework, so stay tuned.

Finally, I wrote this framework in Python. Please refer to my GitHub lab and further describe this framework later.

 

----------------- Create high-qualityArticleFor more information, thank you ---------------

To create high-quality articles, please like one .... Thank you. I will write more good articles.

Sina Weibo: http://weibo.com/baiyang26

The wine ghost official blog: http://www.ibaiyang.org [recommended to use Google Reader subscription]

Pour wine into the official Douban: http://www.douban.com/people/baiyang26/

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.