<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>C100k on 菜菜博士</title>
    <link>https://microcai.org/tags/c100k.html</link>
    <description>Recent content in C100k on 菜菜博士</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Thu, 25 Oct 2018 00:00:00 +0800</lastBuildDate>
    <atom:link href="https://microcai.org/tags/c100k/feed.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>高并发服务器的静态内存分配策略</title>
      <link>https://microcai.org/2018/10/25/static-memory-allocation-made-server-fast.html</link>
      <pubDate>Thu, 25 Oct 2018 00:00:00 +0800</pubDate>
      <guid>https://microcai.org/2018/10/25/static-memory-allocation-made-server-fast.html</guid>
      <description>&lt;p&gt;asio 的 async_accept 函数在高并发的时候，会发生遗漏 accept 的情况。根本原因在于，async_accept 回调的时候，已经 监听的socket已经没有执行 accept 操作了。
解决的办法就是投递多个 async_accept. asio 的 async_* 系列 函数通常只能投递一次，多次投递会发生未定义行为。async_accept 是为数不多的例外。&lt;/p&gt;
&lt;p&gt;通常在写一个 server 的时候，client 是采取 accept 一个就 make_shared 一个的做法。对于大量频繁的连接请求，make_shared 很快就会成为新的瓶颈。
在读 beast 的 example/http/fast_server 的时候，发现了 beast 对这个问题的解决方案，非常的聪明。&lt;/p&gt;
&lt;p&gt;beast 的做法就是，固定的 client 对象。每个 client 自己投递 async_accept 请求，把自己的 socket_ 投递给 async_accept。
async_accept 返回后， client 就对自己的 socket_ 执行处理， 该 read read，该 write 就 write。
直到这个 client 的请求全部处理完毕，连接也 shutdown 后， 它就再次投递 async_accept 进入下一个循环。&lt;/p&gt;
&lt;p&gt;在这个模式下，client 对象并没有被不断的 new 出来。 client 对象的数目是固定的。他利用了 async_accept 可以多次投递的 feature。
在超大量的请求下，该模式始终不会产生频繁的内存分配和释放请求，不仅仅加快了速度，而且也极大的避免了内存碎片。&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
