IOCP , kqueue , epoll ... 有多重要?
设计 mmo 服务器,我听过许多老生常谈,说起处理大量连接时, select 是多么低效。我们应该换用 iocp (windows), kqueue(freebsd), 或是 epoll(linux) 。的确,处理大量的连接的读写,select 是够低效的。因为 kernel 每次都要对 select 传入的一组 socket 号做轮询,那次在上海,以陈榕的说法讲,这叫鬼子进村策略。一遍遍的询问“鬼子进村了吗?”,“鬼子进村了吗?”... 大量的 cpu 时间都耗了进去。(更过分的是在 windows 上,还有个万恶的 64 限制。)
使用 kqueue 这些,变成了派一些个人去站岗,鬼子来了就可以拿到通知,效率自然高了许多。不过最近我在反思,真的需要以这些为基础搭建服务器吗?
刚形成的一个思路是这样的:
我们把处理外部连接和处理游戏逻辑分摊到两个服务器上处理,为了后文容易表述,暂时不太严谨的把前者称为连接服务器,后者叫做逻辑服务器。
连接服务器做的事情可以非常简单,只是把多个连接上的数据汇集到一起。假设同时连接总数不超过 65536 个,我们只需要把每个连接上的数据包加上一个两字节的数据头就可以表识出来。这个连接服务器再通过单个连接和逻辑服务器通讯就够了。
那么连接服务器尽可以用最高效的方式处理数据,它的逻辑却很简单,代码量非常的小。而逻辑服务器只有一个外部连接,无论用什么方式处理都不会慢了。
进一步,我们可以把这个方法扩展开。假定我们逻辑以 10Hz 的频率处理逻辑。我们就让连接服务器以 10Hz 的脉冲把汇总的数据周期性的发送过去,先发一个长度信息再发数据包。即使一个脉冲没有外部数据,也严格保证至少发一个 0 的长度信息。额外的,连接服务器还需要控制每个脉冲的数据总流量,不至于一次发送数据超过逻辑服务器处理的能力。
那么,逻辑服务器甚至可以用阻塞方式调用 recv 收取这些数据,连 select 也省了。至于数据真的是否会被接收方阻塞,就由连接服务器的逻辑保证了。
说到阻塞接收,我跟一个同事讨论的时候,他严重担心这个的可靠性,不希望因为意外把逻辑服务器挂在一个 system call 上。他列举了许多可能发生的意外情况,不过我个人是不太担心的,原因不想在这里多解释。当然我这样设计,主要不是为了节省一个 select 的调用,而是希望方便调试。(当然,如果事实证明这样不可行,修改方案也很容易)
因为阻塞接收可以保证逻辑服务器的严格时序性,当我们把两个服务器中的通讯记录下来,以后可以用这些数据完全重现游戏逻辑的过程,无论怎么调试运行,都可以保证逻辑服务器的行为是可以完全重现的。即,每 0.1s 接受已知的数据包,然后处理它们。
这样做,逻辑服务器对网络层的代码量的需求也大大减少了,可以更专心的构建逻辑。
Comments
Posted by: werd | (55) May 11, 2022 05:15 PM
Posted by: river | (54) October 5, 2016 12:49 PM
Posted by: lynnhan | (53) April 7, 2016 10:34 AM
Posted by: Anonymous | (52) June 22, 2015 11:49 AM
Posted by: 省电兄 | (51) May 28, 2015 02:16 PM
Posted by: chk | (50) March 15, 2015 03:03 AM
Posted by: 菜鸟浮出水 | (49) January 31, 2015 05:33 PM
Posted by: 啥都不懂就老了 | (48) August 6, 2014 10:16 PM
Posted by: DemonHunter | (47) July 20, 2014 03:19 PM
Posted by: DemonHunter | (46) July 20, 2014 03:19 PM
Posted by: 涵曦 | (45) December 10, 2013 04:19 PM
Posted by: luo | (44) April 11, 2013 04:53 PM
Posted by: Anonymous | (43) February 15, 2012 12:17 PM
Posted by: ☆随心の风 | (42) December 8, 2011 10:38 AM
Posted by: CPP | (41) November 24, 2011 03:28 PM
Posted by: 大脚 | (40) November 17, 2011 09:41 PM
Posted by: finalmouse | (39) July 10, 2011 11:20 PM
Posted by: 无趣 | (38) June 27, 2011 10:43 AM
Posted by: 无趣 | (37) June 27, 2011 10:42 AM
Posted by: halida | (36) March 10, 2011 10:46 PM
Posted by: game88 | (35) December 4, 2010 12:47 PM
Posted by: Limlighten | (34) July 29, 2010 05:01 PM
Posted by: suntofly | (33) December 11, 2009 08:48 PM
Posted by: chu | (32) July 25, 2009 04:57 PM
Posted by: redor | (31) November 24, 2008 04:39 PM
Posted by: 游客 | (30) June 26, 2008 04:37 PM
Posted by: iunknown | (29) June 9, 2008 04:37 PM
Posted by: 塞外浪子 | (28) January 17, 2008 03:59 PM
Posted by: youngs | (27) August 8, 2007 11:09 PM
Posted by: Detective | (26) February 8, 2007 02:04 PM
Posted by: Anonymous | (25) October 9, 2006 11:01 AM
Posted by: 卓强 | (24) May 20, 2006 08:26 PM
Posted by: KxjIron | (23) April 26, 2006 11:26 AM
Posted by: code | (22) April 25, 2006 01:44 PM
Posted by: Brink of Wind | (21) April 25, 2006 12:47 PM
Posted by: 卢立祎 | (20) April 24, 2006 10:36 AM
Posted by: sunway | (19) April 24, 2006 08:52 AM
Posted by: Cloud | (18) April 21, 2006 10:25 PM
Posted by: eric119 | (17) April 21, 2006 09:55 PM
Posted by: analyst | (16) April 21, 2006 09:23 PM
Posted by: Cloud | (15) April 21, 2006 01:12 PM
Posted by: mouse | (14) April 21, 2006 10:01 AM
Posted by: tony | (13) April 21, 2006 09:13 AM
Posted by: KxjIron | (12) April 20, 2006 11:22 PM
Posted by: Felix | (11) April 20, 2006 06:21 PM
Posted by: Wesley | (10) April 20, 2006 06:02 PM
Posted by: Cloud | (9) April 20, 2006 02:08 PM
Posted by: Wesley | (8) April 20, 2006 01:16 PM
Posted by: kunkun | (7) April 20, 2006 10:46 AM
Posted by: Felix | (6) April 20, 2006 12:15 AM
Posted by: Atry | (5) April 19, 2006 02:57 PM
Posted by: Atry | (4) April 19, 2006 02:56 PM
Posted by: Atry | (3) April 19, 2006 02:55 PM
Posted by: Atry | (2) April 19, 2006 02:49 PM
Posted by: Atry | (1) April 19, 2006 02:43 PM