{"id":340598,"date":"2021-02-11T20:29:45","date_gmt":"2021-02-11T12:29:45","guid":{"rendered":"http:\/\/4563.org\/?p=340598"},"modified":"2021-02-11T20:29:45","modified_gmt":"2021-02-11T12:29:45","slug":"%e5%a6%82%e4%bd%95%e5%9f%ba%e4%ba%8e-channel-%e5%ae%9e%e7%8e%b0%e5%a4%9a%e8%b7%af%e5%a4%8d%e7%94%a8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=340598","title":{"rendered":"\u5982\u4f55\u57fa\u4e8e Channel \u5b9e\u73b0\u591a\u8def\u590d\u7528"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u57fa\u4e8e Channel \u5b9e\u73b0\u591a\u8def\u590d\u7528               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : limingxinleo <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5148\u4ecb\u7ecd\u4e00\u4e0b <code>SwooleCoroutineClient<\/code> \u7684\u4e00\u4e2a\u9650\u5236\uff0c\u90a3\u4fbf\u662f\u540c\u4e00\u4e2a\u8fde\u63a5\uff0c\u4e0d\u5141\u8bb8\u540c\u65f6\u88ab\u4e24\u4e2a\u534f\u7a0b\u7ed1\u5b9a\uff0c\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u4ee5\u4e0b\u6d4b\u8bd5\u3002<\/p>\n<pre><code>&lt;?php run(function () {     $client = new SwooleCoroutineClient(SWOOLE_SOCK_TCP);     $client-&gt;set([         'open_length_check' =&gt; true,         'package_length_type' =&gt; 'N',         'package_length_offset' =&gt; 0,         'package_body_offset' =&gt; 4,         'package_max_length' =&gt; 1024 * 1024 * 2,     ]);     $client-&gt;connect('127.0.0.1', 9601, 0.5);     go(function () use ($client) {         $ret = $client-&gt;send(str_repeat('xxx', 1000));         $client-&gt;recv();     });     go(function () use ($client) {         $ret = $client-&gt;send('xxx');         $client-&gt;recv();     }); }); <\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\uff0c\u5c31\u4f1a\u629b\u51fa\u4ee5\u4e0b\u9519\u8bef<\/p>\n<pre><code>PHP Fatal error:  Uncaught SwooleError: Socket#9 has already been bound to another coroutine#2, reading of the same socket in coroutine#3 at the same time is not allowed in \/Users\/limingxin\/Applications\/GitHub\/hyperf\/repos\/multiplex-socket\/tests\/swoole_client.php:32 Stack trace: #0 \/Users\/limingxin\/Applications\/GitHub\/hyperf\/repos\/multiplex-socket\/tests\/swoole_client.php(32): SwooleCoroutineClient-&gt;recv() #1 \/Users\/limingxin\/Applications\/GitHub\/hyperf\/repos\/multiplex-socket\/vendor\/hyperf\/utils\/src\/Functions.php(271): {closure}() #2 \/Users\/limingxin\/Applications\/GitHub\/hyperf\/repos\/multiplex-socket\/vendor\/hyperf\/utils\/src\/Coroutine.php(62): call(Object(Closure)) #3 {main}   thrown in \/Users\/limingxin\/Applications\/GitHub\/hyperf\/repos\/multiplex-socket\/tests\/swoole_client.php on line 32 <\/code><\/pre>\n<p>\u4f46\u6211\u4eec\u7a0d\u5fae\u6539\u52a8\u4e00\u4e0b\u4ee3\u7801\uff0c\u5c31\u4e0d\u4f1a\u518d\u6b21\u62a5\u9519\uff0c\u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code>&lt;?php run(function () {     $client = new SwooleCoroutineClient(SWOOLE_SOCK_TCP);     $client-&gt;set([         'open_length_check' =&gt; true,         'package_length_type' =&gt; 'N',         'package_length_offset' =&gt; 0,         'package_body_offset' =&gt; 4,         'package_max_length' =&gt; 1024 * 1024 * 2,     ]);     $client-&gt;connect('127.0.0.1', 9601, 0.5);     $chan = new SwooleCoroutineChannel(1);     go(function () use ($client, $chan) {         $ret = $client-&gt;send(str_repeat('xxx', 1000));         $chan-&gt;push(true);         $client-&gt;recv();         $chan-&gt;pop();     });     go(function () use ($client, $chan) {         $ret = $client-&gt;send('xxx');         $chan-&gt;push(true);         $client-&gt;recv();         $chan-&gt;pop();     }); }); <\/code><\/pre>\n<p>\u53ef\u89c1\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8ba9 <code>recv<\/code> \u5728\u4e00\u4e2a\u534f\u7a0b\u91cc\u5faa\u73af\u8c03\u7528\uff0c\u7136\u540e\u518d\u6839\u636e\u6536\u5305\u53d1\u5230\u4e0d\u540c\u7684 <code>Channel<\/code> \u5f53\u4e2d\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u591a\u4e2a\u534f\u7a0b\u590d\u7528\u540c\u4e00\u4e2a\u8fde\u63a5\u3002<\/p>\n<h2>\u5305\u4f53\u8bbe\u8ba1<\/h2>\n<p>\u63a5\u4e0b\u6765\u7684\u4e8b\u60c5\u5c31\u5f88\u7b80\u5355\u4e86\uff0c\u6211\u4eec\u8bbe\u8ba1\u4e00\u4e2a\u5341\u5206\u7b80\u5355\u7684\u5305\u7ed3\u6784\u3002\u5305\u5934\u4e3a\u4f7f\u7528 pack N \u6253\u5305\u7684\u5305\u4f53\u957f\u5ea6\uff0c\u5305\u4f53\u4e3a pack N \u6253\u5305\u7684 Channel ID \u548c \u6570\u636e\u4f53\u3002<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210218_602ea54898e4a.jpg\" alt=\"\u5982\u4f55\u57fa\u4e8e Channel \u5b9e\u73b0\u591a\u8def\u590d\u7528\" \/><\/p>\n<p>\u56e0\u4e3a Swoole \u4e2d\u5206\u5305\u89c4\u5219\u5df2\u7ecf\u5b9e\u73b0\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u7684\u914d\u7f6e\u4e00\u4e0b\u5b9e\u73b0\u4e0a\u8ff0\u6548\u679c<\/p>\n<pre><code>'open_length_check' =&gt; true, 'package_length_type' =&gt; 'N', 'package_length_offset' =&gt; 0, 'package_body_offset' =&gt; 4, 'package_max_length' =&gt; 1024 * 1024 * 2, <\/code><\/pre>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u53ea\u9700\u8981\u5b9e\u73b0\u5305\u4f53\u7684 \u6253\u5305 \u548c \u89e3\u5305\u529f\u80fd\u5373\u53ef\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5341\u5206\u7b80\u5355\u7684\u6253\u5305\u5668\u3002<\/p>\n<pre><code>&lt;?php  declare(strict_types=1);  namespace Multiplex;  use MultiplexConstractPackerInterface;  class Packer implements PackerInterface {     public function pack(Packet $packet): string     {         return sprintf(             '%s%s%s',             pack('N', strlen($packet-&gt;getBody()) + 4),             pack('N', $packet-&gt;getId()),             $packet-&gt;getBody()         );     }      public function unpack(string $data): Packet     {         $unpacked = unpack('Nid', substr($data, 4, 4));         $body = substr($data, 8);         return new Packet((int) $unpacked['id'], $body);     } }  <\/code><\/pre>\n<h2>\u670d\u52a1\u7aef<\/h2>\n<p>\u670d\u52a1\u7aef\u7684\u8bbe\u8ba1\u5c31\u5c24\u4e3a\u7b80\u5355\u4e86\uff0c\u56e0\u4e3a Channel \u673a\u5236\u4e3b\u8981\u662f\u7ed9 \u5ba2\u6237\u7aef\u4f7f\u7528\uff0c\u6240\u4ee5\u670d\u52a1\u7aef\u89e3\u5305\u4e4b\u540e\uff0c\u539f\u5c01\u4e0d\u52a8\u7684\u5c06 ChannelID \u548c \u6570\u636e\u8fd4\u56de\u5373\u53ef\u3002<\/p>\n<pre><code>$server-&gt;handle(function (Connection $conn) {     while (true) {         $ret = $conn-&gt;recv();         if (empty($ret)) {             break;         }          Coroutine::create(function () use ($ret, $conn) {             $packet = $this-&gt;packer-&gt;unpack($ret);             $id = $packet-&gt;getId();             try {                 $result = $this-&gt;handler-&gt;__invoke($packet, $this-&gt;getSerializer());             } catch (Throwable $exception) {                 $result = $exception;             } finally {                 $conn-&gt;send($this-&gt;packer-&gt;pack(new Packet($id, $this-&gt;getSerializer()-&gt;serialize($result))));             }         });     } }); <\/code><\/pre>\n<h2>\u5ba2\u6237\u7aef<\/h2>\n<p>\u5ba2\u6237\u7aef\u76f8\u6bd4\u800c\u8a00\uff0c\u5c31\u8981\u9ebb\u70e6\u4e00\u4e9b\u3002\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a Channel \u5b58\u50a8\u9700\u8981 \u53d1\u9001\u7684\u6570\u636e\uff0c\u8fd8\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a Channel Map \u5b58\u50a8\u5404\u4e2a ID \u8fd4\u56de\u7684\u6570\u636e\uff0c\u8fd9\u6837\u65b9\u4fbf recv \u65f6\uff0c\u76f4\u63a5\u4f7f\u7528 Channel::pop() \u83b7\u5f97\u6570\u636e\uff0c\u8fd9\u6837\u4e00\u6765\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5c06 \u4e1a\u52a1\u5ba2\u6237\u7aef\u4e0e\u5b9e\u9645\u5ba2\u6237\u7aef\u8fdb\u884c\u89e3\u8026\u3002<\/p>\n<p>\u4e0b\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e24\u4e2a\u534f\u7a0b\uff0c\u5faa\u73af\u8c03\u7528 <code>Client::send<\/code> \u548c <code>Client::recv<\/code> \u65b9\u6cd5\u3002<\/p>\n<pre><code>protected function loop(): void {     if ($this-&gt;chan !== null &amp;&amp; ! $this-&gt;chan-&gt;isClosing()) {         return;     }     $this-&gt;chan = $this-&gt;getChannelManager()-&gt;make(65535);     $this-&gt;client = $this-&gt;makeClient();     Coroutine::create(function () {         try {             $chan = $this-&gt;chan;             $client = $this-&gt;client;             while (true) {                 $data = $client-&gt;recv();                 if (! $client-&gt;isConnected()) {                     break;                 }                 if ($chan-&gt;isClosing()) {                     break;                 }                  $packet = $this-&gt;packer-&gt;unpack($data);                 if ($channel = $this-&gt;getChannelManager()-&gt;get($packet-&gt;getId())) {                     $channel-&gt;push(                         $this-&gt;serializer-&gt;unserialize($packet-&gt;getBody())                     );                 }             }         } finally {             $chan-&gt;close();             $client-&gt;close();         }     });      Coroutine::create(function () {         try {             $chan = $this-&gt;chan;             $client = $this-&gt;client;             while (true) {                 $data = $chan-&gt;pop();                 if ($chan-&gt;isClosing()) {                     break;                 }                 if (! $client-&gt;isConnected()) {                     break;                 }                  if (empty($data)) {                     continue;                 }                  $client-&gt;send($data);             }         } finally {             $chan-&gt;close();             $client-&gt;close();         }     }); } <\/code><\/pre>\n<h2>\u5b9e\u73b0\u7ec4\u4ef6<\/h2>\n<p>\u6700\u540e\uff0c\u6839\u636e\u4e0a\u8ff0\u7684\u60f3\u6cd5\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4ee5\u4e0b\u4e24\u4e2a\u7ec4\u4ef6<\/p>\n<p>multiplex multiplex-socket<\/p>\n<p>\u968f\u624b\u5199\u4e86\u4e24\u6bb5\u4ee3\u7801\uff0c\u5bf9\u591a\u8def\u590d\u7528\u548c\u8fde\u63a5\u6c60\u8fdb\u884c\u6d4b\u8bd5\uff0c\u6211\u4eec\u521b\u5efa 10000 \u4e2a\u534f\u7a0b\uff0c\u540c\u65f6\u8c03\u7528\u670d\u52a1\u7aef\uff0c\u5f53\u670d\u52a1\u7aef\u63a5\u6536\u5230\u6570\u636e\uff0c\u7acb\u9a6c\u8fd4\u56de\u7684\u60c5\u51b5\u4e0b<\/p>\n<p>\u4e8c\u8005\u5dee\u8ddd\u4e0d\u5927\uff0c\u5b8c\u5168\u7ed3\u675f\u90fd\u5728 0.3-0.5 \u79d2\u4e4b\u95f4\u3002<\/p>\n<p>\u4f46\u5f53\u6211\u4eec\u5728\u8fd4\u56de\u6570\u636e\u524d\uff0c\u7761\u7720 10 \u6beb\u79d2\u7684\u60c5\u51b5\u4e0b\uff0c\u591a\u8def\u590d\u7528\u6240\u7528\u7684\u65f6\u95f4\u8981\u4f4e\u4e8e\u8fde\u63a5\u6c60\u7684\u5341\u5206\u4e4b\u4e00\u3002<\/p>\n<p>\u4e0d\u4ec5\u901f\u5ea6\u66f4\u5feb\uff0c\u591a\u8def\u590d\u7528\u7684\u8fde\u63a5\uff0c\u4ece\u59cb\u81f3\u7ec8\u53ea\u7528\u5230 1 \u4e2a\uff0c\u4f46\u8fde\u63a5\u6c60\u5374\u8d77\u4e86 100 \u4e2a\u8fde\u63a5\uff0c\u7efc\u5408\u6765\u8bf4\uff0c\u591a\u8def\u590d\u7528\u8981\u6bd4\u4f7f\u7528\u8fde\u63a5\u6c60\u8868\u73b0\u7684\u66f4\u52a0\u4f18\u79c0\u3002<\/p>\n<h3>\u793a\u4f8b<\/h3>\n<p>\u5ba2\u6237\u7aef<\/p>\n<pre><code>&lt;?php declare(strict_types=1);  require_once __DIR__ . '\/..\/vendor\/autoload.php';  run(function () use ($max) {     $client = new MultiplexSocketClient('127.0.0.1', 9601);     for ($i = 0; $i &lt; $max; ++$i) {         go(function () use ($client, $channel) {             $client-&gt;request('World.');         });     } }); <\/code><\/pre>\n<p>\u670d\u52a1\u7aef<\/p>\n<pre><code>&lt;?php  declare(strict_types=1);  use MultiplexPacket; use MultiplexSocketServer;  require_once __DIR__ . '\/..\/vendor\/autoload.php';  run(function () {     $server = new Server();     $config = collect([]);     $server-&gt;bind('0.0.0.0', 9601, $config)-&gt;handle(static function (Packet $packet) {         return 'Hello ' . $packet-&gt;getBody();     })-&gt;start(); });  <\/code><\/pre>\n<h2>\u5199\u5728\u6700\u540e<\/h2>\n<p>Hyperf \u662f\u57fa\u4e8e Swoole 4.5+ \u5b9e\u73b0\u7684\u9ad8\u6027\u80fd\u3001\u9ad8\u7075\u6d3b\u6027\u7684 PHP \u534f\u7a0b\u6846\u67b6\uff0c\u5185\u7f6e\u534f\u7a0b\u670d\u52a1\u5668\u53ca\u5927\u91cf\u5e38\u7528\u7684\u7ec4\u4ef6\uff0c\u6027\u80fd\u8f83\u4f20\u7edf\u57fa\u4e8e PHP-FPM \u7684\u6846\u67b6\u6709\u8d28\u7684\u63d0\u5347\uff0c\u63d0\u4f9b\u8d85\u9ad8\u6027\u80fd\u7684\u540c\u65f6\uff0c\u4e5f\u4fdd\u6301\u7740\u6781\u5176\u7075\u6d3b\u7684\u53ef\u6269\u5c55\u6027\uff0c\u6807\u51c6\u7ec4\u4ef6\u5747\u57fa\u4e8e PSR \u6807\u51c6 \u5b9e\u73b0\uff0c\u57fa\u4e8e\u5f3a\u5927\u7684\u4f9d\u8d56\u6ce8\u5165\u8bbe\u8ba1\uff0c\u4fdd\u8bc1\u4e86\u7edd\u5927\u90e8\u5206\u7ec4\u4ef6\u6216\u7c7b\u90fd\u662f \u53ef\u66ff\u6362 \u4e0e \u53ef\u590d\u7528 \u7684\u3002<\/p>\n<p>\u6846\u67b6\u7ec4\u4ef6\u5e93\u9664\u4e86\u5e38\u89c1\u7684\u534f\u7a0b\u7248\u7684 MySQL \u5ba2\u6237\u7aef\u3001Redis \u5ba2\u6237\u7aef\uff0c\u8fd8\u4e3a\u60a8\u51c6\u5907\u4e86\u534f\u7a0b\u7248\u7684 Eloquent ORM \u3001WebSocket \u670d\u52a1\u7aef\u53ca\u5ba2\u6237\u7aef\u3001JSON RPC \u670d\u52a1\u7aef\u53ca\u5ba2\u6237\u7aef\u3001GRPC \u670d\u52a1\u7aef\u53ca\u5ba2\u6237\u7aef\u3001Zipkin\/Jaeger (OpenTracing) \u5ba2\u6237\u7aef\u3001Guzzle HTTP \u5ba2\u6237\u7aef\u3001Elasticsearch \u5ba2\u6237\u7aef\u3001Consul \u5ba2\u6237\u7aef\u3001ETCD \u5ba2\u6237\u7aef\u3001AMQP \u7ec4\u4ef6\u3001Apollo \u914d\u7f6e\u4e2d\u5fc3\u3001\u963f\u91cc\u4e91 ACM \u5e94\u7528\u914d\u7f6e\u7ba1\u7406\u3001ETCD \u914d\u7f6e\u4e2d\u5fc3\u3001\u57fa\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\u7684\u9650\u6d41\u5668\u3001\u901a\u7528\u8fde\u63a5\u6c60\u3001\u7194\u65ad\u5668\u3001Swagger \u6587\u6863\u751f\u6210\u3001Swoole Tracker \u3001Blade \u548c Smarty \u89c6\u56fe\u5f15\u64ce\u3001Snowflake \u5168\u5c40 ID \u751f\u6210\u5668 \u7b49\u7ec4\u4ef6\uff0c\u7701\u53bb\u4e86\u81ea\u5df1\u5b9e\u73b0\u5bf9\u5e94\u534f\u7a0b\u7248\u672c\u7684\u9ebb\u70e6\u3002<\/p>\n<p>Hyperf \u8fd8\u63d0\u4f9b\u4e86 \u57fa\u4e8e PSR-11 \u7684\u4f9d\u8d56\u6ce8\u5165\u5bb9\u5668\u3001\u6ce8\u89e3\u3001AOP \u9762\u5411\u5207\u9762\u7f16\u7a0b\u3001\u57fa\u4e8e PSR-15 \u7684\u4e2d\u95f4\u4ef6\u3001\u81ea\u5b9a\u4e49\u8fdb\u7a0b\u3001\u57fa\u4e8e PSR-14 \u7684\u4e8b\u4ef6\u7ba1\u7406\u5668\u3001Redis\/RabbitMQ \u6d88\u606f\u961f\u5217\u3001\u81ea\u52a8\u6a21\u578b\u7f13\u5b58\u3001\u57fa\u4e8e PSR-16 \u7684\u7f13\u5b58\u3001Crontab \u79d2\u7ea7\u5b9a\u65f6\u4efb\u52a1\u3001Translation \u56fd\u9645\u5316\u3001Validation \u9a8c\u8bc1\u5668 \u7b49\u975e\u5e38\u4fbf\u6377\u7684\u529f\u80fd\uff0c\u6ee1\u8db3\u4e30\u5bcc\u7684\u6280\u672f\u573a\u666f\u548c\u4e1a\u52a1\u573a\u666f\uff0c\u5f00\u7bb1\u5373\u7528\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5264992\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : fyibmsd <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             select                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u57fa\u4e8e Channel \u5b9e\u73b0\u591a\u8def&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340598"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340598"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/340598\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340598"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}