{"id":337052,"date":"2022-08-15T15:00:18","date_gmt":"2022-08-15T15:00:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=337052"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=337052","title":{"rendered":"<span>\u041f\u0440\u043e\u0441\u0442\u044b\u0435 highload \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u043d\u0430 Go<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u0433\u0430\u0434\u0436\u0430\u043d\u044f\u043d \u0414\u0430\u0432\u0438\u0434, \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0430\u043c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0430 \u043c\u043e\u0435\u043c \u043e\u043f\u044b\u0442\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u0438 \u0434\u0435\u0440\u0436\u0430\u0442\u044c <strong>highload<\/strong> <strong>\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443<\/strong> \u043d\u0435 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u044f \u043a \u0445\u0430\u0440\u0434\u043a\u043e\u0440\u0443. \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0431\u0443\u0434\u0443\u0442 \u043d\u0430 <strong>Go<\/strong>. \u042d\u0442\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u043d\u043e \u043a\u0430\u043a \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043e\u043d\u0438 \u043d\u0435\u0434\u043e\u043e\u0446\u0435\u043d\u0435\u043d\u044b \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u044d\u0442\u0438\u043c\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c\u0438 \u043f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0433\u0430\u044e\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u0445, \u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438 \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u044b. \u042d\u0442\u0438\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0432 90% \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u043b\u043e \u0437\u0430 \u0433\u043b\u0430\u0437\u0430, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043a\u0440\u0430\u0442\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0444\u043e\u043c\u0430\u043d\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0440\u043e\u043a\u0438. \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c \u043e\u043f\u044b\u0442\u043e\u043c \u043a \u043a\u0430\u043a\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0432\u044b \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u0442\u0435 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0437\u044f\u0442\u044c \u0441\u0435\u0431\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043d\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0443<\/p>\n<h2>Refresh-ahead caching<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0432\u0435\u0436\u0435\u0441\u0442\u0438, \u0442\u043e \u043a\u0435\u0448\u0438\u0440\u0443\u0439\u0442\u0435 \u0438\u0445 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043a\u0430\u043a \u0435\u0441\u0442\u044c. \u0410 \u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0439\u0442\u0435 \u0432 \u0444\u043e\u043d\u0435<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0444\u0438\u043b\u044c\u043c\u043e\u0432, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0435 \u0432\u044b \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0434\u043a\u043e, \u0434\u0430 \u0438 \u0435\u0441\u043b\u0438 \u043e\u0442\u0434\u0430\u0434\u0438\u0442\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0439, \u0442\u043e \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442, \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043f\u043e\u0441\u0442\u0440\u0430\u0434\u0430\u0435\u0442. \u0422\u0430\u043a \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0432\u0437\u044f\u0442\u044c \u0438 \u043d\u0435 \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u044f\u043c\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438?<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0444\u0438\u043b\u044c\u043c\u043e\u0432 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0435\u0441\u0442\u044c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u0444\u043e\u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u043e\u0440\u043a\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437 \u0432 <strong>N <\/strong>\u0441\u0435\u043a\u0443\u043d\u0434 \u043e\u0431\u043d\u043e\u0432\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/876\/544\/3f7\/8765443f76e12cbed1e8ef6c573b223b.png\" alt=\"refresh-ahead caching\" title=\"refresh-ahead caching\" width=\"2092\" height=\"557\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/876\/544\/3f7\/8765443f76e12cbed1e8ef6c573b223b.png\"\/><figcaption>refresh-ahead caching<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: \u043d\u0438\u0436\u0435 \u043a\u043e\u0434, \u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 <strong>github<\/strong>, <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/refresh-ahead\/main.go\" rel=\"noopener noreferrer nofollow\">welcome<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"encoding\/json\" \"net\/http\" \"sync\" \"time\" )  type Movie struct { Title string `json:\"Title\"` }  type CachedPopularItems struct { lock   sync.RWMutex Movies []Movie }  func main() { ctx := context.Background()  \/\/ initializing cache and fill cache := CachedPopularItems{} cache.Movies = getPopularMoviesFromDB() go func() { timer := time.NewTicker(1 * time.Second) defer timer.Stop()  \/\/ initializing background job for { select { \/\/ refreshing cache case &lt;-timer.C: movies := getPopularMoviesFromDB()  \/\/ updating cache struct cache.lock.Lock() cache.Movies = movies cache.lock.Unlock()  \/\/ app is terminating case &lt;-ctx.Done(): break } } }()  http.HandleFunc(\"\/getPopularMovies\", func(writer http.ResponseWriter, request *http.Request) { cache.lock.RLock() movies := cache.Movies cache.lock.RUnlock()  bytes, _ := json.Marshal(movies)  writer.Header().Add(\"Content-Type\", \"application\/json\") writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }  \/\/ Getting from DB func getPopularMoviesFromDB() []Movie { \/\/ simulation request to database with latency time.Sleep(5 * time.Second)  return []Movie{{Title: \"Avatar\"}, {Title: \"I Am Legend\"}, {Title: \"The Wolf of Wall Street\"}} }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438, \u043f\u0440\u0438\u0448\u0435\u043b \u0437\u0430\u043f\u0440\u043e\u0441, \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0434\u0430\u043b\u0438 \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u044f\u0436\u0435\u043b\u043e\u0432\u0435\u0441\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u0421\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043f\u043e\u0445\u043e\u0434 \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<\/li>\n<li>\n<p>\u0423\u0437\u043a\u043e\u0435 \u0433\u043e\u0440\u043b\u044b\u0448\u043a\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u044d\u0442\u043e \u043a\u043e\u043b-\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u043c\u0435\u044e\u0442 \u0432\u0430\u0440\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c, \u0442\u043e \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0442\u0430 \u0435\u0449\u0435 \u0437\u0430\u0434\u0430\u0447\u0430<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u042d\u0442\u043e\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0435 <a href=\"https:\/\/github.com\/donnemartin\/system-design-primer#refresh-ahead\" rel=\"noopener noreferrer nofollow\">system-design-primer<\/a><\/p>\n<\/blockquote>\n<h2>Do once, give it to everyone<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432 \u0441\u0435\u0440\u0432\u0438\u0441 \u0437\u0430 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0437\u0430\u0447\u0435\u043c \u0435\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0432\u0430\u0436\u0434\u044b, \u0430 \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0442\u044b\u0441\u044f\u0447\u0438 \u0440\u0430\u0437?<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043a\u043d\u0438\u0433\u0430\u043c\u0438, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043a\u043d\u0438\u0433\u0438 \u0441\u043c\u043e\u0442\u0440\u044f\u0442 \u0447\u0430\u0449\u0435, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0440\u0435\u0436\u0435, \u0438 \u0431\u044b\u0432\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043d\u0438\u0433 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443 \u043f\u0438\u043a\u043e\u0432 \u043d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f, \u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0445\u0435\u0448-\u043a\u043e\u0434 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0435 \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0432\u0448\u0438\u043c. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u0432\u0438\u0434\u043d\u043e \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u0438 \u0442\u0430 \u0436\u0435 \u043a\u043d\u0438\u0433\u0430 \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u0430 \u0434\u0432\u0430\u0436\u0434\u044b, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u0442\u0438 \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043e\u0442\u0434\u0430\u0442\u044c \u0435\u0435 \u043e\u0431\u043e\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0432\u0448\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ad9\/556\/06a\/ad955606a679e4f6abf82b0d57036af9.png\" alt=\"do once, give it to everyone\" title=\"do once, give it to everyone\" width=\"2411\" height=\"822\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad9\/556\/06a\/ad955606a679e4f6abf82b0d57036af9.png\"\/><figcaption>do once, give it to everyone<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/singleflight\/main.go\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Go \u0432 github<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"math\/rand\" \"net\/http\" \"strconv\" \"strings\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  type Book struct { ID    int Title string `json:\"Title\"` }  func main() { \/\/ Struct for syncing work s := singleflight.Group{}  http.HandleFunc(\"\/getBook\/\", func(writer http.ResponseWriter, request *http.Request) { bookID, _ := strconv.Atoi(strings.TrimLeft(request.RequestURI, \"\/getBook\/\")) workHash := fmt.Sprintf(\"book:%d\", bookID)  \/\/ Doing work with same hash once result, _, _ := s.Do(workHash, func() (interface{}, error) { return getBookFromDB(bookID), nil })  book := result.(Book) bytes, _ := json.Marshal(book)  writer.Header().Add(\"Content-Type\", \"application\/json\") writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }  \/\/ Getting from DB func getBookFromDB(id int) Book { \/\/ simulation request to database with latency time.Sleep(1 * time.Second)  return Book{ID: id, Title: randSeq(rand.Intn(30))} }  var letters = []rune(\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\")  \/\/ Random string generator func randSeq(n int) string { b := make([]rune, n) for i := range b { b[i] = letters[rand.Intn(len(letters))] } return string(b) }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 CPU\/\u0421\u0435\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0438\u043d\u0430\u0447\u0435 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0444\u0438\u0442\u0430 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u043f\u0440\u0438\u043d\u0435\u0441\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<h2>Worker pool<\/h2>\n<p>\u0412\u0430\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0430\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u0441\u0430\u043c\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0437\u0430\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 1<\/strong>:<strong> <\/strong>\u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f &#8212; \u0434\u043e\u0440\u043e\u0433\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 2<\/strong>:<strong> <\/strong>\u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 (\u0431\u0430\u0442\u0447 \u0437\u0430\u043f\u0440\u043e\u0441) \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0441\u043e\u0431\u0440\u0430\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043e\u0442\u0447\u0435\u0442<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u043d\u0430 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c <strong>N <\/strong>\u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443, \u0438 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432\u043e\u0440\u043a\u0435\u0440\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3bc\/4d8\/353\/3bc4d8353c08b2c4e532d5151f26b5f0.png\" alt=\"worker pool\" title=\"worker pool\" width=\"1680\" height=\"664\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3bc\/4d8\/353\/3bc4d8353c08b2c4e532d5151f26b5f0.png\"\/><figcaption>worker pool<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/worker-pool\/main.go\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Go \u0432 github<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"encoding\/json\" \"math\/rand\" \"net\/http\" \"time\" )  type WorkerPool struct { jobs chan WorkJob }  func (w *WorkerPool) StartWorker() { go func() { for { work := &lt;-w.jobs  \/\/ simulating work time.Sleep(1 * time.Second)  status := false if work.ID%10 > 5 { status = true }  work.Result &lt;- WorkJobResult{Status: status} } }() }  \/\/ Adding work job to queue func (w *WorkerPool) AddJob(ctx context.Context, id int) &lt;-chan WorkJobResult { resultChan := make(chan WorkJobResult, 1)  select { \/\/ trying to add wor job case w.jobs &lt;- WorkJob{ID: id, Result: resultChan}:  \/\/ in case if request is aborted case &lt;-ctx.Done(): return nil }  \/\/ return chan where consumer can read result return resultChan }  type WorkJob struct { ID     int Result chan WorkJobResult }  type WorkJobResult struct { Status bool }  func main() { \/\/ worker pool with three workers wp := WorkerPool{ jobs: make(chan WorkJob, 3), } wp.StartWorker() wp.StartWorker() wp.StartWorker()  http.HandleFunc(\"\/handle\", func(writer http.ResponseWriter, request *http.Request) { resultsChan := make([]&lt;-chan WorkJobResult, 0) for i := 0; i &lt; 10; i++ { resultChan := wp.AddJob(context.Background(), rand.Intn(100)) resultsChan = append(resultsChan, resultChan) }  status := false for _, res := range resultsChan { resStatus := &lt;-res status = status &amp;&amp; resStatus.Status }  bytes, _ := json.Marshal(status) writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 <strong>worker pool <\/strong>\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u0435\u0436\u0434\u0443 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0441\u043b\u043e\u043c\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u0418\u0442\u043e\u0433<\/h2>\n<p>\u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u043b\u044e\u0431\u0438\u043c\u044b\u043c\u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430\u043c\u0438, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043d\u043e\u0432\u043e\u0435. \u0411\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d \u043b\u044e\u0431\u044b\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f\u043c. \u0421\u043f\u0430\u0441\u0438\u0431\u043e!<\/p>\n<h2><\/h2>\n<\/p>\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\/682618\/\"> https:\/\/habr.com\/ru\/post\/682618\/<\/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<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u0433\u0430\u0434\u0436\u0430\u043d\u044f\u043d \u0414\u0430\u0432\u0438\u0434, \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0430\u043c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043d\u0430 \u043c\u043e\u0435\u043c \u043e\u043f\u044b\u0442\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u0438 \u0434\u0435\u0440\u0436\u0430\u0442\u044c <strong>highload<\/strong> <strong>\u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443<\/strong> \u043d\u0435 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u044f \u043a \u0445\u0430\u0440\u0434\u043a\u043e\u0440\u0443. \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0431\u0443\u0434\u0443\u0442 \u043d\u0430 <strong>Go<\/strong>. \u042d\u0442\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u043d\u043e \u043a\u0430\u043a \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043e\u043d\u0438 \u043d\u0435\u0434\u043e\u043e\u0446\u0435\u043d\u0435\u043d\u044b \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u044d\u0442\u0438\u043c\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c\u0438 \u043f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0433\u0430\u044e\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0432\u0438\u0434\u0438\u0442\u0435 \u0438\u0445, \u0442\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0438 \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432\u044b \u0431\u0443\u0434\u0435\u0442\u0435 \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u044b. \u042d\u0442\u0438\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0432 90% \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u043b\u043e \u0437\u0430 \u0433\u043b\u0430\u0437\u0430, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043a\u0440\u0430\u0442\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0444\u043e\u043c\u0430\u043d\u0441 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0441\u0440\u043e\u043a\u0438. \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0434\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c \u043e\u043f\u044b\u0442\u043e\u043c \u043a \u043a\u0430\u043a\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0430\u043c \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0432\u044b \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u0442\u0435 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u0432\u0437\u044f\u0442\u044c \u0441\u0435\u0431\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043d\u0430 \u0437\u0430\u043c\u0435\u0442\u043a\u0443<\/p>\n<h2>Refresh-ahead caching<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0432\u0435\u0436\u0435\u0441\u0442\u0438, \u0442\u043e \u043a\u0435\u0448\u0438\u0440\u0443\u0439\u0442\u0435 \u0438\u0445 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043a\u0430\u043a \u0435\u0441\u0442\u044c. \u0410 \u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0439\u0442\u0435 \u0432 \u0444\u043e\u043d\u0435<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0444\u0438\u043b\u044c\u043c\u043e\u0432, \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0435 \u0432\u044b \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0434\u043a\u043e, \u0434\u0430 \u0438 \u0435\u0441\u043b\u0438 \u043e\u0442\u0434\u0430\u0434\u0438\u0442\u0435 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0439, \u0442\u043e \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u0442, \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u043f\u043e\u0441\u0442\u0440\u0430\u0434\u0430\u0435\u0442. \u0422\u0430\u043a \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0432\u0437\u044f\u0442\u044c \u0438 \u043d\u0435 \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u044f\u043c\u043e \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438?<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u0437\u0430\u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0444\u0438\u043b\u044c\u043c\u043e\u0432 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0435\u0441\u0442\u044c, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u0444\u043e\u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u043e\u0440\u043a\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437 \u0432 <strong>N <\/strong>\u0441\u0435\u043a\u0443\u043d\u0434 \u043e\u0431\u043d\u043e\u0432\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438<\/p>\n<figure class=\"full-width\"><figcaption>refresh-ahead caching<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: \u043d\u0438\u0436\u0435 \u043a\u043e\u0434, \u043d\u043e \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 <strong>github<\/strong>, <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/refresh-ahead\/main.go\" rel=\"noopener noreferrer nofollow\">welcome<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"encoding\/json\" \"net\/http\" \"sync\" \"time\" )  type Movie struct { Title string `json:\"Title\"` }  type CachedPopularItems struct { lock   sync.RWMutex Movies []Movie }  func main() { ctx := context.Background()  \/\/ initializing cache and fill cache := CachedPopularItems{} cache.Movies = getPopularMoviesFromDB() go func() { timer := time.NewTicker(1 * time.Second) defer timer.Stop()  \/\/ initializing background job for { select { \/\/ refreshing cache case &lt;-timer.C: movies := getPopularMoviesFromDB()  \/\/ updating cache struct cache.lock.Lock() cache.Movies = movies cache.lock.Unlock()  \/\/ app is terminating case &lt;-ctx.Done(): break } } }()  http.HandleFunc(\"\/getPopularMovies\", func(writer http.ResponseWriter, request *http.Request) { cache.lock.RLock() movies := cache.Movies cache.lock.RUnlock()  bytes, _ := json.Marshal(movies)  writer.Header().Add(\"Content-Type\", \"application\/json\") writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }  \/\/ Getting from DB func getPopularMoviesFromDB() []Movie { \/\/ simulation request to database with latency time.Sleep(5 * time.Second)  return []Movie{{Title: \"Avatar\"}, {Title: \"I Am Legend\"}, {Title: \"The Wolf of Wall Street\"}} }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041d\u0438\u043a\u0430\u043a\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438, \u043f\u0440\u0438\u0448\u0435\u043b \u0437\u0430\u043f\u0440\u043e\u0441, \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0434\u0430\u043b\u0438 \u043e\u0442\u0432\u0435\u0442<\/p>\n<\/li>\n<li>\n<p>\u0421\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435, \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441 \u0442\u044f\u0436\u0435\u043b\u043e\u0432\u0435\u0441\u043d\u044b\u0439<\/p>\n<\/li>\n<li>\n<p>\u0421\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043f\u043e\u0445\u043e\u0434 \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435<\/p>\n<\/li>\n<li>\n<p>\u0423\u0437\u043a\u043e\u0435 \u0433\u043e\u0440\u043b\u044b\u0448\u043a\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u044d\u0442\u043e \u043a\u043e\u043b-\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439 \u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043a\u0430\u043d\u0430\u043b<\/p>\n<\/li>\n<li>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0441\u043b\u0438 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e, \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438<\/p>\n<\/li>\n<li>\n<p>\u0412 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u043c\u0435\u044e\u0442 \u0432\u0430\u0440\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c, \u0442\u043e \u0432\u043d\u0435\u0434\u0440\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0442\u0430 \u0435\u0449\u0435 \u0437\u0430\u0434\u0430\u0447\u0430<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u042d\u0442\u043e\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0435 <a href=\"https:\/\/github.com\/donnemartin\/system-design-primer#refresh-ahead\" rel=\"noopener noreferrer nofollow\">system-design-primer<\/a><\/p>\n<\/blockquote>\n<h2>Do once, give it to everyone<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432 \u0441\u0435\u0440\u0432\u0438\u0441 \u0437\u0430 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439, \u0437\u0430\u0447\u0435\u043c \u0435\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0432\u0430\u0436\u0434\u044b, \u0430 \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0442\u044b\u0441\u044f\u0447\u0438 \u0440\u0430\u0437?<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440<\/strong>: \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043a\u043d\u0438\u0433\u0430\u043c\u0438, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043a\u043d\u0438\u0433\u0438 \u0441\u043c\u043e\u0442\u0440\u044f\u0442 \u0447\u0430\u0449\u0435, \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0440\u0435\u0436\u0435, \u0438 \u0431\u044b\u0432\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0435, \u0447\u0442\u043e \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u043a\u043d\u0438\u0433 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443 \u043f\u0438\u043a\u043e\u0432 \u043d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f, \u0430 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0445\u0435\u0448-\u043a\u043e\u0434 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0435 \u0432\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0432\u0448\u0438\u043c. \u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u0432\u0438\u0434\u043d\u043e \u0447\u0442\u043e \u043e\u0434\u043d\u043e \u0438 \u0442\u0430 \u0436\u0435 \u043a\u043d\u0438\u0433\u0430 \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u0430 \u0434\u0432\u0430\u0436\u0434\u044b, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u0442\u0438 \u0432 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043e\u0442\u0434\u0430\u0442\u044c \u0435\u0435 \u043e\u0431\u043e\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0432\u0448\u0438\u043c \u043a\u043b\u0438\u0435\u043d\u0442\u0430\u043c<\/p>\n<figure class=\"full-width\"><figcaption>do once, give it to everyone<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/singleflight\/main.go\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Go \u0432 github<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"encoding\/json\" \"fmt\" \"math\/rand\" \"net\/http\" \"strconv\" \"strings\" \"time\"  \"golang.org\/x\/sync\/singleflight\" )  type Book struct { ID    int Title string `json:\"Title\"` }  func main() { \/\/ Struct for syncing work s := singleflight.Group{}  http.HandleFunc(\"\/getBook\/\", func(writer http.ResponseWriter, request *http.Request) { bookID, _ := strconv.Atoi(strings.TrimLeft(request.RequestURI, \"\/getBook\/\")) workHash := fmt.Sprintf(\"book:%d\", bookID)  \/\/ Doing work with same hash once result, _, _ := s.Do(workHash, func() (interface{}, error) { return getBookFromDB(bookID), nil })  book := result.(Book) bytes, _ := json.Marshal(book)  writer.Header().Add(\"Content-Type\", \"application\/json\") writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }  \/\/ Getting from DB func getBookFromDB(id int) Book { \/\/ simulation request to database with latency time.Sleep(1 * time.Second)  return Book{ID: id, Title: randSeq(rand.Intn(30))} }  var letters = []rune(\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\")  \/\/ Random string generator func randSeq(n int) string { b := make([]rune, n) for i := range b { b[i] = letters[rand.Intn(len(letters))] } return string(b) }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 CPU\/\u0421\u0435\u0442\u0438<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0438\u043d\u0430\u0447\u0435 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0444\u0438\u0442\u0430 \u044d\u0442\u043e\u0442 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435 \u043f\u0440\u0438\u043d\u0435\u0441\u0435\u0442<\/p>\n<\/li>\n<\/ul>\n<h2>Worker pool<\/h2>\n<p>\u0412\u0430\u043c \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0430\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u0441\u0430\u043c\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0437\u0430\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 1<\/strong>:<strong> <\/strong>\u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f &#8212; \u0434\u043e\u0440\u043e\u0433\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0437\u0430\u0440\u0430\u043d\u0435\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439<\/p>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 2<\/strong>:<strong> <\/strong>\u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 (\u0431\u0430\u0442\u0447 \u0437\u0430\u043f\u0440\u043e\u0441) \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e, \u0441\u043e\u0431\u0440\u0430\u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043e\u0442\u0447\u0435\u0442<\/p>\n<p><strong>\u041a\u043e\u043d\u0446\u0435\u043f\u0442<\/strong>: \u043d\u0430 \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c <strong>N <\/strong>\u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443, \u0438 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432\u043e\u0440\u043a\u0435\u0440\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438<\/p>\n<figure class=\"full-width\"><figcaption>worker pool<\/figcaption><\/figure>\n<p><strong>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong>: <a href=\"https:\/\/github.com\/agadzhanyan\/highload-patterns\/blob\/main\/worker-pool\/main.go\" rel=\"noopener noreferrer nofollow\">\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Go \u0432 github<\/a><\/p>\n<pre><code class=\"go\">package main  import ( \"context\" \"encoding\/json\" \"math\/rand\" \"net\/http\" \"time\" )  type WorkerPool struct { jobs chan WorkJob }  func (w *WorkerPool) StartWorker() { go func() { for { work := &lt;-w.jobs  \/\/ simulating work time.Sleep(1 * time.Second)  status := false if work.ID%10 > 5 { status = true }  work.Result &lt;- WorkJobResult{Status: status} } }() }  \/\/ Adding work job to queue func (w *WorkerPool) AddJob(ctx context.Context, id int) &lt;-chan WorkJobResult { resultChan := make(chan WorkJobResult, 1)  select { \/\/ trying to add wor job case w.jobs &lt;- WorkJob{ID: id, Result: resultChan}:  \/\/ in case if request is aborted case &lt;-ctx.Done(): return nil }  \/\/ return chan where consumer can read result return resultChan }  type WorkJob struct { ID     int Result chan WorkJobResult }  type WorkJobResult struct { Status bool }  func main() { \/\/ worker pool with three workers wp := WorkerPool{ jobs: make(chan WorkJob, 3), } wp.StartWorker() wp.StartWorker() wp.StartWorker()  http.HandleFunc(\"\/handle\", func(writer http.ResponseWriter, request *http.Request) { resultsChan := make([]&lt;-chan WorkJobResult, 0) for i := 0; i &lt; 10; i++ { resultChan := wp.AddJob(context.Background(), rand.Intn(100)) resultsChan = append(resultsChan, resultChan) }  status := false for _, res := range resultsChan { resStatus := &lt;-res status = status &amp;&amp; resStatus.Status }  bytes, _ := json.Marshal(status) writer.Write(bytes) })  _ = http.ListenAndServe(\":8890\", nil) }<\/code><\/pre>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447<\/p>\n<\/li>\n<li>\n<p>\u042d\u043a\u043e\u043d\u043e\u043c\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 <strong>worker pool <\/strong>\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043c\u0435\u0436\u0434\u0443 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u0448\u043d\u0435\u0433\u043e \u043c\u0443\u0441\u043e\u0440\u0430<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443 \u0438 \u0441\u043b\u043e\u043c\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<h2>\u0418\u0442\u043e\u0433<\/h2>\n<p>\u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u043b\u044e\u0431\u0438\u043c\u044b\u043c\u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430\u043c\u0438, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434 \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043d\u043e\u0432\u043e\u0435. \u0411\u0443\u0434\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u0442\u0435\u043b\u0435\u043d \u043b\u044e\u0431\u044b\u043c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f\u043c. \u0421\u043f\u0430\u0441\u0438\u0431\u043e!<\/p>\n<h2><\/h2>\n<\/p>\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\/682618\/\"> https:\/\/habr.com\/ru\/post\/682618\/<\/a><br \/><\/br><\/br><\/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-337052","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337052","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=337052"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/337052\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}