{"id":259721,"date":"2015-06-19T18:45:02","date_gmt":"2015-06-19T14:45:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=259721"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=259721","title":{"rendered":"\u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u00ab\u0447\u0438\u0441\u0442\u043e\u0433\u043e\u00bb \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 Go"},"content":{"rendered":"<p>     \t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0442\u0440\u043e\u043d\u0443\u0442\u0430 \u0442\u0435\u043c\u0430 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u00ab\u0447\u0438\u0441\u0442\u043e\u0433\u043e\u00bb \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Go.<br \/>  \u0427\u0438\u0441\u0442\u044b\u043c \u0432\u044b\u0445\u043e\u0434\u043e\u043c \u044f \u043d\u0430\u0437\u044b\u0432\u0430\u044e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f (\u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443 \u0438\u043b\u0438 \u043f\u043e \u043b\u044e\u0431\u044b\u043c \u0438\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043a\u0440\u043e\u043c\u0435 system failure), \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0438 \u0432\u044b\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d \u0434\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u0414\u0430\u043b\u0435\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e \u0441\u0432\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445.<\/p>\n<p>  TL;DR: <a href=\"https:\/\/github.com\/xlab\/closer\">github.com\/xlab\/closer<\/a> <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer\"><img decoding=\"async\" src=\"https:\/\/godoc.org\/github.com\/xlab\/closer?status.svg\" alt=\"GoDoc\"\/><\/a><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>1. \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0418\u0442\u0430\u043a, \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u044b \u0437\u0430\u043c\u0435\u0447\u0430\u043b\u0438 \u0445\u043e\u0442\u044c \u0440\u0430\u0437, \u043a\u0430\u043a \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438\u043b\u0438 \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u043b\u043e\u0432\u0438\u0442 \u0432\u0430\u0448 \u043a\u0440\u0443\u0447\u0451\u043d\u044b\u0439 <code>Ctrl^C<\/code> \u0438, \u0434\u0438\u043a\u043e \u0438\u0437\u0432\u0438\u043d\u044f\u044f\u0441\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u043e\u043d\u0430 \u043f\u043e\u0440\u0435\u0448\u0430\u0435\u0442 \u0434\u0435\u043b\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0438\u043a\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u0442\u043b\u043e\u0436\u0438\u0442\u044c. \u0425\u043e\u0440\u043e\u0448\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0442 \u0434\u0435\u043b\u0430 \u0438 \u0432\u044b\u0445\u043e\u0434\u044f\u0442, \u043f\u043b\u043e\u0445\u0438\u0435 \u0436\u0435 \u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0432 deadlock \u0438 \u0441\u0434\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0432\u0438\u0434\u0435 <code>SIGKILL<\/code>. \u0422\u043e\u0447\u043d\u0435\u0435, \u043e <code>SIGKILL<\/code> \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0443\u0437\u043d\u0430\u0442\u044c \u043d\u0435 \u0443\u0441\u043f\u0435\u0432\u0430\u0435\u0442, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u043f\u0438\u0441\u0430\u043d \u0437\u0434\u0435\u0441\u044c: <a href=\"https:\/\/major.io\/2010\/03\/18\/sigterm-vs-sigkill\/\">SIGTERM vs. SIGKILL<\/a> \u0438 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix_signal#SIGINT\">Unix Signal<\/a>.<\/p>\n<p>  \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043d\u0430 Go \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432 \u043c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u044f\u0441\u043d\u043e, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0438\u0437-\u0437\u0430 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 <code>Go<\/code> \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043e\u043c \u044f\u0437\u044b\u043a\u0430. \u0412\u043d\u0443\u0442\u0440\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043c\u043e\u0433\u0443\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438:<\/p>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Connection_pool\">Connection pool<\/a> \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0411\u0414;<\/li>\n<li>Consumer \u0434\u043b\u044f pub\/sub \u043e\u0447\u0435\u0440\u0435\u0434\u0438;<\/li>\n<li>Publisher \u0434\u043b\u044f pub\/sub \u043e\u0447\u0435\u0440\u0435\u0434\u0438;<\/li>\n<li>N \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432;<\/li>\n<li>\u041a\u044d\u0448 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438;<\/li>\n<li>\u041e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u043b\u043e\u0433\u043e\u0432;<\/li>\n<\/ul>\n<p>  \u0417\u0434\u0435\u0441\u044c \u043d\u0435\u0442 \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u0432\u0435\u0440\u0445\u044a\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e (\u0438\u0437\u0432\u0438\u043d\u0438\u0442\u0435, \u0435\u0441\u043b\u0438 \u043e\u0431\u0438\u0434\u0435\u043b), \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u043d\u0430 \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u0430\u044e\u0442 \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0432 \u0444\u043e\u043d\u0435 (<a href=\"https:\/\/gobyexample.com\/goroutines\">go-\u0440\u0443\u0442\u0438\u043d\u044b<\/a>), \u0438 \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/gobyexample.com\/channels\">go-\u043a\u0430\u043d\u0430\u043b\u044b<\/a> (\u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438). \u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0442\u0430\u043a\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 <a href=\"http:\/\/habrahabr.ru\/post\/249183\/\">\u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0439<\/a> \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b.<\/p>\n<p>  \u0418 \u0441 \u0437\u0430\u043f\u0443\u0441\u043a\u043e\u043c \u0432\u0441\u0451 \u043f\u0440\u0435\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u043f\u0443\u043b \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0411\u0414, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u043b \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439. \u0417\u0430\u0442\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043a\u044d\u0448 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u0417\u0430\u0442\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c publisher, \u0435\u0441\u043b\u0438 \u043d\u0435 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u043b \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439. \u0417\u0430\u0442\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0444\u0430\u0439\u043b\u044b \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043b\u043e\u0433\u0438. \u0417\u0430\u0442\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u0434\u0430 \u043f\u043e\u0431\u043e\u043b\u044c\u0448\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 consumer, \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0411\u0414 \u0438 \u0447\u0442\u043e-\u0442\u043e \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u043a\u044d\u0448\u0435, \u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u0432 publisher. \u0410\u0445 \u0434\u0430, \u0435\u0449\u0451 \u0441\u043e\u0431\u044b\u0442\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u0432 \u043b\u043e\u0433\u0438, \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437 \u0442\u0435\u0445 \u0436\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0418, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0430\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0451 \u044d\u0442\u043e \u043e\u0442\u043a\u0440\u044b\u0432 \u043a\u0440\u0430\u043d\u0442\u0438\u043a consumer, \u0430 \u0435\u0441\u043b\u0438 \u043d\u0435 \u043e\u0442\u0440\u044b\u043b\u0441\u044f \u2014 \u0432\u044b\u0445\u043e\u0434\u0438\u043c.<\/p>\n<p>  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0432 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u044d\u0442\u0430\u043f\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0443\u043b\u0435\u0432\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432\u0441\u0451 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0442\u043a\u0440\u043e\u0435\u043c \u043a\u0440\u0430\u043d\u0442\u0438\u043a. \u0418 \u0432\u043e\u0442 \u043e\u0442\u043a\u0440\u044b\u043b\u0438 \u043a\u0440\u0430\u043d\u0442\u0438\u043a, \u0430 \u0447\u0435\u0440\u0435\u0437 5 \u043c\u0438\u043d\u0443\u0442 \u043d\u0430\u043c \u0441\u0440\u043e\u0447\u043d\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0439\u0442\u0438, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0432\u0441\u0451, \u0434\u0430 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438 \u0447\u0438\u0441\u0442\u043e.<\/p>\n<p>  \u0417\u0430\u0447\u0435\u043c? \u0410 \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u0432\u0441\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438\u0437 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430 \u043c\u043e\u0433\u043b\u0438 \u0443\u0441\u043f\u0435\u0442\u044c \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0411\u0414, \u0434\u0430 \u0438 \u0442\u0435, \u0447\u0442\u043e \u0431\u044b\u043b\u0438 \u0441\u0447\u0438\u0442\u0430\u043d\u044b \u0438\u0437 \u043a\u0430\u043d\u0430\u043b\u0430, \u043c\u043e\u0433\u043b\u0438 \u043d\u0435 \u0443\u0441\u043f\u0435\u0442\u044c \u0434\u043e\u0439\u0442\u0438 \u0434\u043e \u0411\u0414 \u043f\u043e \u0441\u0435\u0442\u0438. \u0418 \u043d\u0435 \u0432\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u043e\u0433\u043b\u0438 \u0443\u0441\u043f\u0435\u0442\u044c \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 pub\/sub \u043e\u0447\u0435\u0440\u0435\u0434\u044c. \u041d\u0435 \u0432\u0441\u0435 \u0432\u043e\u0440\u043a\u0435\u0440\u044b \u043c\u043e\u0433\u043b\u0438 \u0443\u0441\u043f\u0435\u0442\u044c \u0441\u0434\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043a\u0430\u043d\u0430\u043b\u044b. \u041f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u043e\u0440\u043a\u0435\u0440\u0430\u043c\u0438 \u043c\u043e\u0433\u043b\u043e \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043c\u043e\u0433\u043b\u0430 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u0447\u0438\u0442\u0430\u043d\u043d\u043e\u0439 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 pub\/sub \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043d\u043e \u0435\u0449\u0451 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u043e\u0439 \u0432\u043e\u0440\u043a\u0435\u0440\u0430\u043c\u0438. \u041a\u044d\u0448 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0434\u0430\u043c\u043f\u043b\u0435\u043d \u043d\u0430 \u0434\u0438\u0441\u043a \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u0430 \u0435\u0449\u0451 \u0432\u0441\u0435 \u0431\u0443\u0444\u0435\u0440\u044b \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043b\u043e\u0433\u043e\u0432 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0447\u0438\u0449\u0435\u043d\u044b \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0444\u0430\u0439\u043b\u044b. \u0412\u0441\u0451 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u0441 \u0446\u0435\u043b\u044c\u044e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0439 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0444\u043e\u043d\u043e\u0432\u044b\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u043e\u0431\u0440\u0435\u0447\u0451\u043d \u0438\u043c\u0435\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 <i>\u043d\u0430\u0434\u0451\u0436\u043d\u043e\u0433\u043e<\/i> \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0418 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0440\u0430\u0434\u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0433\u043e \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f <i>\u00abBye bye&#8230;\u00bb<\/i> \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438, \u0430 \u043a\u0430\u043a \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043c\u0431\u0430\u0439\u043d\u0430.<\/p>\n<h4>2. \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438<\/h4>\n<p>  \u0412 Go \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u2014 <a href=\"http:\/\/blog.golang.org\/defer-panic-and-recover\">defer<\/a>, \u044d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0431\u0443\u0434\u0443\u0447\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0451\u043d\u043d\u044b\u043c \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u0435\u0451 \u0432 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0422\u0430\u043a\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0438\u043d\u043e\u0439 \u0440\u0430\u0437 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u0430\u043c\u0438 \u0438 \u043f\u0440\u043e\u0447\u0438\u043c\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435. \u042d\u0444\u0444\u0435\u043a\u0442 <code>defer<\/code> \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u043d\u0438\u043a\u0430 (=\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435), \u0442\u043e \u0435\u0441\u0442\u044c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0439 \u0432 deferred-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u044e \u0431\u044b\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u043c, \u0430 \u0441\u0430\u043c\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0439\u043c\u0430\u043d\u044b \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b.<\/p>\n<pre><code class=\"go\">func Checked() { defer func() { \t\/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0431\u044b\u043b\u0430 \u043b\u0438 \u043f\u0430\u043d\u0438\u043a\u0430 \tif x := recover(); x != nil { \t\t\/\/ \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u043b\u043e\u0433, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0432\u0435\u0440\u0445 \t} }()  \/\/ \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0434\u0435\u043b\u0430\u0435\u043c, \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u043d\u0438\u043a\u0430 } <\/code><\/pre>\n<p>  \u041d\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u0438\u043d \u0437\u043b\u043e\u0441\u0442\u043d\u044b\u0439 \u0430\u043d\u0442\u0438\u043f\u0430\u0442\u0442\u0435\u0440\u043d, \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e <code>defer<\/code> \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>main<\/code>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"go\">func main() { \tdefer doCleanup()  \t\/\/ \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0441\u0435\u0432\u0434\u043e\u0440\u0430\u0431\u043e\u0442\u044b \tfmt.Println(&quot;10 seconds to go...&quot;) \t&lt;-time.Tick(10 * time.Second) } <\/code><\/pre>\n<p>  \u041a\u043e\u0434 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0438 \u0434\u0430\u0436\u0435 \u043f\u0430\u043d\u0438\u043a\u0438, \u043d\u043e \u043b\u044e\u0434\u0438 \u0437\u0430\u0431\u044b\u043b\u0438 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e <code>defer<\/code> \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u0441\u0438\u0433\u043d\u0430\u043b\u0430 \u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 (\u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <a href=\"http:\/\/linux.die.net\/man\/2\/exit\">syscall exit<\/a>, \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 Go: <i>\u00abThe program terminates immediately; deferred functions are not run.\u00bb<\/i>).<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0441\u0438\u0433\u043d\u0430\u043b\u044b \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043b\u043e\u0432\u0438\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u00ab\u043f\u043e\u0434\u043f\u0438\u0441\u0430\u0432\u0448\u0438\u0441\u044c\u00bb \u043d\u0430 \u043d\u0443\u0436\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432. \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0451\u043d\u043d\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 (\u0441\u0443\u0434\u044f \u043f\u043e \u043e\u0442\u0432\u0435\u0442\u0430\u043c \u043d\u0430 StackOverflow) \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"https:\/\/godoc.org\/os\/signal#Notify\">signal.Notify<\/a>, \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, \tsyscall.SIGHUP, \tsyscall.SIGINT, \tsyscall.SIGTERM, \tsyscall.SIGQUIT) go func() { \ts := &lt;-sigChan \t\/\/ \u043f\u043e\u0439\u043c\u0430\u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 }() <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0441\u043a\u0440\u044b\u0442\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0431\u044b\u043b \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043d \u043f\u0430\u043a\u0435\u0442 <a href=\"http:\/\/github.com\/xlab\/closer\">xlab\/closer<\/a>, \u043e \u043d\u0451\u043c \u043f\u043e\u0439\u0434\u0451\u0442 \u0440\u0435\u0447\u044c \u0434\u0430\u043b\u044c\u0448\u0435.<\/p>\n<h4>3. Closer<\/h4>\n<p>  \u0418\u0442\u0430\u043a, \u043f\u0430\u043a\u0435\u0442 <code>closer<\/code> \u0431\u0435\u0440\u0451\u0442 \u043d\u0430 \u0441\u0435\u0431\u044f \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b\u044b, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0438\u0445 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438. \u041f\u0430\u043a\u0435\u0442 \u043f\u043e\u0442\u043e\u043a\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0435\u043d, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0430\u0432\u043b\u044f\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043e\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u0443\u043c\u0430\u0442\u044c \u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0437\u0434\u0435\u0441\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f\u0445 \u0433\u043e\u043d\u043a\u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Close\">closer.Close<\/a> \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. API \u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 5 \u0444\u0443\u043d\u043a\u0446\u0438\u0439: <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Init\">Init<\/a>, <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Bind\">Bind<\/a>, <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Checked\">Checked<\/a>, <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Hold\">Hold<\/a> \u0438 <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Close\">Close<\/a>. Init \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u043f\u0446\u0438\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445.<\/p>\n<p>  \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432: <code>syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGABRT<\/code>.<\/p>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u044b\u0447\u043d\u044b\u0439<\/h5>\n<p>  <img decoding=\"async\" src=\"http:\/\/up.xlab.is\/image\/431y0f1m010B\/Image%202015-06-19%20at%205.18.39%20pm.png\"\/>  <\/p>\n<pre><code class=\"go\">func main() { \tcloser.Bind(cleanup)  \tgo func() { \t\t\/\/ \u0434\u0435\u043b\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \t\tfmt.Println(&quot;10 seconds to go...&quot;) \t\t&lt;-time.Tick(10 * time.Second) \t\t\/\/ \u043f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \t\tcloser.Close() \t}()  \t\/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u043d \u0432\u044b\u0445\u043e\u0434 \u2014 \u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443 \u0438\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 closer.Close \tcloser.Hold() }  func cleanup() { \tfmt.Print(&quot;Hang on! I'm closing some DBs, wiping some trails..&quot;) \t&lt;-time.Tick(3 * time.Second) \tfmt.Println(&quot;  Done.&quot;) } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439<\/h5>\n<p>  <img decoding=\"async\" src=\"http:\/\/up.xlab.is\/image\/233t340J1m2k\/Image%202015-06-19%20at%205.19.26%20pm.png\"\/><br \/>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Bind\">closer.Checked<\/a> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043b\u043e\u0432\u0438\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u0417\u0434\u0435\u0441\u044c \u043a\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u0435\u043d \u043e\u0442 \u043d\u0443\u043b\u044f, \u043f\u0440\u0438\u0447\u0451\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043f\u0430\u043a\u0435\u0442 <code>closer<\/code>.  <\/p>\n<pre><code class=\"go\">func main() { \tcloser.Bind(cleanup) \tcloser.Checked(run, true) }  func run() error { \tfmt.Println(&quot;Will throw an error in 10 seconds...&quot;) \t&lt;-time.Tick(10 * time.Second) \treturn errors.New(&quot;KAWABANGA!&quot;) }  func cleanup() { \tfmt.Print(&quot;Hang on! I'm closing some DBs, wiping some trails...&quot;) \t&lt;-time.Tick(3 * time.Second) \tfmt.Println(&quot;  Done.&quot;) } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u043f\u0430\u043d\u0438\u043a\u043e\u0439 (\u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c)<\/h5>\n<p>  <img decoding=\"async\" src=\"http:\/\/up.xlab.is\/image\/4142391V2s09\/Image%202015-06-19%20at%205.20.04%20pm.png\"\/>  <\/p>\n<pre><code class=\"go\">func main() { \tcloser.Bind(cleanup) \tcloser.Checked(run, true) }  func run() error { \tfmt.Println(&quot;Will panic in 10 seconds...&quot;) \t&lt;-time.Tick(10 * time.Second) \tpanic(&quot;KAWABANGA!&quot;) \treturn nil }  func cleanup() { \tfmt.Print(&quot;Hang on! I'm closing some DBs, wiping some trails...&quot;) \t&lt;-time.Tick(3 * time.Second) \tfmt.Println(&quot;  Done.&quot;) } <\/code><\/pre>\n<p>  \u0422\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u043a\u043e\u0434\u043e\u0432 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>   \u0421\u043e\u0431\u044b\u0442\u0438\u0435       | \u041a\u043e\u0434 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f    ------------- | -------------    error = nil   | 0 (\u0443\u0441\u043f\u0435\u0445)    error != nil  | 1 (\u043e\u0448\u0438\u0431\u043a\u0430)    panic         | 1 (\u043e\u0448\u0438\u0431\u043a\u0430) <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0435\u0440\u0432\u043e\u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430, \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Go \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u00ab\u0447\u0438\u0441\u0442\u043e\u0433\u043e\u00bb \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f. \u0412 Go \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0435\u0439 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b, \u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Close, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b \u0444\u0438\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b \u0432\u0441\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u044d\u0442\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u0417\u043d\u0430\u0447\u0438\u0442, \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0448\u0435\u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438\u0437 \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 <code>Close()<\/code> \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439, \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0440\u0430\u043d\u0442\u0438\u043a consumer \u043e\u0447\u0435\u0440\u0435\u0434\u0438 pub\/sub, \u043d\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0437\u0430\u0442\u0435\u043c \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u0436\u0434\u0451\u0442\u0441\u044f, \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0432\u043e\u0440\u043a\u0435\u0440\u044b \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u0441\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0441 \u0434\u0438\u0441\u043a\u043e\u043c \u043a\u044d\u0448, \u0437\u0430\u043a\u0440\u044b\u0442 \u043a\u0430\u043d\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0411\u0414, \u0437\u0430\u043a\u0440\u044b\u0442 \u043a\u0430\u043d\u0430\u043b publisher, \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0438 \u0437\u0430\u043a\u0440\u044b\u0442\u044b \u0444\u0430\u0439\u043b\u044b \u043b\u043e\u0433\u043e\u0432, \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0431\u0443\u0434\u0443\u0442 \u0437\u0430\u043a\u0440\u044b\u0442\u044b \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a \u0411\u0414 \u0438 \u0441\u0430\u043c publisher. \u041d\u0430 \u0441\u043b\u043e\u0432\u0430\u0445 \u0437\u0432\u0443\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e, \u043d\u043e \u043d\u0430 \u0434\u0435\u043b\u0435 \u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0438\u0448\u044c \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 Close \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438 \u0432 <code>main<\/code> \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer#Bind\">closer.Bind<\/a>. \u042d\u0441\u043a\u0438\u0437 <code>main<\/code> \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"go\">func main() { defer closer.Close()  pool, _ := xxx.NewPool() closer.Bind(pool.Close)  pub, _ := yyy.NewPublisher() closer.Bind(function(){ \tpub.Stop() \t&lt;-pub.StopChan })  wChan := make(chan string, BUFFER_SIZE) workers, _ := zzz.NewWorkgroup(pool, pub, wChan) closer.Bind(workers.Close)  sub, _ := yyy.NewConsumer() closer.Bind(sub.Stop)  \/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0437\u043e\u0432 (\u0438\u043d\u0430\u0447\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 closer.Hold) sub.Consume(wChan) } <\/code><\/pre>\n<p>  \u0423\u0434\u0430\u0447\u043d\u043e\u0439 \u0432\u0430\u043c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438!     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/260661\/\"> http:\/\/habrahabr.ru\/post\/260661\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043c\u0435\u0442\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0442\u0440\u043e\u043d\u0443\u0442\u0430 \u0442\u0435\u043c\u0430 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u00ab\u0447\u0438\u0441\u0442\u043e\u0433\u043e\u00bb \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Go.<br \/>  \u0427\u0438\u0441\u0442\u044b\u043c \u0432\u044b\u0445\u043e\u0434\u043e\u043c \u044f \u043d\u0430\u0437\u044b\u0432\u0430\u044e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f (\u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443 \u0438\u043b\u0438 \u043f\u043e \u043b\u044e\u0431\u044b\u043c \u0438\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043a\u0440\u043e\u043c\u0435 system failure), \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0438 \u0432\u044b\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u043e\u0436\u0435\u043d \u0434\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u0414\u0430\u043b\u0435\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u044e \u0441\u0432\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 \u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445.<\/p>\n<p>  TL;DR: <a href=\"https:\/\/github.com\/xlab\/closer\">github.com\/xlab\/closer<\/a> <a href=\"https:\/\/godoc.org\/github.com\/xlab\/closer\"><img decoding=\"async\" src=\"https:\/\/godoc.org\/github.com\/xlab\/closer?status.svg\" alt=\"GoDoc\"\/><\/a>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-259721","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/259721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=259721"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/259721\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=259721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=259721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=259721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}