{"id":956,"date":"2020-02-01T20:02:24","date_gmt":"2020-02-01T12:02:24","guid":{"rendered":"http:\/\/www.shuran.cn\/?p=956"},"modified":"2020-02-01T20:02:24","modified_gmt":"2020-02-01T12:02:24","slug":"nextcloud-%e7%9a%84webdav%e6%8b%b7%e8%b4%9d%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.shuran.cn\/?p=956","title":{"rendered":"Nextcloud \u7684webdav\u62f7\u8d1d\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u539f\u6587\uff1a\u4f18\u5316 PHP \u5927\u6587\u4ef6\u4e0b\u8f7d\u901f\u5ea6\u81f3\u4e07\u5146\uff0c\u8ba9 Nextcloud \u652f\u6301\u4e07\u5146\u7f51\u7edc<\/p><cite> <a href=\"https:\/\/www.v2ex.com\/amp\/t\/555144\/5\">https:\/\/www.v2ex.com\/amp\/t\/555144\/5<\/a> <\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u6700\u8fd1\u5728 HP Microserver Gen8 \u4e0a\u91cd\u65b0\u642d\u5efa\u4e86 Nextcloud \uff08\u5728\u865a\u62df\u673a\u91cc\u9762\u5bb9\u5668\u91cc\uff0c\u57fa\u4e8e PHP 7.2 \uff09\uff0c\u53ef\u60dc\u901a\u8fc7 virtio \u865a\u62df\u4e07\u5146\u7f51\u7edc\u8fdb\u884c\u4e0b\u8f7d\uff0cSSD \u4e0a\u6587\u4ef6\u7684\u4e0b\u8f7d\u901f\u5ea6\u4e0d\u8d85\u8fc7 260MiB\/s\uff0c\u673a\u68b0\u786c\u76d8\u4e0a\u6587\u4ef6\u7684\u4e0b\u8f7d\u901f\u5ea6\u4e0d\u8d85\u8fc7 80MB\/s\u3002\u8981\u77e5\u9053\u76f4\u63a5\u672c\u5730\u8bbf\u95ee\u65f6\uff0cSSD \u80fd\u8fbe\u5230 550MB\/s \u5de6\u53f3\uff0c\u673a\u68b0\u786c\u76d8\u5e73\u5747 130MB\/s\uff0c\u4e0d\u7518\u5fc3\uff08\u8fd9\u65f6 PHP \u8fdb\u7a0b\u7684 CPU \u5360\u7528\u7387\u5f88\u4f4e\uff0c\u8bf4\u660e\u6839\u672c\u6ca1\u6709\u8fbe\u5230 CPU \u6267\u884c\u74f6\u9888\uff09\u3002<\/p>\n\n\n\n<p>\u6392\u9664\u4e86\u7f51\u7edc\u95ee\u9898\u540e\uff0c\u6211\u5728\u5b58\u50a8\u76ee\u5f55\u4e0a\u642d\u5efa\u4e86\u4e00\u4e2a Nginx \u8fdb\u884c\u6d4b\u8bd5\uff0c\u53d1\u73b0\u901a\u8fc7 Ngninx \u76f4\u63a5\u4e0b\u8f7d\u6587\u4ef6\u51e0\u4e4e\u80fd\u8fbe\u5230\u672c\u5730\u76f4\u63a5\u8bbf\u95ee\u7684\u6027\u80fd\u3002\u4e8e\u662f\uff0c\u4e0b\u8f7d\u901f\u5ea6\u6162\u7684\u9505\u5c31\u843d\u5728\u4e86 PHP \u7684\u6027\u80fd\u4e0a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8c03\u7814<\/h2>\n\n\n\n<p>\u7ecf\u8fc7\u4e00\u756a\u8c03\u7814\uff0cNextcloud \u7684 WebDav \u670d\u52a1\u662f\u57fa\u4e8e sabre\/dav \u7684\u6846\u67b6\u5f00\u53d1\u7684\u3002\u4e8e\u662f\u627e\u5230\u4e86 sabre\/dav \u7684\u6e90\u7801\uff0c\u6700\u540e\u5b9a\u4f4d\u5230\u4e0b\u8f7d\u6587\u4ef6\u4ee3\u7801\u7684\u4f4d\u7f6e\uff1a3rdparty\/sabre\/http\/lib\/Sapi.php \u3002\u539f\u6765 sabre\/dav \u662f\u901a\u8fc7\u8c03\u7528&nbsp;<code>stream_copy_to_stream<\/code>&nbsp;\u5c06\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\u62f7\u8d1d\u5230 HTTP \u8f93\u51fa\u7684\uff1a\u76f4\u63a5\u628a\u6587\u4ef6\u6d41\u548c PHP \u7684\u8f93\u51fa\u6d41\u8fdb\u884c\u5bf9\u62f7\uff0c\u4e4b\u524d\u5e76\u6ca1\u6709\u5176\u4ed6\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u8bf4\u660e\u74f6\u9888\u5c31\u5728\u8fd9\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ 3rdparty\/sabre\/http\/lib\/Sapi.php\n\/\/ ...\n if (is_resource($body) &amp;&amp; 'stream' == get_resource_type($body)) {\n                if (PHP_INT_SIZE !== 4) {\n                    \/\/ use the dedicated function on 64 Bit systems\n                    stream_copy_to_stream($body, $output, (int) $contentLength);\n                } else {\n\/\/ ...\n<\/code><\/pre>\n\n\n\n<p>\u6211\u672c\u4eba\u5e76\u4e0d\u662f PHP \u7a0b\u5e8f\u5458\uff0c\u4e8e\u662f\u5f00\u59cb\u4e86\u6f2b\u957f\u7684\u641c\u7d22\u3002Google \u5a18\u544a\u8bc9\u6211 PHP \u4e13\u95e8\u63d0\u4f9b\u4e86<code>fpassthru<\/code>\u51fd\u6570\u63d0\u4f9b\u9ad8\u6027\u80fd\u6587\u4ef6\u4e0b\u8f7d\uff0c\u4e8e\u662f\u6211\u4fee\u6539\u4ee3\u7801\u628a stream_copy_to_stream \u6362\u6210\u4e86<code>fpassthru<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ 3rdparty\/sabre\/http\/lib\/Sapi.php\n\/\/ ...\n if (is_resource($body) &amp;&amp; 'stream' == get_resource_type($body)) {\n                if (PHP_INT_SIZE !== 4) {\n                    \/\/ use the dedicated function on 64 Bit systems\n                    \/\/ stream_copy_to_stream($body, $output, (int) $contentLength);\n                    fpassthru($body); \/\/ \u6539\u52a8\u8fd9\u4e00\u884c\n                } else {\n\/\/ ...\n<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u4e0b\u8f7d\u901f\u5ea6\u76f4\u63a5\u6253\u4e86\u9e21\u8840\uff0c440-470 MiB\/s\u3002\u53ef\u60dc&nbsp;<code>fpassthru<\/code>&nbsp;\u53ea\u80fd\u628a\u6587\u4ef6\u8f93\u51fa\u5230\u7ed3\u5c3e\uff0c\u4e0d\u80fd\u53ea\u8f93\u51fa\u6587\u4ef6\u7684\u4e00\u90e8\u5206\uff08\u4e3a\u4e86\u652f\u6301\u65ad\u70b9\u7eed\u4f20\u548c\u5206\u7247\u4e0b\u8f7d\uff09\u3002\u53e6\u5916\u7ffb\u4e86\u4e00\u4e0b sabre\/dav \u7684 issues\uff0c\u53d1\u73b0 sabre\/dav \u4e0d\u7528<code>fpassthru<\/code>\u7684\u53e6\u5916\u4e00\u4e2a\u539f\u56e0\u662f\u6709\u4e9b\u7248\u672c\u7684 PHP \u4e2d<code>fpassthru<\/code>\u51fd\u6570\u5b58\u5728 BUG\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ee7\u7eed\u6df1\u5165<\/h2>\n\n\n\n<p>\u90a3\u4e3a\u4ec0\u4e48<code>stream_copy_to_stream<\/code>\u901f\u5ea6\u548c<code>fpassthru<\/code>\u5dee\u8ddd\u5927\u5f97\u4e0d\u79d1\u5b66\u5462\uff1f\u53ea\u80fd\u53bb\u8bfb PHP \u7684\u6e90\u7801\u4e86\uff0c\u5e78\u597d C \u8bed\u8a00\u662f\u6211\u7684\u5f3a\u9879\u3002 \u6211\u53d1\u73b0\uff0c<code>fpassthru<\/code>\u51fd\u6570\u548c<code>stream_copy_to_stream<\/code>\u51fd\u6570\u5b9e\u73b0\u662f\u53ca\u5176\u7c7b\u4f3c\u7684\uff1a\u5148\u5c1d\u8bd5\u628a\u6e90\u6587\u4ef6\u521b\u5efa\u4e3a\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08\u901a\u8fc7\u8c03\u7528 mmap \uff09\uff0c\u5982\u679c\u6210\u529f\u5219\u76f4\u63a5\u4ece\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u62f7\u8d1d\u5230\u76ee\u7684\u6d41\uff0c\u5426\u5219\u5c31\u8bfb\u5230\u5185\u5b58\u4e2d\u8fdb\u884c\u4f20\u7edf\u7684\u624b\u52a8\u62f7\u8d1d\u3002\u5dee\u522b\u6765\u4e86\uff0c<code>stream_copy_to_stream<\/code>\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u8981\u62f7\u8d1d\u7684\u5b57\u8282\u6570\uff0c\u53ef\u60dc\u5982\u679c\u8fd9\u4e2a\u503c\u5927\u4e8e 4MiB\uff0cPHP \u5c31\u62d2\u7edd\u521b\u5efa\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff0c\u76f4\u63a5\u56de\u9000\u5230\u4f20\u7edf\u62f7\u8d1d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u5728\u5faa\u73af\u4e2d\u8c03\u7528<code>stream_copy_to_stream<\/code>\uff0c\u6bcf\u6b21\u6700\u591a\u62f7 4MiB\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ 3rdparty\/sabre\/http\/lib\/Sapi.php\n\/\/ ...\n if (is_resource($body) &amp;&amp; 'stream' == get_resource_type($body)) {\n                if (PHP_INT_SIZE !== 4) {\n                    \/\/ use the dedicated function on 64 Bit systems\n                    \/\/ \u4e0b\u9762\u662f\u6539\u52a8\u7684\u90e8\u5206\uff1a\n                    \/\/ allow PHP to use mmap by copying in 4MiB chunks\n                    $chunk_size = 4 * 1024 * 1024;\n                    stream_set_chunk_size($output, $chunk_size);\n\t\t    $left = $contentLength;\n\t\t    while ($left > 0) {\n\t\t        $left -= stream_copy_to_stream($body, $output, min($left, $chunk_size));\n\t\t    }\n               } else {\n \/\/ ...\n<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u4e86\u4e00\u4e0b\uff0c\u7ed3\u679c\u4ee4\u4eba\u9707\u60ca\uff1a\u4e0b\u8f7d\u901f\u5ea6\u51e0\u4e4e\u548c\u672c\u5730\u8bfb\u53d6\u65e0\u5f02\u4e86\uff1aSSD \u6587\u4ef6\u7684\u4e0b\u8f7d\u901f\u5ea6\u8d85\u8fc7\u4e86 500 MB\/s\uff0c\u751a\u81f3\u8d85\u8fc7\u4e86&nbsp;<code>fpassthru<\/code>&nbsp;\u7684\u901f\u5ea6(\u5927\u6982\u662f\u56e0\u4e3a\u7f13\u51b2\u533a\u5f00\u7684\u6bd4<code>fpassthru<\/code>\u5927)\u3002<\/p>\n\n\n\n<p>\u6211\u53c8\u8bd5\u7740\u521b\u5efa\u4e86\u4e00\u4e2a 10G \u5927\u5c0f\u7684 sparse \u6587\u4ef6 \uff08 truncate -s 10G 10G.bin \uff09\uff0cLinux \u5728\u8bfb\u53d6 sparse \u6587\u4ef6\u65f6\u53ef\u4ee5\u7acb\u5373\u5b8c\u6210\uff0c\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u5982\u679c\u786c\u76d8\u901f\u5ea6\u8db3\u591f\u5feb\u7684\u60c5\u51b5\u3002\u7ee7\u7eed\u6d4b\u8bd5\uff0c\u53d1\u73b0\u4e0b\u8f7d\u901f\u5ea6\u8d85\u8fc7\u4e86 700MiB\/s\uff0c\u5df2\u7ecf\u63a5\u8fd1\u4e07\u5146\u7f51\u7edc\u7684\u4f20\u8f93\u6781\u9650\u3002\u8fd9\u65f6 PHP \u8fdb\u7a0b\u7684 CPU \u5360\u7528\u7387\u5df2\u7ecf\u8fbe\u5230 100%\uff0c\u8bf4\u660e\u74f6\u9888\u5728 CPU \u6027\u80fd\u4e0a\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>\u7528<code>stream_copy_to_stream<\/code>&nbsp;\u62f7\u8d1d\u6d41\u65f6\uff0c\u5982\u679c source \u662f\u6587\u4ef6\u5e76\u4e14\u6bcf\u6b21\u62f7\u8d1d\u5c0f\u4e8e 4MiB\uff0cPHP \u4f1a\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u5bf9\u62f7\u8d1d\u8fdb\u884c\u52a0\u901f\u3002\u8d85\u8fc7 4MiB \u540e\u5c31\u4f1a\u56de\u9000\u5230\u4f20\u7edf\u8bfb\u53d6\u673a\u5236\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u540e\u7eed<\/h2>\n\n\n\n<p>\u5411 Sabre \u9879\u76ee\u63d0\u4e86 PR\uff1a<a href=\"https:\/\/github.com\/sabre-io\/http\/pull\/119\">https:\/\/github.com\/sabre-io\/http\/pull\/119<\/a>\u3002\u5982\u679c\u5404\u4f4d\u4e5f\u5728\u73a9 Nextcloud \u5e76\u4e14\u9047\u5230\u4e86\u4e0b\u8f7d\u901f\u5ea6\u74f6\u9888\uff0c\u53ef\u4ee5\u8bd5\u7740\u6253\u4e00\u4e0b\u6211\u8fd9\u4e2a\u8865\u4e01\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1a\u4f18\u5316 PHP \u5927\u6587\u4ef6\u4e0b\u8f7d\u901f\u5ea6\u81f3\u4e07\u5146\uff0c\u8ba9 Nextcloud \u652f\u6301\u4e07\u5146\u7f51\u7edc https:\/\/www.v2e\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.shuran.cn\/?p=956\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-956","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=956"}],"version-history":[{"count":1,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/956\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/956\/revisions\/957"}],"wp:attachment":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}