简体   繁体   English

为Boost ASIO设计,Worker线程对“实用” Web服务器进行SQl查询

[英]Design for Boost ASIO , Worker threads SQl queries for 'practical' web server

I am looking for solution to develop an efficient web server framework where: 我正在寻找解决方案以开发高效的Web服务器框架,其中:

  1. One or few IO threads handle client HTTP connections and TCP IO. 一个或几个IO线程处理客户端HTTP连接和TCP IO。
  2. Multiple threads do business processing (SQL queries, file IO, etc.) 多个线程进行业务处理(SQL查询,文件IO等)

All blogs solution I have seen are solving 10000 connections with worker threads doing almost zero business logic (ie just writing data using async_write ). 我看到的所有博客解决方案都在解决与工作线程建立的10000个连接,这些线程几乎执行零业务逻辑(即仅使用async_write写入数据)。 Is the Boost.Asio's HTTP Server 3 a solution for my problem? Boost.Asio的HTTP Server 3是否可以解决我的问题? If so, how many threads should I use per core? 如果是这样,每个内核应使用多少个线程?

I am also interested in knowing how HTTP Server 3 compares to the 1 acceptor thread + thread pool model used by mongoose . 我也有兴趣知道如何HTTP Server 3的比较所使用的1个受体线程+线程池模型猫鼬

Sorry, I don't have enough reputation to use the comments, so I have to post a new answer to give any input. 抱歉,我没有足够的声誉来使用评论,所以我必须发布一个新答案以提供任何输入。

OP is asking a specific question about http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/http/server3/ . OP正在询问有关http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/http/server3/的特定问题。 This is an example http server that starts a thread pool to handle the http requests. 这是一个示例http服务器,它启动一个线程池来处理http请求。

OP's original question might be restated: "given a server machine with a set of resources A and a workload that consumes B resources per request, how many threads should I allocate in the thread pool?" OP的原始问题可能会被重申:“如果一台服务器计算机具有一组资源A和每个请求消耗B资源的工作负载,那么我应该在线程池中分配多少个线程?”

There's a thread here: Reasonable number of threads for thread pool running web service requests with a similar discussion (with respect to Java thread pools) but that discussion doesn't seem to come to any conclusive answer. 这里有一个线程: 用于运行Web服务请求的线程池的线程数量合理,讨论类似(关于Java线程池),但是该讨论似乎没有任何结论性的答案。

Here's an example of a short tutorial on capacity planning in the "old fashioned 1970's mainframe style" that I learned in school: http://www.cs.umb.edu/~eb/goalmode/primer.pdf . 这是我在学校中学到的有关以“ 1970年代老式大型机样式”进行容量规划的简短教程的示例: http : //www.cs.umb.edu/~eb/goalmode/primer.pdf

In this case you might create a simple model like: 在这种情况下,您可以创建一个简单的模型,例如:

You have an average rate of requests arriving, X. For each request you are consuming a certain average amount of cpu (in units of time) S_c, and an average amount of time spent waiting for disk requests to complete, S_d. 您有一个平均的请求到达率X。对于每个请求,您消耗的CPU平均数量(以时间为单位)为S_c,而等待磁盘请求完成的平均时间为S_d。 So each thread is taking an average time S_c + S_d before it is returned to the thread pool. 因此,每个线程在返回到线程池之前要花费平均时间S_c + S_d。 (You would need to measure this.) Thus on average you would expect that you would need at least N = X * (S_c+S_d) threads to avoid incoming threads queueing at an empty thread pool. (您需要对此进行度量。)因此,平均而言,您期望至少需要N = X *(S_c + S_d)个线程,以避免传入线程在空线程池中排队。 You might actually want to allocate some small multiple of N (eg 3N) threads to be able to deal with bursts of one kind or another. 您实际上可能希望分配N(例如3N)个线程的较小倍数,以便能够处理一种或另一种突发。

But the number of threads in the pool isn't really the interesting limit. 但是池中的线程数并不是真正有趣的限制。 The interesting limit is either the total amount of CPU or the total amount of disk bandwidth you have available. 有趣的限制是您可以使用的CPU总量或磁盘带宽总量。 Suppose each request requires 3 seconds of CPU processing, and that you have a system with 12 cores. 假设每个请求需要3秒的CPU处理时间,并且您的系统具有12个核心。 Thus, in any 3 second period you should expect to handle 12 simultaneous requests. 因此,在任何3秒钟的时间内,您应该期望同时处理12个请求。 Thus an average arrival rate greater than 12/3 = 4 requests per second would saturate your CPU. 因此,平均到达速率大于每秒12/3 = 4个请求将使您的CPU饱和。 (Similar calculation for your disk bandwidth.) (对磁盘带宽的类似计算。)

So really what you are going to end up figuring out is: given my expected arrival rate of requests, X, and the amount of CPU and disk consumed by each request, how much cpu and disk should I purchase? 因此,您最终要弄清楚的是:给定我的请求的预期到达率X,以及每个请求消耗的CPU和磁盘数量, 我应该购买多少cpu和磁盘?

After studying most of options , I have nearly come to conclusion 在研究了大多数选项之后,我几乎得出结论了
A] One thread for epoll A] epoll的一个线程
B] Pool for business logic B]业务逻辑池
C] Queue between IO thread & pool C] IO线程和池之间的队列
D] pipe between IO thread & pool to wake IO thread for write data D] IO线程和池之间的管道,以唤醒IO线程以写入数据
E] accept & client read & write be done by IO thread E]接受和客户端读写由IO线程完成
And I guess ngix & lighthttpd follow same ? 我猜ngix和lighthttpd遵循相同吗?

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

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