{"id":335823,"date":"2022-07-18T09:00:08","date_gmt":"2022-07-18T09:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=335823"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=335823","title":{"rendered":"<span>Go sync\/singleflight: \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430<\/h3>\n<p>\u041d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u0434\u043e\u0432\u0435\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0441 \u0447\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u043d\u0430 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0445\u043e\u043b\u043e\u0434\u043d\u043e\u043c \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0430 \u043f\u043e\u0440\u043e\u0439 \u0438 \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0430\u043d\u0438\u0438 \u043a\u044d\u0448\u0430. \u0410\u043d\u0430\u043b\u0438\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0438\u0437 \u0442\u044b\u0441\u044f\u0447 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0447\u0430\u0449\u0435 \u0434\u0440\u0443\u0433\u0438\u0445. \u041d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u043e, \u0447\u0442\u043e \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u0448\u0435\u0434\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443\u0441\u043f\u0435\u0432\u0430\u043b\u043e \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u0442\u044c \u0435\u0449\u0435 \u0441 \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0449\u0430\u0434\u043d\u043e \u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0432 \u0442\u043e\u0442 \u0440\u0430\u0437 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0435 \u0441\u0430\u043c\u044b\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c\u0435\u0434\u043d\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043a\u0435\u0439\u0441\u043e\u043c \u0443\u0436\u0435 \u043d\u0430 Go-\u0441\u0442\u0435\u043a\u0435, \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0432 Google, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0435\u0436\u043d\u0438\u0435 \u0433\u0440\u0430\u0431\u043b\u0438. \u041a \u0441\u0432\u043e\u0435\u0439 \u0440\u0430\u0434\u043e\u0441\u0442\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"http:\/\/golang.org\/x\/sync\/singleflight\" rel=\"noopener noreferrer nofollow\"><u>singleflight (\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0444 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e),<\/u><\/a> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0447\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u0434\u0435\u0441\u044f\u0442\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 &#171;\u0442\u044f\u0436\u0435\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438 \u0442\u043e\u043c\u0443 \u0436\u0435 \u043a\u043b\u044e\u0447\u0443 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c singleflight.Group. <br \/>\u0412 \u0440\u0430\u0431\u043e\u0442\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0436\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0443\u043f\u0430\u0434\u0435\u0442 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439, \u0442\u043e \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u043e\u0436\u0435 \u0432\u0435\u0440\u043d\u0443\u0442 \u044d\u0442\u0443 \u0436\u0435 \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0430\u043a\u0435\u0442\u0435 \u043d\u0430\u0448\u0435\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445, \u0447\u0442\u043e \u0438 \u043f\u043e\u0431\u0443\u0434\u0438\u043b\u043e \u043c\u0435\u043d\u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435.<\/p>\n<hr\/>\n<p>\u0421\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043b\u0438 \u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0441 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0435\u0439, \u0447\u0442\u043e \u0432 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0435\u0442\u0438, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f) \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430? <br \/>\u0415\u0441\u043b\u0438 \u0412\u0430\u0448 \u043e\u0442\u0432\u0435\u0442 \u00ab\u0434\u0430\u00bb, \u0412\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 <a href=\"https:\/\/pkg.go.dev\/golang.org\/x\/sync\/singleflight\" rel=\"noopener noreferrer nofollow\"><u>sync\/singleflight<\/u><\/a>.<\/p>\n<p>\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u043f\u0430\u043a\u0435\u0442 singleflight \u043d\u0435 \u043d\u043e\u0432. \u041e\u043d \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e (<em>\u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430: \u0441 5 \u043e\u043a\u0442\u044f\u0431\u0440\u044f 2016<\/em>) \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Go \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/pkg.go.dev\/cmd\/go#hdr-Internal_Directories\" rel=\"noopener noreferrer nofollow\"><u>(\u0434\u043b\u044f \u043d\u0435\u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0445: \u0438\u043c\u043f\u043e\u0440\u0442 \u043f\u0443\u0442\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u00abinternal\u00bb, \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d, \u0435\u0441\u043b\u0438 \u043a\u043e\u0434 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u0430, \u0438\u043c\u0435\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0440\u0435\u043d\u044c \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0435 \u00abinternal\u00bb \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430).<\/u><\/a><\/p>\n<p>\u041a\u0430\u043a \u0438\u0442\u043e\u0433 \u043f\u0430\u043a\u0435\u0442 \u0431\u044b\u043b \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043d\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439, \u043d\u0438 \u0441\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439, \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"http:\/\/golang.org\/x\" rel=\"noopener noreferrer nofollow\">golang.org\/x<\/a>.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0437\u0433\u043e\u0432\u043e\u0440\u043e\u0432, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 DNS \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041d\u043e \u043d\u0435 \u0431\u0443\u0434\u0443 \u0437\u0430\u043e\u0441\u0442\u0440\u044f\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0442.\u043a. <a href=\"https:\/\/github.com\/golang\/go\/blob\/f54c0db859867f415a0702c8ceaa30c1a8976b0b\/src\/net\/lookup.go#L193-L197\" rel=\"noopener noreferrer nofollow\"><u>\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0442 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c; \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d singleflight<\/u><\/a>. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0432\u043d\u0435\u0448\u043d\u0435\u043c\u0443 API.<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\" )  func main() { \/\/ Create an HTTP handler http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { \/\/ Retrieve GitHub's API status status, err := githubStatus() if err != nil { \/\/ Send an internal error response if we were unable to retrieve the status. \/\/ note: not a great idea to raw errors to a client, but this is just a demonstration. http.Error(w, err.Error(), http.StatusInternalServerError) return }  \/\/ Log the status so we can see what's happening from the server's perspective. log.Printf(\"\/github handler requst: status %q\", status)  \/\/ Write the response to the client. fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { \/\/ Log the start and end of the function so we can see how many times it's called. log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\") \/\/ The defer causes this to be logged after the function's return statement.  \/\/ Atrificially delay this function to emulate a long running task time.Sleep(1 * time.Second)  \/\/ Make a request to the GitHub Status API resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  \/\/ Check that we got a good response. if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  \/\/ Anonymous struct to extract the status from the response r := struct{ Status string }{}  \/\/ Decode the JSON response err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a GitHub \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 API \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432 \u0432\u0438\u0434\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f. \u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u043d\u043e \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/tsenart\/vegeta\" rel=\"noopener noreferrer nofollow\"><u>vegeta, \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f HTTP<\/u><\/a>, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 Go.<\/p>\n<pre><code class=\"bash\"># echo \u201cGET http:\/\/localhost:8080\/github\" | vegeta attack -duration=1s -rate=10 | vegeta report Requests [total, rate] 10, 11.11 Duration [total, attack, wait] 2.011072002s, 899.999ms, 1.111073002s Latencies [mean, 50, 95, 99, max] 1.269280654s, 1.222384648s, 1.445123078s, 1.445123078s, 1.526538858s Bytes In [total, mean] 210, 21.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:10 Error Set:<\/code><\/pre>\n<p>\u0412\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e vegeta 10 GET \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0430 \u043d\u0430\u0448 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 1 \u0441\u0435\u043a\u0443\u043d\u0434\u044b.<\/p>\n<p>\u0418\u0437 \u043b\u043e\u0433\u043e\u0432 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f githubStatus() \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0430\u0441\u044c 10 \u0440\u0430\u0437, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"go\"># go run e1_without_singleflight.go 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:31 Making request to GitHub API 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u041f\u043e\u0447\u0442\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0435\u0442 10 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043d\u0430\u0448 \u043a\u043e\u0434 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442 10 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u041a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u041c\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043a\u0430\u043d\u0430\u043b \u0441\u0432\u044f\u0437\u0438 \u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u044b\u0435 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a API GitHub.\u00a0<\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u0430\u043a\u0435\u0442 singleflight \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0430\u0441.\u00a0<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { \/\/ We need a Group to use singleflight. var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { \/\/ This time we'll wrap the githubStatus() call with singleflight's Group.Do() \/\/ Do takes a key (more on this later) and a function that returns a interface{} and an error. v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { \/\/ githubStatus() returns string, error, which statifies interface{}, error, so we can return the result directly. return githubStatus() }) \/\/ Do returns an interface{}, error, and a bool which indicates whether multiple calls to the function shared the same result.  \/\/ Check the error, as before. if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  \/\/ We know that v will be a string, so we'll use a type assertion. status := v.(string)  \/\/ Update the log statement so we can see if the results were shared. log.Printf(\"\/github handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { \/\/ No changes made to this function other than removing the comments for brevity. log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 vegeta \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043f\u0435\u0440\u044c?<\/p>\n<pre><code class=\"bash\"># go run e2_with_singleflight.go 2016\/10\/11 13:02:49 Making request to GitHub API 2016\/10\/11 13:02:51 Request to GitHub API Complete 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true<\/code><\/pre>\n<p>\u0413\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0443\u0447\u0448\u0435! \u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u043c\u044b \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a GitHub, \u0430 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u0435\u0440\u043d\u0443\u043b\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0435\u0440\u0432\u043e\u0433\u043e. \u0412\u0430\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e singleflight \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 &#8212; \u043e\u043d \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432 \u043a\u044d\u0448\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.\u00a0<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0432 vegeta \u043d\u0430 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b (\u043d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 githubStatus() \u0435\u0441\u0442\u044c \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u0432 1 \u0441\u0435\u043a\u0443\u043d\u0434\u0443).<\/p>\n<pre><code class=\"bash\"># echo \u201cGET http:\/\/localhost:8080\/github\" | vegeta attack -duration=2s -rate=10 | vegeta report Requests [total, rate] 20, 10.53 Duration [total, attack, wait] 2.609915466s, 1.899999924s, 709.915542ms Latencies [mean, 50, 95, 99, max] 807.15366ms, 809.915542ms, 1.373483043s, 1.373483043s, 1.473610875s Bytes In [total, mean] 420, 21.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:20 Error Set: 2016\/10\/11 13:17:07 Making request to GitHub API 2016\/10\/11 13:17:08 Request to GitHub API Complete 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:08 Making request to GitHub API 2016\/10\/11 13:17:10 Request to GitHub API Complete 2016\/10\/11 13:17:10 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:10 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:10 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:10 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:17:10 \/github handler requst: status \u201cgood\u201d, shared result true<\/code><\/pre>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a API GitHub \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0442\u043e\u0440\u043e\u0439. \u041e\u0442\u043c\u0435\u0442\u0438\u043c \u0435\u0449\u0435 \u0440\u0430\u0437: singleflight \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0430 \u043d\u0435 \u0434\u043b\u044f \u043a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0435\u0439<\/h3>\n<p>\u041c\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0442\u0435\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0435\u0439. \u041a\u043b\u044e\u0447 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c. \u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u043b\u0443\u0447\u0448\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0443\u0441\u0430 BitBucket API.<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { return githubStatus() }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  status := v.(string)  log.Printf(\"\/github handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.HandleFunc(\"\/bitbucket\", func(w http.ResponseWriter, r *http.Request) { \/\/ We can use the same singleflight.Group as long as we use a different key v, err, shared := requestGroup.Do(\"bitbucket\", func() (interface{}, error) { return bitbucketStatus() }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  status := v.(string)  log.Printf(\"\/bitbucket handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"BitBucket Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }  \/\/ bitbucketStatus retrieves BitBucket's API status func bitbucketStatus() (string, error) { log.Println(\"Making request to BitBucket API\") defer log.Println(\"Request to BitBucket API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.bitbucket.org\/api\/v2\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status struct{ Description string } }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status.Description, err }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0439 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 JSON, \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u044b GitHub \u0438 BitBucket \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u043d\u044b. \u041a\u0430\u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043a\u043e\u0434\u0435, \u0434\u0440\u0443\u0433\u043e\u0439 \u043a\u043b\u044e\u0447 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 singleflight.Group.<\/p>\n<pre><code class=\"bash\">echo \u201cGET http:\/\/localhost:8080\/github\\nGET http:\/\/localhost:8080\/bitbucket\" | vegeta attack -duration=1s -rate=10 | vegeta report Requests [total, rate] 10, 11.11 Duration [total, attack, wait] 1.554732379s, 899.999947ms, 654.732432ms Latencies [mean, 50, 95, 99, max] 1.069608701s, 984.508873ms, 1.384495166s, 1.384495166s, 1.5546515s Bytes In [total, mean] 320, 32.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:10 Error Set: # go run e3_bitbucket.go 2016\/10\/11 13:27:48 Making request to GitHub API 2016\/10\/11 13:27:48 Making request to BitBucket API 2016\/10\/11 13:27:49 Request to BitBucket API Complete 2016\/10\/11 13:27:49 \/bitbucket handler requst: status \u201cAll Systems Operational\u201d, shared result true 2016\/10\/11 13:27:49 \/bitbucket handler requst: status \u201cAll Systems Operational\u201d, shared result true 2016\/10\/11 13:27:49 \/bitbucket handler requst: status \u201cAll Systems Operational\u201d, shared result true 2016\/10\/11 13:27:49 \/bitbucket handler requst: status \u201cAll Systems Operational\u201d, shared result true 2016\/10\/11 13:27:49 \/bitbucket handler requst: status \u201cAll Systems Operational\u201d, shared result true 2016\/10\/11 13:27:49 Request to GitHub API Complete 2016\/10\/11 13:27:49 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:27:49 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:27:49 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:27:49 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:27:49 \/github handler requst: status \u201cgood\u201d, shared result true<\/code><\/pre>\n<p>\u041d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 githubStatus() \u0438 bitbucketStatus() \u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u044f\u0432\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f.\u00a0<\/p>\n<p>\u0420\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u043c\u044b \u201c\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u201d \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438 \u0442\u0435\u043c \u0436\u0435 \u043a\u043b\u044e\u0447\u043e\u043c.<\/p>\n<pre><code class=\"go\"> package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { return githubStatus() }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  status := v.(string)  log.Printf(\"\/github handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.HandleFunc(\"\/bitbucket\", func(w http.ResponseWriter, r *http.Request) { \/\/ Oops! Same key! v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { return bitbucketStatus() }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  status := v.(string)  log.Printf(\"\/bitbucket handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"BitBucket Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }  \/\/ bitbucketStatus retrieves BitBucket's API status func bitbucketStatus() (string, error) { log.Println(\"Making request to BitBucket API\") defer log.Println(\"Request to BitBucket API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.bitbucket.org\/api\/v2\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status struct{ Description string } }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status.Description, err }<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<pre><code class=\"bash\"># go run e4_same_key.go 2016\/10\/11 13:34:39 Making request to GitHub API 2016\/10\/11 13:34:40 Request to GitHub API Complete 2016\/10\/11 13:34:40 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/bitbucket handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/bitbucket handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/bitbucket handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/bitbucket handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:34:40 \/bitbucket handler requst: status \u201cgood\u201d, shared result true<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u043e \u043e\u0436\u0438\u0434\u0430\u0442\u044c, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a BitBucket \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043b\u044f GitHub (\u0438\u043b\u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u043c).<\/p>\n<h2>\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0430<\/h2>\n<p>\u041f\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u201c\u0437\u0430\u0431\u044b\u0442\u044c\u201d \u0440\u0430\u043d\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043b\u0438\u0448\u044c \u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u0434\u0443\u0442 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 250 \u043c\u0441 \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u0446\u0438\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { \/\/ Start a goroutine which deletes the \"github\" key after 250ms. go func() { time.Sleep(250 * time.Millisecond) log.Println(\"Deleting \\\"github\\\" key\") requestGroup.Forget(\"github\") }()  return githubStatus() }) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  status := v.(string)  log.Printf(\"\/github handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"bash\"># echo \u201cGET http:\/\/localhost:8080\/github\" | vegeta attack -duration=1s -rate=10 | vegeta report Requests [total, rate] 10, 11.11 Duration [total, attack, wait] 2.007352469s, 899.999899ms, 1.10735257s Latencies [mean, 50, 95, 99, max] 1.214591847s, 1.13383041s, 1.432634042s, 1.432634042s, 1.53263476s Bytes In [total, mean] 210, 21.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:10 Error Set: # go run e5_forget.go 2016\/10\/11 13:45:21 Making request to GitHub API 2016\/10\/11 13:45:21 Deleting \u201cgithub\u201d key 2016\/10\/11 13:45:21 Making request to GitHub API 2016\/10\/11 13:45:21 Deleting \u201cgithub\u201d key 2016\/10\/11 13:45:21 Making request to GitHub API 2016\/10\/11 13:45:22 Deleting \u201cgithub\u201d key 2016\/10\/11 13:45:22 Making request to GitHub API 2016\/10\/11 13:45:22 Deleting \u201cgithub\u201d key 2016\/10\/11 13:45:22 Request to GitHub API Complete 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 Request to GitHub API Complete 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 Request to GitHub API Complete 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:22 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:45:23 Request to GitHub API Complete 2016\/10\/11 13:45:23 \/github handler requst: status \u201cgood\u201d, shared result false<\/code><\/pre>\n<p>\u0421\u0443\u0434\u044f \u043f\u043e \u043b\u043e\u0433\u0430\u043c, \u0431\u044b\u043b\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0447\u0435\u0442\u044b\u0440\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0447\u0435\u0440\u0435\u0437 \u043a\u0430\u0436\u0434\u044b\u0435 250 \u043c\u0441, \u0445\u043e\u0442\u044f \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0441\u0442\u043e\u043b\u044c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u043a\u0430\u043a \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b.\u00a0<\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u044e\u0447\u0430 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0432\u0448\u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0430. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n<h3>\u041c\u0435\u0442\u043e\u0434 DoChan<\/h3>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u044b Do() \u043f\u0430\u043a\u0435\u0442 singleflight \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 DoChan().\u00a0<\/p>\n<p>\u041e\u043d \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434 Do(), \u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0430\u043d\u0430\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043e \u043c\u0435\u0440\u0435 \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 select.<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { \/\/ With DoChan we get a channel of singleflight.Result ch := requestGroup.DoChan(\"github\", func() (interface{}, error) { return githubStatus() })  \/\/ Create our timeout timeout := time.After(500 * time.Millisecond)  var result singleflight.Result select { case &lt;-timeout: \/\/ Timeout elapsed, send a timeout message (504) log.Println(\"\/github handler timed out\") http.Error(w, \"github request timed out\", http.StatusGatewayTimeout) return case result = &lt;-ch: \/\/ Received result from channel }  \/\/ singleflight.Result is the same three values as returned from Do(), but wrapped \/\/ in a struct. We can use the same logic as before, buy referencing the struct fields.  if result.Err != nil { http.Error(w, result.Err.Error(), http.StatusInternalServerError) return }  status := result.Val.(string)  log.Printf(\"\/github handler requst: status %q, shared result %t\", status, result.Shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0431\u044b\u043b\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e, \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044e \u043f\u043e \u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u0443. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0432\u0430\u043c \u043f\u043e\u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c, \u0435\u0441\u043b\u0438 \u0412\u044b \u043d\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0442\u0430\u0439\u043c-\u0430\u0443\u0442\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 select.<\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"bash\"># echo \u201cGET http:\/\/localhost:8080\/github\" | vegeta attack -duration=1s -rate=10 | vegeta report Requests [total, rate] 10, 11.11 Duration [total, attack, wait] 1.403848483s, 899.99994ms, 503.848543ms Latencies [mean, 50, 95, 99, max] 506.630505ms, 506.02365ms, 509.411055ms, 509.411055ms, 511.554041ms Bytes In [total, mean] 250, 25.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 0.00% Status Codes [code:count] 504:10 Error Set: 504 Gateway Timeout<\/code><\/pre>\n<p>Vegeta \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 504 Gateway Time Out.<\/p>\n<pre><code class=\"bash\"># go run e6_dochan.go 2016\/10\/11 14:14:32 Making request to GitHub API 2016\/10\/11 14:14:32 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:33 \/github handler timed out 2016\/10\/11 14:14:34 Request to GitHub API Complete<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438\u0441\u0442\u0435\u043a\u043b\u043e.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u043e\u043b\u0438\u043b\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0432\u0435\u0442\u0430 \u043d\u0430 \u043f\u0430\u043a\u0435\u0442 singleflight. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0412\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0434\u0435\u043d\u044c, \u043e\u0434\u043d\u0430\u043a\u043e, \u0443\u0432\u0435\u0440\u0435\u043d, \u043e\u043d \u0441\u0442\u0430\u043d\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u0432\u0430\u0448\u0435\u043c \u0430\u0440\u0441\u0435\u043d\u0430\u043b\u0435.<\/p>\n<h3>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/h3>\n<ul>\n<li>\n<p><a href=\"https:\/\/pkg.go.dev\/golang.org\/x\/sync\/singleflight\" rel=\"noopener noreferrer nofollow\"><u>\u041f\u0430\u043a\u0435\u0442 sync\/singleflight<\/u><\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/677444\/\"> https:\/\/habr.com\/ru\/post\/677444\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435 \u043e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430<\/h3>\n<p>\u041d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432 \u0434\u043e\u0432\u0435\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0441 \u0447\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e\u0439 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u043d\u0430 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0445\u043e\u043b\u043e\u0434\u043d\u043e\u043c \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0430 \u043f\u043e\u0440\u043e\u0439 \u0438 \u043f\u0440\u0438 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0430\u043d\u0438\u0438 \u043a\u044d\u0448\u0430. \u0410\u043d\u0430\u043b\u0438\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0438\u0437 \u0442\u044b\u0441\u044f\u0447 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0447\u0430\u0449\u0435 \u0434\u0440\u0443\u0433\u0438\u0445. \u041d\u0430\u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u043e, \u0447\u0442\u043e \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u044d\u0448\u0430 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u0440\u0438\u0448\u0435\u0434\u0448\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443\u0441\u043f\u0435\u0432\u0430\u043b\u043e \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u0442\u044c \u0435\u0449\u0435 \u0441 \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u0432\u0441\u0435 \u043e\u043d\u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0449\u0430\u0434\u043d\u043e \u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0432 \u0442\u043e\u0442 \u0440\u0430\u0437 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0435 \u0441\u0430\u043c\u044b\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c\u0435\u0434\u043d\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u043e\u0445\u043e\u0436\u0438\u043c \u043a\u0435\u0439\u0441\u043e\u043c \u0443\u0436\u0435 \u043d\u0430 Go-\u0441\u0442\u0435\u043a\u0435, \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0432 Google, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043d\u0430\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043d\u0430 \u043f\u0440\u0435\u0436\u043d\u0438\u0435 \u0433\u0440\u0430\u0431\u043b\u0438. \u041a \u0441\u0432\u043e\u0435\u0439 \u0440\u0430\u0434\u043e\u0441\u0442\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u0430\u043a\u0435\u0442 <a href=\"http:\/\/golang.org\/x\/sync\/singleflight\" rel=\"noopener noreferrer nofollow\"><u>singleflight (\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0444 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e),<\/u><\/a> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u0447\u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u0434\u0435\u0441\u044f\u0442\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 &#171;\u0442\u044f\u0436\u0435\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438 \u0442\u043e\u043c\u0443 \u0436\u0435 \u043a\u043b\u044e\u0447\u0443 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c singleflight.Group. <br \/>\u0412 \u0440\u0430\u0431\u043e\u0442\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0436\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0443\u043f\u0430\u0434\u0435\u0442 \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439, \u0442\u043e \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0442\u043e\u0436\u0435 \u0432\u0435\u0440\u043d\u0443\u0442 \u044d\u0442\u0443 \u0436\u0435 \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043f\u0430\u043a\u0435\u0442\u0435 \u043d\u0430\u0448\u0435\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0430\u043d\u0433\u043b\u043e\u044f\u0437\u044b\u0447\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445, \u0447\u0442\u043e \u0438 \u043f\u043e\u0431\u0443\u0434\u0438\u043b\u043e \u043c\u0435\u043d\u044f \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435.<\/p>\n<hr\/>\n<p>\u0421\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043b\u0438 \u0412\u044b \u043a\u043e\u0433\u0434\u0430-\u043d\u0438\u0431\u0443\u0434\u044c \u0441 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0435\u0439, \u0447\u0442\u043e \u0432 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0440\u0435\u0441\u0443\u0440\u0441\u0443, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u0430 \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u0435\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430, \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0435\u0442\u0438, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f) \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430? <br \/>\u0415\u0441\u043b\u0438 \u0412\u0430\u0448 \u043e\u0442\u0432\u0435\u0442 \u00ab\u0434\u0430\u00bb, \u0412\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 <a href=\"https:\/\/pkg.go.dev\/golang.org\/x\/sync\/singleflight\" rel=\"noopener noreferrer nofollow\"><u>sync\/singleflight<\/u><\/a>.<\/p>\n<p>\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u043f\u0430\u043a\u0435\u0442 singleflight \u043d\u0435 \u043d\u043e\u0432. \u041e\u043d \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e (<em>\u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430: \u0441 5 \u043e\u043a\u0442\u044f\u0431\u0440\u044f 2016<\/em>) \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Go \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"https:\/\/pkg.go.dev\/cmd\/go#hdr-Internal_Directories\" rel=\"noopener noreferrer nofollow\"><u>(\u0434\u043b\u044f \u043d\u0435\u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u044b\u0445: \u0438\u043c\u043f\u043e\u0440\u0442 \u043f\u0443\u0442\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u00abinternal\u00bb, \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d, \u0435\u0441\u043b\u0438 \u043a\u043e\u0434 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u0430, \u0438\u043c\u0435\u044e\u0449\u0435\u0433\u043e \u043a\u043e\u0440\u0435\u043d\u044c \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0435 \u00abinternal\u00bb \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0430).<\/u><\/a><\/p>\n<p>\u041a\u0430\u043a \u0438\u0442\u043e\u0433 \u043f\u0430\u043a\u0435\u0442 \u0431\u044b\u043b \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043d\u0438 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439, \u043d\u0438 \u0441\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439, \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 <a href=\"http:\/\/golang.org\/x\" rel=\"noopener noreferrer nofollow\">golang.org\/x<\/a>.<\/p>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0437\u0433\u043e\u0432\u043e\u0440\u043e\u0432, \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 DNS \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u041d\u043e \u043d\u0435 \u0431\u0443\u0434\u0443 \u0437\u0430\u043e\u0441\u0442\u0440\u044f\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u0442.\u043a. <a href=\"https:\/\/github.com\/golang\/go\/blob\/f54c0db859867f415a0702c8ceaa30c1a8976b0b\/src\/net\/lookup.go#L193-L197\" rel=\"noopener noreferrer nofollow\"><u>\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u0442 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c; \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d singleflight<\/u><\/a>. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 HTTP-\u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0432\u043d\u0435\u0448\u043d\u0435\u043c\u0443 API.<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\" )  func main() { \/\/ Create an HTTP handler http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { \/\/ Retrieve GitHub's API status status, err := githubStatus() if err != nil { \/\/ Send an internal error response if we were unable to retrieve the status. \/\/ note: not a great idea to raw errors to a client, but this is just a demonstration. http.Error(w, err.Error(), http.StatusInternalServerError) return }  \/\/ Log the status so we can see what's happening from the server's perspective. log.Printf(\"\/github handler requst: status %q\", status)  \/\/ Write the response to the client. fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { \/\/ Log the start and end of the function so we can see how many times it's called. log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\") \/\/ The defer causes this to be logged after the function's return statement.  \/\/ Atrificially delay this function to emulate a long running task time.Sleep(1 * time.Second)  \/\/ Make a request to the GitHub Status API resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  \/\/ Check that we got a good response. if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  \/\/ Anonymous struct to extract the status from the response r := struct{ Status string }{}  \/\/ Decode the JSON response err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a GitHub \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 API \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0432 \u0432\u0438\u0434\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f. \u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e, \u043d\u043e \u044f \u0431\u0443\u0434\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/github.com\/tsenart\/vegeta\" rel=\"noopener noreferrer nofollow\"><u>vegeta, \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u0447\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f HTTP<\/u><\/a>, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 Go.<\/p>\n<pre><code class=\"bash\"># echo \u201cGET http:\/\/localhost:8080\/github\" | vegeta attack -duration=1s -rate=10 | vegeta report Requests [total, rate] 10, 11.11 Duration [total, attack, wait] 2.011072002s, 899.999ms, 1.111073002s Latencies [mean, 50, 95, 99, max] 1.269280654s, 1.222384648s, 1.445123078s, 1.445123078s, 1.526538858s Bytes In [total, mean] 210, 21.00 Bytes Out [total, mean] 0, 0.00 Success [ratio] 100.00% Status Codes [code:count] 200:10 Error Set:<\/code><\/pre>\n<p>\u0412\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e vegeta 10 GET \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043d\u0430 \u043d\u0430\u0448 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 1 \u0441\u0435\u043a\u0443\u043d\u0434\u044b.<\/p>\n<p>\u0418\u0437 \u043b\u043e\u0433\u043e\u0432 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f githubStatus() \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0430\u0441\u044c 10 \u0440\u0430\u0437, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<pre><code class=\"go\"># go run e1_without_singleflight.go 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:30 Making request to GitHub API 2016\/10\/11 12:36:31 Making request to GitHub API 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:31 Request to GitHub API Complete 2016\/10\/11 12:36:31 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d 2016\/10\/11 12:36:32 Request to GitHub API Complete 2016\/10\/11 12:36:32 \/github handler requst: status \u201cgood\u201d<\/code><\/pre>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442. \u041f\u043e\u0447\u0442\u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0435\u0442 10 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043d\u0430\u0448 \u043a\u043e\u0434 \u0438\u043d\u0438\u0446\u0438\u0438\u0440\u0443\u0435\u0442 10 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u041a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043b\u0438\u0448\u043d\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u044b. \u041c\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u043a\u0430\u043d\u0430\u043b \u0441\u0432\u044f\u0437\u0438 \u0438 \u0441\u043e\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u044b\u0435 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a API GitHub.\u00a0<\/p>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u0430\u043a\u0435\u0442 singleflight \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0430\u0441.\u00a0<\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"log\" \"net\/http\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  func main() { \/\/ We need a Group to use singleflight. var requestGroup singleflight.Group  http.HandleFunc(\"\/github\", func(w http.ResponseWriter, r *http.Request) { \/\/ This time we'll wrap the githubStatus() call with singleflight's Group.Do() \/\/ Do takes a key (more on this later) and a function that returns a interface{} and an error. v, err, shared := requestGroup.Do(\"github\", func() (interface{}, error) { \/\/ githubStatus() returns string, error, which statifies interface{}, error, so we can return the result directly. return githubStatus() }) \/\/ Do returns an interface{}, error, and a bool which indicates whether multiple calls to the function shared the same result.  \/\/ Check the error, as before. if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return }  \/\/ We know that v will be a string, so we'll use a type assertion. status := v.(string)  \/\/ Update the log statement so we can see if the results were shared. log.Printf(\"\/github handler requst: status %q, shared result %t\", status, shared)  fmt.Fprintf(w, \"GitHub Status: %q\", status) })  http.ListenAndServe(\"127.0.0.1:8080\", nil) }  \/\/ githubStatus retrieves GitHub's API status func githubStatus() (string, error) { \/\/ No changes made to this function other than removing the comments for brevity. log.Println(\"Making request to GitHub API\") defer log.Println(\"Request to GitHub API Complete\")  time.Sleep(1 * time.Second)  resp, err := http.Get(\"https:\/\/status.github.com\/api\/status.json\") if err != nil { return \"\", err } defer resp.Body.Close()  if resp.StatusCode != 200 { return \"\", fmt.Errorf(\"github response: %s\", resp.Status) }  r := struct{ Status string }{}  err = json.NewDecoder(resp.Body).Decode(&amp;r)  return r.Status, err }<\/code><\/pre>\n<p>\u041a\u0430\u043a\u043e\u0439 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 vegeta \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0435\u043f\u0435\u0440\u044c?<\/p>\n<pre><code class=\"bash\"># go run e2_with_singleflight.go 2016\/10\/11 13:02:49 Making request to GitHub API 2016\/10\/11 13:02:51 Request to GitHub API Complete 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst: status \u201cgood\u201d, shared result true 2016\/10\/11 13:02:51 \/github handler requst:<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-335823","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335823","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=335823"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/335823\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=335823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=335823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=335823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}