{"id":6572,"date":"2024-09-19T19:32:32","date_gmt":"2024-09-19T11:32:32","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6572"},"modified":"2024-09-19T19:32:32","modified_gmt":"2024-09-19T11:32:32","slug":"srv-shutdownctx-%e5%85%b3%e9%94%ae%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6572","title":{"rendered":"srv.Shutdown(ctx) \u5173\u952e\u4ee3\u7801"},"content":{"rendered":"<p><code>srv.Shutdown(ctx)<\/code> \u662f Go \u6807\u51c6\u5e93 <code>net\/http<\/code> \u5305\u4e2d\u7684 <code>http.Server<\/code> \u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u5b83\u7528\u4e8e\u4f18\u96c5\u5730\u5173\u95ed HTTP \u670d\u52a1\u5668\uff0c\u786e\u4fdd\u6240\u6709\u6b63\u5728\u5904\u7406\u7684\u8bf7\u6c42\u5b8c\u6210\u540e\u518d\u9000\u51fa\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f <code>srv.Shutdown(ctx)<\/code> \u7684\u6838\u5fc3\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u670d\u52a1\u5668\u5173\u95ed\u8fc7\u7a0b\u4e2d\u7b49\u5f85\u6240\u6709\u8bf7\u6c42\u5b8c\u6210\u3002\u8bf7\u6ce8\u610f\uff0c\u5b9e\u9645\u6e90\u7801\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4ee5\u4e0b\u4ee3\u7801\u4ec5\u4e3a\u6838\u5fc3\u903b\u8f91\u7684\u7b80\u5316\u7248\u672c\uff1a<\/p>\n<pre><code class=\"language-go\">\/\/ http\/server.go\n\n\/\/ Shutdown initiates a graceful shutdown of the server, waiting for\n\/\/ currently active connections to finish.\nfunc (srv *Server) Shutdown(ctx context.Context) error {\n    \/\/ Signal the server to stop accepting new connections.\n    srv.setClosing()\n\n    \/\/ Close the listener so no new connections will be accepted.\n    if err := srv.Listener.Close(); err != nil &amp;&amp; err != ErrServerClosed {\n        return err\n    }\n\n    \/\/ Create a channel to signal when all active connections have finished.\n    done := make(chan struct{})\n\n    \/\/ Start a goroutine to wait for active connections to close.\n    go func() {\n        srv.wg.Wait()\n        close(done)\n    }()\n\n    \/\/ Wait for either the connections to close or the context to timeout.\n    select {\n    case &lt;-ctx.Done():\n        return ctx.Err() \/\/ Return context error if context is canceled or timed out.\n    case &lt;-done:\n        return nil \/\/ Successfully closed after all connections are done.\n    }\n}\n\n\/\/ setClosing sets the server to a &quot;closing&quot; state to stop accepting new connections.\nfunc (srv *Server) setClosing() {\n    srv.mu.Lock()\n    defer srv.mu.Unlock()\n    srv.closing = true\n}<\/code><\/pre>\n<h3>\u5173\u952e\u90e8\u5206\u89e3\u6790<\/h3>\n<ol>\n<li>\n<p><strong>\u505c\u6b62\u63a5\u53d7\u65b0\u8fde\u63a5<\/strong>\uff1a<\/p>\n<ul>\n<li><code>srv.setClosing()<\/code> \u8bbe\u7f6e\u670d\u52a1\u5668\u4e3a\u5173\u95ed\u72b6\u6001\uff0c\u6807\u5fd7\u7740\u670d\u52a1\u5668\u5c06\u4e0d\u518d\u63a5\u53d7\u65b0\u7684\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5173\u95ed\u76d1\u542c\u5668<\/strong>\uff1a<\/p>\n<ul>\n<li><code>srv.Listener.Close()<\/code> \u5173\u95ed\u76d1\u542c\u5668\uff0c\u9632\u6b62\u65b0\u7684\u8fde\u63a5\u5230\u8fbe\u3002\u6ce8\u610f\uff0c\u5982\u679c\u76d1\u542c\u5668\u5df2\u7ecf\u5173\u95ed\u6216\u8005\u51fa\u73b0\u9519\u8bef\uff0c<code>Shutdown<\/code> \u65b9\u6cd5\u4f1a\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7b49\u5f85\u6d3b\u8dc3\u8fde\u63a5\u5173\u95ed<\/strong>\uff1a<\/p>\n<ul>\n<li><code>srv.wg.Wait()<\/code> \u7b49\u5f85\u6240\u6709\u6d3b\u8dc3\u7684\u8fde\u63a5\u5904\u7406\u5b8c\u6bd5\u3002<code>srv.wg<\/code> \u662f\u4e00\u4e2a <code>sync.WaitGroup<\/code>\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6d3b\u52a8\u8fde\u63a5\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5904\u7406\u8d85\u65f6\u6216\u5b8c\u6210\u4fe1\u53f7<\/strong>\uff1a<\/p>\n<ul>\n<li><code>select<\/code> \u8bed\u53e5\u7528\u4e8e\u7b49\u5f85 <code>ctx.Done()<\/code>\uff08\u4e0a\u4e0b\u6587\u7684\u8d85\u65f6\u6216\u53d6\u6d88\uff09\u6216 <code>done<\/code> \u901a\u9053\uff08\u6240\u6709\u8fde\u63a5\u5b8c\u6210\uff09\u3002\u5982\u679c\u4e0a\u4e0b\u6587\u8d85\u65f6\u6216\u88ab\u53d6\u6d88\uff0c\u8fd4\u56de\u4e0a\u4e0b\u6587\u9519\u8bef\uff1b\u5982\u679c\u6240\u6709\u8fde\u63a5\u5b8c\u6210\uff0c\u5219\u8fd4\u56de <code>nil<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>\u76f8\u5173\u4ee3\u7801<\/h3>\n<ul>\n<li><code>srv.wg<\/code> \u662f <code>sync.WaitGroup<\/code> \u7c7b\u578b\uff0c\u7528\u4e8e\u8ddf\u8e2a\u6d3b\u52a8\u8fde\u63a5\u3002<\/li>\n<li><code>srv.setClosing()<\/code> \u6807\u8bb0\u670d\u52a1\u5668\u72b6\u6001\u4e3a\u201c\u5173\u95ed\u201d\uff0c\u505c\u6b62\u63a5\u53d7\u65b0\u8fde\u63a5\u3002<\/li>\n<li><code>srv.Listener<\/code> \u662f\u5b9e\u9645\u7684\u7f51\u7edc\u76d1\u542c\u5668\uff0c\u8d1f\u8d23\u63a5\u53d7\u65b0\u7684\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p><code>Shutdown<\/code> \u65b9\u6cd5\u7684\u5b9e\u9645\u6e90\u7801\u53ef\u80fd\u5305\u542b\u66f4\u591a\u7ec6\u8282\u548c\u5904\u7406\u903b\u8f91\uff0c\u4f46\u6838\u5fc3\u601d\u60f3\u662f\u5728\u5173\u95ed\u8fc7\u7a0b\u4e2d\u786e\u4fdd\u6240\u6709\u6d3b\u8dc3\u8fde\u63a5\u5f97\u5230\u5904\u7406\uff0c\u5e76\u5728\u8d85\u65f6\u6216\u53d6\u6d88\u65f6\u4f18\u96c5\u9000\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>srv.Shutdown(ctx) \u662f Go \u6807\u51c6\u5e93 net\/http \u5305\u4e2d\u7684 http.Server \u7c7b\u578b\u7684&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6572\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6572","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6572"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6572\/revisions"}],"predecessor-version":[{"id":6573,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6572\/revisions\/6573"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}