{"id":15397,"date":"2024-11-17T20:52:14","date_gmt":"2024-11-17T12:52:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15397"},"modified":"2024-11-17T20:52:14","modified_gmt":"2024-11-17T12:52:14","slug":"openresty%e4%b8%adcosocket%e7%9a%84%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15397\/","title":{"rendered":"openResty\u4e2dcosocket\u7684\u63a7\u5236"},"content":{"rendered":"<p>cosocket \u662f OpenResty \u4e16\u754c\u4e2d\u6280\u672f\u3001\u5b9e\u7528\u4ef7\u503c\u6700\u9ad8\u7684\u90e8\u5206\u3002\u8ba9\u6211\u4eec\u53ef\u4ee5\u7528\u975e\u5e38\u4f4e\u5ec9\u7684\u6210\u672c\uff0c\u4f18\u96c5\u7684\u59ff\u52bf\uff0c\u6bd4\u4f20\u7edf socket \u7f16\u7a0b\u6548\u7387\u9ad8\u597d\u51e0\u500d\u7684\u65b9\u5f0f\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u3002\u65e0\u8bba\u8d44\u6e90\u5360\u7528\u3001\u6267\u884c\u6548\u7387\u3001\u5e76\u53d1\u6570\u7b49\u90fd\u975e\u5e38\u51fa\u8272\u3002<\/p>\n<pre>cosocket = coroutine + socket\r\ncoroutine\uff1a\u534f\u540c\u7a0b\u5e8f\uff08\u540e\u9762\u7b80\u79f0\uff1a\u534f\u7a0b\uff09\r\nsocket\uff1a\u7f51\u7edc\u5957\u63a5\u5b57<\/pre>\n<p>&nbsp;<\/p>\n<p>OpenResty \u4e2d\u7684 cosocket \u4e0d\u4ec5\u9700\u8981\u534f\u7a0b\u7279\u6027\u652f\u6491\uff0c\u5b83\u8fd8\u9700 nginx \u975e\u5e38\u6700\u91cd\u8981\u7684\u4e00\u90e8\u5206\u201c\u4e8b\u4ef6\u5faa\u73af\u56de\u8c03\u673a\u5236\u201d\uff0c\u4e24\u90e8\u5206\u62fc\u5728\u4e00\u8d77\u624d\u8fbe\u5230\u4e86\u6700\u540e\u7684 cosocket \u6548\u679c\uff0c\u518d\u7ed3\u5408 nginx \u81ea\u8eab\u5bf9\u5404\u79cd\u8d44\u6e90\u7684\u201c\u5c0f\u6c14\u201d\uff0c\u4f7f\u5f97\u6574\u4f53\u52a0\u5206\u4e0d\u5c11\u3002\u5728 Lua \u4e16\u754c\u4e2d\u8c03\u7528\u4efb\u4f55\u4e00\u4e2a\u6709\u5173 cosocket \u7f51\u7edc\u51fd\u6570\u5185\u90e8\u5173\u952e\u8c03\u7528\u5982\u56fe\u6240\u793a\uff1a<\/p>\n<p>\u4ece\u8be5\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u7528\u6237\u7684 Lua \u811a\u672c\u6bcf\u89e6\u53d1\u4e00\u4e2a\u7f51\u7edc\u64cd\u4f5c\uff0c\u90fd\u4f1a\u6709\u534f\u7a0b\u7684 yield \u4ee5\u53ca resume\uff0c\u56e0\u4e3a\u8bf7\u6c42\u7684 Lua \u811a\u672c\u5b9e\u9645\u4e0a\u90fd\u8fd0\u884c\u5728\u72ec\u4eab\u534f\u7a0b\u4e4b\u4e0a\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u65f6\u5019\u6682\u505c\u81ea\u5df1\uff08yield\uff09\uff0c\u4e5f\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u65f6\u5019\u88ab\u5524\u9192\uff08resume\uff09\u3002<\/p>\n<p>\u6682\u505c\u81ea\u5df1\uff0c\u628a\u7f51\u7edc\u4e8b\u4ef6\u6ce8\u518c\u5230 Nginx \u76d1\u542c\u5217\u8868\u4e2d\uff0c\u5e76\u628a\u8fd0\u884c\u6743\u9650\u4ea4\u7ed9 Nginx\u3002\u5f53\u6709 Nginx \u6ce8\u518c\u7f51\u7edc\u4e8b\u4ef6\u8fbe\u5230\u89e6\u53d1\u6761\u4ef6\u65f6\uff0c\u5524\u9192\u5bf9\u5e94\u7684\u534f\u7a0b\u7ee7\u7eed\u5904\u7406\u3002<\/p>\n<p>\u4ee5\u6b64\u4e3a\u84dd\u677f\uff0c\u5c01\u88c5\u5b9e\u73b0 connect\u3001read\u3001recieve \u7b49\u64cd\u4f5c\uff0c\u5f62\u6210\u4e86\u5927\u5bb6\u76ee\u524d\u6240\u770b\u5230\u7684 cosocket API\u3002<\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0ccosocket \u662f\u4f9d\u8d56 Lua \u534f\u7a0b + Nginx \u4e8b\u4ef6\u901a\u77e5\u4e24\u4e2a\u91cd\u8981\u7279\u6027\u62fc\u7684\u3002<\/p>\n<p>\u4ece 0.9.9 \u7248\u672c\u5f00\u59cb\uff0ccosocket \u5bf9\u8c61\u662f\u5168\u53cc\u5de5\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u4e13\u95e8\u8bfb\u53d6\u7684 \u201clight thread\u201d\uff0c\u4e00\u4e2a\u4e13\u95e8\u5199\u5165\u7684 \u201clight thread\u201d\uff0c\u5b83\u4eec\u53ef\u4ee5\u540c\u65f6\u5bf9\u540c\u4e00\u4e2a cosocket \u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\uff08\u4e24\u4e2a \u201clight threads\u201d \u5fc5\u987b\u8fd0\u884c\u5728\u540c\u4e00\u4e2a Lua \u73af\u5883\u4e2d\uff0c\u539f\u56e0\u89c1\u4e0a\uff09\u3002\u4f46\u662f\u4f60\u4e0d\u80fd\u8ba9\u4e24\u4e2a \u201clight threads\u201d \u5bf9\u540c\u4e00\u4e2a cosocket \u5bf9\u8c61\u90fd\u8fdb\u884c\u8bfb\uff08\u6216\u8005\u5199\u5165\u3001\u6216\u8005\u8fde\u63a5\uff09\u64cd\u4f5c\uff0c\u5426\u5219\u5f53\u8c03\u7528 cosocket \u5bf9\u8c61\u65f6\uff0c\u4f60\u5c06\u5f97\u5230\u4e00\u4e2a\u7c7b\u4f3c \u201csocket busy reading\u201d \u7684\u9519\u8bef\u3002<\/p>\n<p>\u6240\u4ee5\u4e1c\u897f\u603b\u7ed3\u4e0b\u6765\uff0c\u5230\u5e95\u4ec0\u4e48\u662f cosocket\uff0c\u4e2d\u6587\u5e94\u8be5\u600e\u4e48\u7ffb\u8bd1\uff0c\u7b14\u8005\u672c\u4eba\u90fd\u5f00\u59cb\u7ea0\u7ed3\u4e86\u3002\u6211\u4eec\u4e0d\u59a8\u4ece\u53e6\u5916\u4e00\u4e2a\u89d2\u5ea6\u6765\u5ba1\u89c6\u5b83\uff0c\u5b83\u5230\u5e95\u7ed9\u6211\u4eec\u5e26\u6765\u4e86\u4ec0\u4e48\u3002<\/p>\n<ul>\n<li>\u5b83\u662f\u540c\u6b65\u7684\uff1b<\/li>\n<li>\u5b83\u662f\u975e\u963b\u585e\u7684\uff1b<\/li>\n<li>\u5b83\u662f\u5168\u53cc\u5de5\u7684\uff1b<\/li>\n<\/ul>\n<pre>\u540c\u6b65\u4e0e\u5f02\u6b65\u89e3\u91ca\uff1a\r\n\u540c\u6b65\uff1a\u505a\u5b8c\u4e00\u4ef6\u4e8b\u518d\u53bb\u505a\u53e6\u4e00\u4ef6\uff1b\r\n\u5f02\u6b65\uff1a\u540c\u65f6\u505a\u591a\u4ef6\u4e8b\u60c5\uff0c\u67d0\u4e2a\u4e8b\u60c5\u6709\u7ed3\u679c\u4e86\u518d\u53bb\u5904\u7406\u3002\r\n\r\n\u963b\u585e\u4e0e\u975e\u963b\u585e\u89e3\u91ca\uff1a\r\n\u963b\u585e\uff1a\u4e0d\u7b49\u5230\u60f3\u8981\u7684\u7ed3\u679c\u6211\u5c31\u4e0d\u8d70\u4e86\uff1b\r\n\u975e\u963b\u585e\uff1a\u6709\u7ed3\u679c\u6211\u5c31\u5e26\u8d70\uff0c\u6ca1\u7ed3\u679c\u6211\u5c31\u7a7a\u624b\u800c\u56de\uff0c\u603b\u4e4b\u4e00\u53e5\u8bdd\uff1a\u7237\u7b49\u4e0d\u8d77\u3002<\/pre>\n<p>\u5f02\u6b65\uff0f\u540c\u6b65\u662f\u505a\u4e8b\u6d3e\u53d1\u65b9\u5f0f\uff0c\u963b\u585e\uff0f\u975e\u963b\u585e\u662f\u5982\u4f55\u5904\u7406\u4e8b\u60c5\uff0c\u4e24\u7ec4\u6982\u5ff5\u4e0d\u5728\u540c\u4e00\u4e2a\u5c42\u9762\u3002<\/p>\n<p>\u65e0\u8bba ngx.socket.tcp()\u3001ngx.socket.udp()\u3001ngx.socket.stream()\u3001ngx.req.socket()\uff0c\u5b83\u4eec\u57fa\u672c\u6d41\u7a0b\u90fd\u662f\u4e00\u6837\u7684\uff0c\u53ea\u662f\u4e00\u4e9b\u7ec6\u8282\u53c2\u6570\u4e0a\u6709\u533a\u522b\uff08\u6bd4\u5982 TCP \u548c UDP \u7684\u533a\u522b\uff09\u3002\u4e0b\u9762\u8fd9\u4e9b\u51fd\u6570\uff0c\u90fd\u662f\u7528\u6765\u8f85\u52a9\u5b8c\u6210\u66f4\u9ad8\u7ea7\u7684 socket \u884c\u4e3a\u63a7\u5236\uff1a<\/p>\n<h1>1.1 ngx.req.socket<\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>tcpsock, err = ngx.req.socket()<br \/>\n<\/em><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>tcpsock, err = ngx.req.socket(raw)<br \/>\n<\/em><strong>\u4f5c\u7528\u57df:<\/strong>\u00a0<em>rewrite_by_lua*, access_by_lua*, content_by_lua*<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>cosocket \u662f OpenResty \u4e16\u754c\u4e2d\u6280\u672f\u3001\u5b9e\u7528\u4ef7\u503c\u6700\u9ad8\u7684\u90e8\u5206\u3002\u8ba9\u6211\u4eec\u53ef\u4ee5\u7528\u975e\u5e38\u4f4e\u5ec9\u7684\u6210\u672c\uff0c\u4f18\u96c5\u7684\u59ff\u52bf\uff0c\u6bd4\u4f20\u7edf socket \u7f16\u7a0b\u6548\u7387\u9ad8\u597d\u51e0\u500d\u7684\u65b9\u5f0f\u8fdb\u884c\u7f51\u7edc\u7f16\u7a0b\u3002\u65e0\u8bba\u8d44\u6e90\u5360\u7528\u3001\u6267\u884c\u6548\u7387\u3001\u5e76\u53d1\u6570\u7b49\u90fd\u975e\u5e38\u51fa\u8272\u3002 cosocket = coroutine + socket coroutine\uff1a\u534f\u540c\u7a0b\u5e8f\uff08\u540e\u9762\u7b80\u79f0\uff1a\u534f\u7a0b\uff09 socket\uff1a\u7f51\u7edc\u5957\u63a5\u5b57 &nbsp; OpenResty \u4e2d\u7684 cosocket \u4e0d\u4ec5\u9700\u8981\u534f\u7a0b\u7279\u6027\u652f\u6491\uff0c\u5b83\u8fd8\u9700 nginx \u975e\u5e38\u6700\u91cd\u8981\u7684\u4e00\u90e8\u5206\u201c\u4e8b\u4ef6\u5faa\u73af\u56de\u8c03\u673a\u5236\u201d\uff0c\u4e24\u90e8\u5206\u62fc\u5728\u4e00\u8d77\u624d\u8fbe\u5230\u4e86\u6700\u540e\u7684 cosocket \u6548\u679c\uff0c\u518d\u7ed3\u5408 nginx \u81ea\u8eab\u5bf9\u5404\u79cd\u8d44\u6e90\u7684\u201c\u5c0f\u6c14\u201d\uff0c\u4f7f\u5f97\u6574\u4f53\u52a0\u5206\u4e0d\u5c11\u3002\u5728 Lua \u4e16\u754c\u4e2d\u8c03\u7528\u4efb\u4f55\u4e00\u4e2a\u6709\u5173 cosocket \u7f51\u7edc\u51fd\u6570\u5185\u90e8\u5173\u952e\u8c03\u7528\u5982\u56fe\u6240\u793a\uff1a \u4ece\u8be5\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u7528\u6237\u7684 Lua \u811a\u672c\u6bcf\u89e6\u53d1\u4e00\u4e2a\u7f51\u7edc\u64cd\u4f5c\uff0c\u90fd\u4f1a\u6709\u534f\u7a0b\u7684 yield \u4ee5\u53ca resume\uff0c\u56e0\u4e3a\u8bf7\u6c42\u7684 Lua \u811a\u672c\u5b9e\u9645\u4e0a\u90fd\u8fd0\u884c\u5728\u72ec\u4eab\u534f\u7a0b\u4e4b\u4e0a\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u65f6\u5019\u6682\u505c\u81ea\u5df1\uff08yield\uff09\uff0c\u4e5f\u53ef\u4ee5\u5728\u4efb\u4f55\u9700\u8981\u7684\u65f6\u5019\u88ab\u5524\u9192\uff08resume\uff09\u3002 \u6682\u505c\u81ea\u5df1\uff0c\u628a\u7f51\u7edc\u4e8b\u4ef6\u6ce8\u518c\u5230 Nginx \u76d1\u542c\u5217\u8868\u4e2d\uff0c\u5e76\u628a\u8fd0\u884c\u6743\u9650\u4ea4\u7ed9 Nginx\u3002\u5f53\u6709 Nginx \u6ce8\u518c\u7f51\u7edc\u4e8b\u4ef6\u8fbe\u5230\u89e6\u53d1\u6761\u4ef6\u65f6\uff0c\u5524\u9192\u5bf9\u5e94\u7684\u534f\u7a0b\u7ee7\u7eed\u5904\u7406\u3002 \u4ee5\u6b64\u4e3a\u84dd\u677f\uff0c\u5c01\u88c5\u5b9e\u73b0 connect\u3001read\u3001recieve \u7b49\u64cd\u4f5c\uff0c\u5f62\u6210\u4e86\u5927\u5bb6\u76ee\u524d\u6240\u770b\u5230\u7684 cosocket API\u3002 \u53ef\u4ee5\u770b\u5230\uff0ccosocket \u662f\u4f9d\u8d56 Lua \u534f\u7a0b + Nginx \u4e8b\u4ef6\u901a\u77e5\u4e24\u4e2a\u91cd\u8981\u7279\u6027\u62fc\u7684\u3002 \u4ece [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15397","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15397"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15397\/revisions"}],"predecessor-version":[{"id":15398,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15397\/revisions\/15398"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}