众所周知,SO_REUSEPORT 允许多个套接字监听相同的 IP 地址和端口组合,它使每秒请求数增加2 到 3 倍,并减少延迟(~30%) 和延迟的标准差(8 次):https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
NGINX release 1.9.1 introduces a new feature that enables use of the SO_REUSEPORT socket option, which is available in newer versions of many operating systems, including DragonFly BSD and Linux (kernel version 3.9 and later). This socket option allows multiple sockets to listen on the same IP address and port combination. The kernel then load balances incoming connections across the sockets. ...
As shown in the figure, reuseport increases requests per second by 2 to 3 times, and reduces both latency and the standard deviation for latency.
SO_REUSEPORT 在大多数现代操作系统上可用:Linux(kernel >= 3.9 自 29 Apr 2013 )、Free/Open/NetBSD、MacOS、iOS/watchOS/tvOS, IBM AIX 7.2 , Oracle Solaris 11.1 , Windows(只有 SO_REUSEPORT 在 BSD 中表现为 2 个标志一起 SO_REUSEPORT+SO_REUSEADDR),并且可能在 Android 上: https://stackoverflow.com/a/14388707/1558037
Linux >= 3.9
- Additionally the kernel performs some "special magic" for
SO_REUSEPORTsockets that isn't found in other operating systems: For UDP sockets, it tries to distribute datagrams evenly, for TCP listening sockets, it tries to distribute incoming connect requests (those accepted by callingaccept()) evenly across all the sockets that share the same address and port combination. Thus an application can easily open the same port in multiple child processes and then useSO_REUSEPORTto get a very inexpensive load balancing.
众所周知,为了避免自旋锁的锁并实现高性能,不应有读取超过 1 个线程的套接字。 IE。每个线程都应该处理自己的套接字以进行读/写。
accept() 是相同套接字描述符的线程安全函数,因此它应该由锁保护 - 因此锁争用会降低性能:http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2007-06/msg00246.html POSIX.1-2001/SUSv3 requires accept(), bind(), connect(), listen(), socket(), send(), recv(), etc. to be thread-safe functions. It's possible that there are some ambiguities in the standard regarding their interaction with threads, but the intention is that their behaviour in multithreaded programs is governed by the standard.
The receiving performance is down compared to a single threaded program. That's caused by a lock contention on the UDP receive buffer side. Since both threads are using the same socket descriptor, they spend a disproportionate amount of time fighting for a lock around the UDP receive buffer. This paper describes the problem in more detail.
V. K ERNEL ISOLATION
....
From the other side, when the application tries to read data from the socket, it executes a similar process, which isdescribed below and represented in Figure 3 from right to left:
1) Dequeue one or more packets from the receive queue, using the corresponding spinlock (green one).
2) Copy the information to user-space memory.
3) Release the memory used by the packet. This potentiallychanges the state of the socket, so two ways of locking the socket can occur: fast and slow. In both cases, the packet is unlinked from the socket, Memory Accounting statistics are updated and socket is released according to the locking path taken.
即当许多线程访问同一个套接字时,性能会因等待一个自旋锁而下降。
我们有 2 个 Xeon 32 HT-Cores 服务器,共有 64 个 HT-cores,两个 10 Gbit 以太网卡和 Linux(内核 3.9)。
我们使用 RFS 和 XPS - 即在与应用程序线程(用户空间)相同的 CPU 核心上处理相同的 TCP/IP 堆栈(内核空间)连接。
至少有 3 种方法接受连接以在多个线程中处理它:
ip:port,在每个线程中有 1 个单独的接受器套接字,以及接收连接然后处理它(接收/发送)如果我们接受大量新的 TCP 连接,什么是更有效的方法?
最佳答案
在生产中不得不处理这样的情况,这里有一个解决这个问题的好方法:
首先,设置一个线程来处理所有传入的连接。修改亲和图,使该线程拥有专用核心,应用程序(甚至整个系统)中的其他线程都不会尝试访问该核心。 You can also modify your boot scripts so that certain cores are never automatically assigned to an execution unit unless that specific core is explicitly requested (i.e. isolcpus kernel boot parameters).
将该核心标记为未使用,and then explicitly request it in your code for the "listen to socket" thread via cpuset.
接下来,设置一个优先写入操作的队列(最好是优先级队列)(i.e. "the second readers-writers problem).现在,设置您认为合理的工作线程。
此时,“传入连接”线程的目标应该是:
accept() 传入连接。accept()状态。这将使您能够尽快委派传入的连接。您的工作线程可以在项目到达时从共享队列中获取项目。也可能有第二个高优先级线程从该队列中获取数据,并将其移动到辅助队列,从而避免“监听套接字”线程不得不花费额外的周期来委派客户端 FD。
这也将防止“监听套接字”线程和工作线程不得不同时访问同一个队列,这将使您免受最坏情况的影响,例如慢速工作线程在“监听”时锁定队列套接字”线程想要将数据放入其中。即
Incoming client connections
||
|| Listener thread - accept() connection.
\/
Listener/Helper queue
||
|| Helper thread
\/
Shared Worker queue
||
|| Worker thread #n
\/
Worker-specific memory space. read() from client.
至于您提出的另外两个选项:
Use one acceptor socket shared between many threads, and each thread accept connections and processes it.
凌乱。线程将不得不以某种方式轮流发出 accept() 调用,这样做没有任何好处。您还将有一些额外的排序逻辑来处理哪个线程的“轮到”。
Use many acceptor sockets which listen the same ip:port, 1 individual acceptor socket in each thread, and the thread that receives the connection then processes it (recv/send)
Not the most portable option. I'd avoid it.此外,您可能需要让您的服务器进程使用多进程(即 fork())而不是多线程,具体取决于操作系统、内核版本等。
关于c - 我们应该使用多个接受器套接字来接受大量连接吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45001349/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h