简体   繁体   English

linux下的TCP / UDP高性能服务器

[英]TCP/UDP high-performance server under linux

I want to understand some question of high-performance server program which use Multithreading. 我想了解一些使用多线程的高性能服务器程序的问题。

  1. epoll can handle TCP socket listening and socket connect,and i can use epoll_wait in the main thread, if there are any socket connect comming, program can accept connections and recv data in the work thread. epoll可以处理TCP套接字监听和套接字连接,我可以在主线程中使用epoll_wait,如果有任何套接字连接,程序可以接受工作线程中的连接和recv数据。 there is no conflict between work thread. 工作线程之间没有冲突。 am i right? 我对吗?

    udp is an connectionless protocol,can we use recvfrom function in the all of work thread at the same time? udp是一个无连接协议,我们可以同时在所有工作线程中使用recvfrom函数吗? the main thread just use epoll to notice the work thread receive data. 主线程只是使用epoll来注意工作线程接收数据。 (assume that i can process each UDP packet individually). (假设我可以单独处理每个UDP数据包)。

  2. this is a UDP server. 这是一个UDP服务器。 i set it work with non-blocking socket. 我设置它使用非阻塞套接字。 i receive data in the main thread and handle it , then send it to the task queue , if the task queue have data, the program wake up the work thread and lock the task queue ,then get the data from task queue, unlock the task queue so that other work thread can get task from task queue, and then handle it. 我在主线程中接收数据并处理它,然后将其发送到任务队列,如果任务队列有数据,程序唤醒工作线程并锁定任务队列,然后从任务队列中获取数据,解锁任务队列,以便其他工作线程可以从任务队列中获取任务,然后处理它。

    is this a good multithread UDP server?i am not sure. 这是一个很好的多线程UDP服务器吗?我不确定。 is there another design of UDP server with high-performance。 还有另一种具有高性能的UDP服务器设计。

    I am in a puzzle about this questions, thank you very much! 我对这个问题感到困惑,非常感谢!

Your basic approach is right. 你的基本方法是对的。 Start by studying the infamous C10K problem and how it was overcome. 首先研究臭名昭着的C10K问题及其如何克服。 Once you understand the major bottlenecks in various implementations, you need to consider the following as part your design process : 一旦了解了各种实现中的主要瓶颈,就需要将以下内容作为设计过程的一部分:

  • Minimise thread creation/deletion cycles. 最小化线程创建/删除周期。
  • Always choose an "event-based" model over a blocking model. 始终在阻塞模型上选择“基于事件”的模型。

Basically the one-thread-per-request model is preferred for its simplicity of implementation. 基本上, 每个请求的单线程模型因其简单的实现而是优选的。 But it does NOT scale well with the number of concurrent requests. 但它并不能很好地适应并发请求的数量。 When designing systems required to support more than a few 1000 concurrent requests, one prefers the use of sockets over threads. 在设计支持超过几千个并发请求所需的系统时,人们更喜欢在线程上使用套接字。

The optimum number of "worker-threads" to instantiate depends upon : 要实例化的“工作线程”的最佳数量取决于:

  • Load (number of concurrent requests) 加载 (并发请求数)
  • System (CPU, RAM) 系统 (CPU,RAM)

Since this is a very popular problem when implementing web-server designs, you can find several analyses like this one , by simply searching for Apache vs. Nginx . 由于这是实现Web服务器设计时非常流行的问题,因此您可以通过简单地搜索Apache与Nginx来找到类似这样的分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM