{"id":437413,"date":"2021-04-18T21:58:56","date_gmt":"2021-04-18T13:58:56","guid":{"rendered":"http:\/\/4563.org\/?p=437413"},"modified":"2021-04-18T21:58:56","modified_gmt":"2021-04-18T13:58:56","slug":"%e4%bd%bf%e7%94%a8-go-1-16-%e7%9a%84-signal-notifycontext-%e8%ae%a9%e4%bd%a0%e7%9a%84%e6%9c%8d%e5%8a%a1%e9%87%8d%e5%90%af%e6%9b%b4%e4%bc%98%e9%9b%85","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=437413","title":{"rendered":"\u4f7f\u7528 Go 1.16 \u7684 signal.NotifyContext \u8ba9\u4f60\u7684\u670d\u52a1\u91cd\u542f\u66f4\u4f18\u96c5"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4f7f\u7528 Go 1.16 \u7684 signal.NotifyContext \u8ba9\u4f60\u7684\u670d\u52a1\u91cd\u542f\u66f4\u4f18\u96c5               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Muninn <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5728 Go 1.16 \u7684\u66f4\u65b0\u4e2d\uff0c<code>signal<\/code>\u5305\u589e\u52a0\u4e86\u4e00\u4e2a\u51fd\u6570 NotifyContext\uff0c \u8fd9\u8ba9\u6211\u4eec\u4f18\u96c5\u7684\u91cd\u542f\u670d\u52a1\uff08 Graceful Restart \uff09\u53ef\u4ee5\u5199\u7684\u66f4\u52a0\u4f18\u96c5\u3002<\/p>\n<p>\u4e00\u4e2a\u670d\u52a1\u60f3\u8981\u4f18\u96c5\u7684\u91cd\u542f\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u9000\u51fa\u7684\u65e7\u670d\u52a1\u9700\u8981 <code>Graceful Shutdown<\/code>\uff0c\u4e0d\u5f3a\u5236\u6740\u8fdb\u7a0b\uff0c\u4e0d\u6cc4\u6f0f\u7cfb\u7edf\u8d44\u6e90\u3002<\/li>\n<li>\u5728\u4e00\u4e2a\u96c6\u7fa4\u5185\u8f6e\u6d41\u91cd\u542f\u670d\u52a1\u5b9e\u4f8b\uff0c\u4fdd\u8bc1\u670d\u52a1\u4e0d\u4e2d\u65ad\u3002<\/li>\n<\/ul>\n<p>\u7b2c\u4e8c\u4e2a\u95ee\u9898\u8ddf\u90e8\u7f72\u65b9\u5f0f\u76f8\u5173\uff0c\u6539\u5929\u4e13\u95e8\u5199\u4e00\u7bc7\u8ba8\u8bba\uff0c\u4eca\u5929\u6211\u4eec\u4e3b\u8981\u8c08\u600e\u4e48\u6837\u4f18\u96c5\u7684\u9000\u51fa\u3002<\/p>\n<p>\u9996\u5148\u5728\u4ee3\u7801\u91cc\uff0c\u7528\u4e86\u5916\u90e8\u8d44\u6e90\uff0c\u4e00\u5b9a\u8981\u4f7f\u7528<code>defer<\/code>\u53bb\u8c03\u7528<code>Close()<\/code>\u65b9\u6cd5\u5173\u95ed\u3002 \u7136\u540e\u6211\u4eec\u5c31\u8981\u62e6\u622a\u7cfb\u7edf\u7684\u4e2d\u65ad\u4fe1\u53f7\uff0c\u4fdd\u8bc1\u7a0b\u5e8f\u6536\u5230\u4e2d\u65ad\u4fe1\u53f7\u4e4b\u540e\uff0c\u4e3b\u52a8\u6709\u5e8f\u9000\u51fa\uff0c\u8fd9\u6837\u6240\u6709\u7684 defer \u624d\u4f1a\u88ab\u6267\u884c\u3002<\/p>\n<p>\u5728\u4ee5\u524d\uff0c\u5927\u6982\u662f\u8fd9\u4e48\u5199\uff1a<\/p>\n<pre><code>func everLoop(ctx context.Context) { LOOP:     for {         select {         case &lt;-ctx.Done():             \/\/ \u6536\u5230\u4fe1\u53f7\u9000\u51fa\u65e0\u9650\u5faa\u73af             break LOOP         default:             \/\/ \u7528\u4e00\u4e2a sleep \u6a21\u62df\u4e1a\u52a1\u903b\u8f91             time.Sleep(time.Second * 10)         }     } }  func main() {     \/\/ \u5efa\u7acb\u4e00\u4e2a\u53ef\u4ee5\u624b\u52a8\u53d6\u6d88\u7684 Context     ctx, cancel := context.WithCancel(context.Background())      \/\/ \u76d1\u63a7\u7cfb\u7edf\u4fe1\u53f7\uff0c\u8fd9\u91cc\u53ea\u76d1\u63a7\u4e86 SIGINT \uff08 Ctrl+c \uff09\uff0cSIGTERM     \/\/ \u5728 systemd \u548c docker \u4e2d\uff0c\u90fd\u662f\u5148\u53d1 SIGTERM\uff0c\u8fc7\u4e00\u6bb5\u65f6\u95f4\u6ca1\u9000\u51fa\u518d\u53d1 SIGKILL     \/\/ \u6240\u4ee5\u8fd9\u91cc\u6ca1\u6355\u83b7 SIGKILL     sig := make(chan os.Signal, 1)     signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)     go func() {         &lt;-sig         cancel()     }()      \/\/ \u5f00\u59cb\u65e0\u9650\u5faa\u73af\uff0c\u6536\u5230\u4fe1\u53f7\u5c31\u4f1a\u9000\u51fa     everLoop(ctx)     fmt.Println(\"graceful shuwdown\") }  <\/code><\/pre>\n<p>\u73b0\u5728\u6709\u4e86\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e00\u6bb5\u53d8\u5f97\u66f4\u7b80\u5355\u4e86\uff1a<\/p>\n<pre><code>func main() {     \/\/ \u76d1\u63a7\u7cfb\u7edf\u4fe1\u53f7\u548c\u521b\u5efa Context \u73b0\u5728\u4e00\u6b65\u641e\u5b9a     ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)     \/\/ \u5728\u6536\u5230\u4fe1\u53f7\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u89e6\u53d1 ctx \u7684 Done\uff0c\u8fd9\u4e2a stop \u662f\u4e0d\u518d\u6355\u83b7\u6ce8\u518c\u7684\u4fe1\u53f7\u7684\u610f\u601d\uff0c\u7b97\u662f\u4e00\u79cd\u91ca\u653e\u8d44\u6e90\u3002     defer stop()      \/\/ \u5f00\u59cb\u65e0\u9650\u5faa\u73af\uff0c\u6536\u5230\u4fe1\u53f7\u5c31\u4f1a\u9000\u51fa     everLoop(ctx)     fmt.Println(\"graceful shuwdown\") }  <\/code><\/pre>\n<p>\u611f\u8c22 <code>Golang<\/code> \uff0c\u5f53\u5e74\u7528\u522b\u7684\u8bed\u8a00\u9700\u8981\u5199\u4e00\u5927\u5806\u4ee3\u7801\u7684\u529f\u80fd\uff0c\u73b0\u5728\u51e0\u884c\u5c31\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u4e86\u3002 \u8ba9\u5b83\u6210\u4e3a\u4f60\u670d\u52a1\u7a0b\u5e8f\u7684\u6807\u914d\u5427\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u662f\u5199\u6700\u65b0\u7684\u9879\u76eeLetServerRun\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u8fd9\u79cd\u6700\u65b0\u7684\u5199\u6cd5\u7684\u3002 LetServerRun \u53ef\u4ee5\u8ba9\u4f60\u628a\u5fae\u4fe1\u516c\u4f17\u53f7\u5f53\u4f5c\u968f\u8eab\u7684 Terminal \u63a7\u5236\u4f60\u7684\u670d\u52a1\u7aef\u3002 \u5728\u5b83\u7684 Agent \u7684 main \u51fd\u6570\u4e2d\u5c31\u6709\u4e0a\u8ff0\u7528\u6cd5\u7684\u793a\u4f8b\uff0c\u6b22\u8fce\u53c2\u8003\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5745775\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : labulaka521 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u725b\u903c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745776\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : labulaka521 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5f88\u68d2\u8fd9\u4e2a\u51fd\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745777\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ahmcsxcc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u592a\u660e\u767d\u7b2c\u4e00\u79cd\u7684\u5199\u6cd5<br \/>\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u7528 sig\uff0c\u8fd8\u8981\u641e\u4e2a context \uff1f<br \/>\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p>func everLoop(ctx context.Context) {<br \/>LOOP:<br \/> for {<br \/> select {<br \/> case &lt;-sig:<br \/> \/\/ \u6536\u5230\u4fe1\u53f7\u9000\u51fa\u65e0\u9650\u5faa\u73af<br \/> break LOOP<br \/> default:<br \/> \/\/ \u7528\u4e00\u4e2a sleep \u6a21\u62df\u4e1a\u52a1\u903b\u8f91<br \/> time.Sleep(time.Second * 10)<br \/> }<br \/> }<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745778\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ahmcsxcc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ahmcsxcc #3 \u5fd8\u4e86\u6539\u51fd\u6570\u7684\u5165\u53c2                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745779\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Muninn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ahmcsxcc \u6211\u662f\u6a21\u62df\u5e38\u89c1\u60c5\u51b5\uff0c\u73b0\u5728 ctx \u662f\u6807\u51c6\uff0c\u5f88\u591a\u7b2c\u4e09\u65b9\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u662f ctx \u3002<\/p>\n<p>\u81ea\u5df1\u5355\u7eaf\u7684\u5199\u5c31\u7528\u90a3\u4e2a\u4fe1\u53f7\u5c31\u597d\u4e86\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\u662f\u9700\u8981\u9760 context \u63a7\u5236\u4e00\u5927\u5806\u4e1c\u897f\u9000\u51fa\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745780\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aoang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`go<\/p>\n<p>func handleSignal() {<br \/> c := make(chan os.Signal)<br \/> signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)<\/p>\n<p> go func() {<br \/> sig := &lt;-c<br \/> log.Info(&#8220;server exit signal&#8221;, zap.Any(&#8220;signal notify&#8221;, sig))<\/p>\n<p> cron.Stop()<br \/> server.Close()<\/p>\n<p> log.Info(&#8220;server exit&#8221;, zap.Time(&#8220;time&#8221;, time.Now()))<br \/> os.Exit(0)<br \/> }()<br \/>}<\/p>\n<p>&#8220;`                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745781\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Muninn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Aoang \u5bf9\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u53ea\u662f\u6539\u5584\u4e86\u672c\u6765\u8981\u7528 context \u7684\u65f6\u5019\u7684\u5199\u6cd5\uff0c\u5982\u679c\u672c\u6765\u4e0d\u7528 context\uff0c\u90a3\u5c31\u7ee7\u7eed\u7528\u4f20\u7edf\u7684\u5199\u6cd5\u5c31\u597d\u3002<\/p>\n<p>\u5b98\u65b9\u7684 http \u5305\uff0c\u4e3a\u4e86\u517c\u5bb9\u5386\u53f2\uff0c\u662f\u6709\u5355\u72ec\u7684 shutdown \u51fd\u6570\uff0c<br \/>\u4e8e\u662f\u5404\u5bb6 web \u6846\u67b6\u4e5f\u90fd\u7528\u4e86 http \u5b98\u65b9\u7684 shutdown \u65b9\u6cd5\uff0c\u6bd4\u5982 echo\uff1a<br \/>https:\/\/echo.labstack.com\/cookbook\/graceful-shutdown\/<\/p>\n<p>\u4e0d\u8fc7\u4e00\u4e9b\u5176\u4ed6\u7684\u540e\u53f0\u670d\u52a1\uff0c\u6216\u8005\u81ea\u5df1\u5199\u7684\uff0c\u90fd\u5df2\u7ecf\u7528 context \u63a7\u5236\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745782\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eudore <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6709\u76d1\u542c\u5bf9\u8c61\u7684 fd \u4f20\u9012\uff1f\uff1f\uff1f \u91cd\u542f\u4e0d\u6b62\u4fe1\u53f7\u76d1\u542c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5745783\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Muninn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @eudore \u90fd\u5199 golang \u4e86\uff0c\u90e8\u7f72\u7684\u65f6\u5019\u80af\u5b9a\u662f docker \/ Kubernetes\uff0c\u5373\u4f7f\u5199\u7cfb\u7edf daemon\uff0c\u4e5f\u662f\u7528 systemd \u7ef4\u62a4\u7740\u3002<\/p>\n<p>\u8fd9\u51e0\u4e2a\u751f\u6001\u90fd\u662f\u7528 sigterm \u4fe1\u53f7\u53bb\u91cd\u542f\u7684\u3002 \u6ca1\u6709\u592a\u5927\u5fc5\u8981\u81ea\u5df1\u5b9e\u73b0\u91cd\u542f\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Go 1.16 \u7684 sign&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\/437413"}],"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=437413"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/437413\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=437413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=437413"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=437413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}