{"id":473854,"date":"2025-09-03T21:00:42","date_gmt":"2025-09-03T21:00:42","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=473854"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=473854","title":{"rendered":"<span>\u0412\u0441\u0451 \u043f\u0440\u043e BlockingCollection \u0438 \u0444\u0438\u0447\u0438 \u0441 \u043d\u0438\u043c\u0438<\/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!<\/p>\n<p>\u0412 .NET \u0436\u0438\u0432\u0451\u0442 \u0441\u0442\u0430\u0440\u044b\u0439, \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u2014 <code>BlockingCollection&lt;T&gt;<\/code>. \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c producer\u2013consumer, \u0434\u0430\u0451\u0442 \u0441\u0442\u0440\u043e\u0433\u0443\u044e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u0442\u0435 \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438. \u0414\u0430, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>Channel&lt;T&gt;<\/code>, \u0435\u0441\u0442\u044c TPL Dataflow, \u0435\u0441\u0442\u044c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u043d\u0430 <code>SemaphoreSlim<\/code>. \u041d\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438 \u0436\u0435\u043b\u0435\u0437\u043d\u044b\u0439 backpressure \u0431\u0435\u0437 <code>BlockingCollection<\/code> \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0445\u043e\u0440\u043e\u0448\u043e. <\/p>\n<h4>\u0427\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u0430\u0451\u0442 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f<\/h4>\n<ol>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u0415\u0451 \u0437\u0430\u0434\u0430\u044e\u0442 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0431\u044e\u0434\u0436\u0435\u0442 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0442\u043e\u0440\u043c\u0430\u0436\u0438\u0432\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0411\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. <code>Add<\/code> \u0438 <code>Take<\/code> \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0442\u043e\u043a \u0434\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u0441\u0442\u0430 \u0438\u043b\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u0442\u044c \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438 <code>TryAdd<\/code> \u0438 <code>TryTake<\/code> \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0430\u043c\u0438 \u0438 <code>CancellationToken<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 <code>CompleteAdding<\/code>, \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u043e <code>IsCompleted<\/code> \u0438 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0431\u0435\u0437 \u0432\u043e\u0440\u043d\u0438\u043d\u0433\u043e\u0432 \u0438 \u043f\u043e\u0434\u0432\u0438\u0441\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441 \u0438\u0437\u044a\u044f\u0442\u0438\u0435\u043c. <code>GetConsumingEnumerable<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f \u0447\u0435\u0440\u0435\u0437 <code>foreach<\/code>, \u0431\u0435\u0437 \u0440\u0443\u0447\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439. \u0415\u0441\u0442\u044c <code>TakeFromAny<\/code> \u0438 <code>AddToAny<\/code>, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043a\u0430\u043a \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u043f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>ConcurrentQueue&lt;T&gt;<\/code> (FIFO), \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0438 <code>ConcurrentStack&lt;T&gt;<\/code> (LIFO), \u0438 <code>ConcurrentBag&lt;T&gt;<\/code> \u2014 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438.<\/p>\n<h4>\u041a\u0430\u0440\u043a\u0430\u0441 \u043f\u0443\u043b\u0430 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0431\u0435\u0437 \u043f\u043e\u0434\u0432\u0438\u0441\u0430\u043d\u0438\u0439<\/h4>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: \u0434\u0430\u0442\u044c backpressure, \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443, \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u0432\u0438\u0441\u0448\u0438\u0435 <code>Take<\/code>, \u043d\u0435 \u0441\u0436\u0435\u0447\u044c CPU \u0441\u043f\u0438\u043d\u043e\u043c.<\/p>\n<pre><code class=\"cs\">using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks;  public sealed class WorkerPool&lt;T&gt; : IDisposable {     private readonly BlockingCollection&lt;T&gt; _queue;     private readonly Task[] _workers;     private readonly CancellationTokenSource _cts = new();      public WorkerPool(         int boundedCapacity,         int degreeOfParallelism,         Action&lt;T, CancellationToken&gt; handler)     {         if (boundedCapacity &lt;= 0) throw new ArgumentOutOfRangeException(nameof(boundedCapacity));         if (degreeOfParallelism &lt;= 0) throw new ArgumentOutOfRangeException(nameof(degreeOfParallelism));         if (handler is null) throw new ArgumentNullException(nameof(handler));          _queue = new BlockingCollection&lt;T&gt;(new ConcurrentQueue&lt;T&gt;(), boundedCapacity);          _workers = new Task[degreeOfParallelism];         for (int i = 0; i &lt; degreeOfParallelism; i++)         {             _workers[i] = Task.Factory.StartNew(() =&gt;             {                 var ct = _cts.Token;                 while (!_queue.IsCompleted)                 {                     try                     {                         var item = _queue.Take(ct);                         handler(item, ct);                     }                     catch (OperationCanceledException)                     {                         break;                     }                     catch (InvalidOperationException)                     {                         break; \/\/ \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430 \u0438 \u043f\u0443\u0441\u0442\u0430                     }                 }             }, TaskCreationOptions.LongRunning);         }     }      public bool TryPost(T item, TimeSpan timeout, CancellationToken ct = default)         =&gt; _queue.TryAdd(item, timeout, ct);      public void Complete() =&gt; _queue.CompleteAdding();      public void Dispose()     {         try         {             _cts.Cancel();             _queue.CompleteAdding();             Task.WaitAll(_workers, TimeSpan.FromSeconds(5));         }         finally         {             _queue.Dispose();             _cts.Dispose();         }     } }<\/code><\/pre>\n<p><code>TaskCreationOptions.LongRunning<\/code> \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u0434 \u0432\u043e\u0440\u043a\u0435\u0440, \u044d\u0442\u043e \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0446\u0438\u044e \u0437\u0430 \u043f\u0443\u043b, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0435 \u043c\u0438\u043a\u0440\u043e\u0441\u043a\u043e\u043f\u0438\u0447\u0435\u0441\u043a\u0430\u044f. \u041d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0441\u0442\u0430\u0432\u0438\u0442\u044c LongRunning \u0432\u0435\u0437\u0434\u0435 \u043f\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0435. \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f, \u043e\u0431\u044b\u0447\u043d\u044b\u0435 <code>Task.Run<\/code> \u0431\u0435\u0437 LongRunning \u0434\u0430\u044e\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u0443\u044e \u0443\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043b\u0438 <code>CompleteAdding<\/code> \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430, <code>Take<\/code> \u043a\u0438\u0434\u0430\u0435\u0442 <code>InvalidOperationException<\/code>. <\/p>\n<h4>\u0422\u0430\u0439\u043c\u0430\u0443\u0442\u044b, \u043e\u0442\u043c\u0435\u043d\u0430 \u0438 \u043e\u0442\u043a\u0430\u0437 \u043e\u0442 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043f\u0438\u043d\u043e\u0432<\/h4>\n<p>\u0421\u043f\u0438\u043d \u0441 <code>TryTake<\/code> \u0432 \u0432\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u0433\u0434\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 <code>Thread.Sleep(1)<\/code> \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f. \u042d\u0442\u043e \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 CPU \u0431\u0435\u0437 \u043f\u043e\u043b\u044c\u0437\u044b \u0438 \u043f\u043e\u0440\u0442\u0438\u0442 \u044d\u043d\u0435\u0440\u0433\u043e\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435. \u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0430\u043c\u0438 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u043e\u0442\u043c\u0435\u043d\u043e\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u0435\u0435:<\/p>\n<pre><code class=\"cs\">public static void DrainWithTimeout&lt;T&gt;(     BlockingCollection&lt;T&gt; bc,     Action&lt;T&gt; handle,     TimeSpan wait,     CancellationToken ct) {     while (!bc.IsCompleted)     {         try         {             if (bc.TryTake(out var item, (int)wait.TotalMilliseconds, ct))             {                 handle(item);             }             \/\/ \u0438\u043d\u0430\u0447\u0435 \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0434\u0451\u043c \u0432 \u043d\u043e\u0432\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e         }         catch (OperationCanceledException)         {             break;         }         catch (InvalidOperationException)         {             break;         }     } }<\/code><\/pre>\n<p>\u0422\u0430\u0439\u043c\u0430\u0443\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 SLA \u043d\u0430 \u043b\u0430\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u0432\u0430\u0436\u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u043d\u043e \u0432\u044b\u0448\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f CPU, \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0431\u043e\u043b\u044c\u0448\u0435. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043d\u0435\u0442.<\/p>\n<h4>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u0445<\/h4>\n<p>\u0422\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>CompleteAdding<\/code> \u0438\u0437 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0443\u0442\u044c \u043a \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c. \u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u2014 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0443\u0440\u043e\u0432\u043d\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u043e\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u0442. \u041f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e <code>IsCompleted<\/code>.<\/p>\n<p>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>CompleteAdding<\/code> \u0432 <code>finally<\/code> \u0440\u044f\u0434\u043e\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u043c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0434 \u043d\u0435 \u0434\u043e\u0431\u0435\u0436\u0430\u043b \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0446\u0438\u043a\u043b\u0430 \u0438\u0437-\u0437\u0430 \u043e\u0442\u043c\u0435\u043d\u044b \u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. <\/p>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442. <code>Dispose<\/code> \u043d\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 <code>Take<\/code>. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a: <code>CompleteAdding<\/code>, \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e <code>Dispose<\/code>. \u041f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0443\u0431\u0438\u0442\u044c \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0434\u0438\u0441\u043f\u043e\u0437\u043e\u043c \u0442\u0430\u043a \u0441\u0435\u0431\u0435 \u0437\u0430\u0442\u0435\u044f.<\/p>\n<h4>\u0412\u044b\u0431\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/h4>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441\u0442\u043e\u0438\u0442 <code>ConcurrentQueue&lt;T&gt;<\/code> \u2014 \u044d\u0442\u043e FIFO \u0438 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u0438\u0441\u0442\u0435\u043c \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u041d\u0443\u0436\u0435\u043d LIFO, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043f\u0440\u0438\u0448\u0435\u0434\u0448\u0438\u0439 \u0432\u0430\u0436\u043d\u0435\u0435 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>ConcurrentStack&lt;T&gt;<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441 \u0431\u0435\u0437\u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435\u043c \u043a \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u2014 <code>ConcurrentBag&lt;T&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><code>PriorityQueue&lt;TElement,TPriority&gt;<\/code> \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 <code>IProducerConsumerCollection&lt;T&gt;<\/code>, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0437\u0430\u0446\u0438\u044f, \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u0432\u043e\u044e \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0443, \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a <code>Channel&lt;T&gt;<\/code> \u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438.<\/p>\n<h4>GetConsumingEnumerable: \u0447\u0438\u0441\u0442\u044b\u0439 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c \u0431\u0435\u0437 \u0440\u0443\u0447\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432<\/h4>\n<p>\u041d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u0438\u043c \u0438\u0437-\u0437\u0430 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438. \u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043a\u0440\u044b\u0442\u0430 \u0438 \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430.<\/p>\n<pre><code class=\"cs\">public static Task StartConsumer&lt;T&gt;(     BlockingCollection&lt;T&gt; bc,     Action&lt;T&gt; handler,     CancellationToken ct) {     return Task.Factory.StartNew(() =&gt;     {         foreach (var item in bc.GetConsumingEnumerable(ct))             handler(item);     }, TaskCreationOptions.LongRunning); }<\/code><\/pre>\n<p>\u0423 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043e\u0434\u0438\u043d \u043d\u044e\u0430\u043d\u0441: \u043d\u0435 \u043a\u043b\u0430\u0434\u0438\u0442\u0435 \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c, \u0442\u044f\u043d\u0438\u0442\u0435 \u043d\u0430\u0440\u0443\u0436\u0443. \u0422\u043e\u0433\u0434\u0430 \u0442\u0435\u0441\u0442\u044b \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u043f\u0440\u043e\u0449\u0435.<\/p>\n<h4>\u041a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0442\u0430\u0434\u0438\u0439 \u0441 \u0447\u0451\u0442\u043a\u0438\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c<\/h4>\n<p>\u0425\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u2014 \u0441\u0442\u0430\u0434\u0438\u044f \u043d\u0430 \u0432\u0445\u043e\u0434, \u0441\u0442\u0430\u0434\u0438\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u041a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>CompleteAdding<\/code> \u043d\u0430 \u0441\u0432\u043e\u0451\u043c \u0432\u044b\u0445\u043e\u0434\u0435. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<pre><code class=\"cs\">public static class Pipeline {     public static void Run(CancellationToken ct)     {         using var stage1 = new BlockingCollection&lt;int&gt;(boundedCapacity: 256);         using var stage2 = new BlockingCollection&lt;string&gt;(boundedCapacity: 256);          var producer = Task.Run(() =&gt;         {             try             {                 for (var i = 0; i &lt; 10_000; i++)                 {                     ct.ThrowIfCancellationRequested();                     stage1.Add(i, ct);                 }             }             finally             {                 stage1.CompleteAdding();             }         }, ct);          var middle = Task.Run(() =&gt;         {             try             {                 foreach (var n in stage1.GetConsumingEnumerable(ct))                 {                     var line = $\"value={n}\";                     stage2.Add(line, ct);                 }             }             finally             {                 stage2.CompleteAdding();             }         }, ct);          var consumer = Task.Run(() =&gt;         {             foreach (var s in stage2.GetConsumingEnumerable(ct))             {                 \/\/ \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430             }         }, ct);          Task.WaitAll(producer, middle, consumer);     } }<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u0430\u044f. \u041d\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432. \u041d\u0435\u0442 \u0441\u043a\u0440\u044b\u0442\u044b\u0445  \u0444\u043b\u0430\u0433\u043e\u0432. \u0412\u0441\u0451 \u0432\u0438\u0434\u043d\u043e \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438 \u0432 <code>finally<\/code>.<\/p>\n<h4>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0445\u043e\u0434\u043e\u0432: TakeFromAny \u0438 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u044b\u0439 \u0432\u044b\u0445\u043e\u0434<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043c\u043e\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0433\u043e\u0442\u043e\u0432\u0430. \u0412\u0430\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u0432\u044b\u0445\u043e\u0434\u0430, \u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u043d\u0443\u0442\u044c \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438.<\/p>\n<pre><code class=\"cs\">public static void DrainAny(     BlockingCollection&lt;byte[]&gt;[] inputs,     Action&lt;byte[]&gt; handle,     CancellationToken ct) {     while (true)     {         if (AllCompleted(inputs)) break;          try         {             int idx = BlockingCollection&lt;byte[]&gt;.TakeFromAny(inputs, out var item, ct);             handle(item);         }         catch (OperationCanceledException)         {             break;         }         catch (InvalidOperationException)         {             \/\/ \u043e\u0434\u043d\u0430 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0438 \u043f\u0443\u0441\u0442\u0430 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043e\u0431\u0449\u0443\u044e \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u044c \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c         }     }      static bool AllCompleted(BlockingCollection&lt;byte[]&gt;[] arr)     {         foreach (var bc in arr)             if (!bc.IsCompleted) return false;         return true;     } }<\/code><\/pre>\n<p>\u0421\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e\u0441\u0442\u0438 \u043f\u043e \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u043c \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043d\u0435 \u043e\u0431\u0435\u0449\u0430\u0435\u0442. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442, \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u0430\u043c\u0438: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 <code>TryTake<\/code> \u0441 \u043d\u0443\u043b\u0435\u0432\u044b\u043c \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c \u0438\u0437 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u043e\u0439, \u043f\u043e\u0442\u043e\u043c <code>TryTakeFromAny<\/code> \u0438\u0437 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c.<\/p>\n<h4>\u041e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0432\u0440\u0435\u0434\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b<\/h4>\n<p>\u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0441\u0430\u043c\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u044f\u0432\u043d\u044b\u043c. \u0423\u0434\u043e\u0431\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 dead letter \u0432\u044b\u0445\u043e\u0434.<\/p>\n<pre><code class=\"cs\">public sealed class SafeWorkerPool&lt;T&gt; : IDisposable {     \/\/ ... \u0442\u0435 \u0436\u0435 \u043f\u043e\u043b\u044f, \u0447\u0442\u043e \u0438 \u0440\u0430\u043d\u044c\u0448\u0435     private readonly Action&lt;Exception, T&gt;? _onError;      public SafeWorkerPool(         int capacity, int dop,         Action&lt;T, CancellationToken&gt; handler,         Action&lt;Exception, T&gt;? onError = null)     {         \/\/ ... \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438         _onError = onError;          \/\/ \u0432 \u0432\u043e\u0440\u043a\u0435\u0440\u0435:         \/\/ try { handler(item, ct); }         \/\/ catch (Exception ex) { _onError?.Invoke(ex, item); }     }      \/\/ \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u2014 \u043a\u0430\u043a \u0432 WorkerPool }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0434\u0435\u043c\u043f\u043e\u0442\u0435\u043d\u0442\u043d\u0430\u044f \u2014 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0442\u0440\u0430\u0438\u0442\u044c \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u043f\u043e\u043f\u044b\u0442\u043e\u043a, \u0438\u043d\u043a\u0440\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432 \u043c\u0435\u0442\u0440\u0438\u043a\u0430\u0445, \u043f\u043e\u0441\u043b\u0435 N \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043c\u0435\u0440\u0442\u0432\u043e\u0435 \u043f\u0438\u0441\u044c\u043c\u043e. \u0415\u0441\u043b\u0438 \u043d\u0435 \u0438\u0434\u0435\u043c\u043f\u043e\u0442\u0435\u043d\u0442\u043d\u0430\u044f \u2014 \u0440\u0435\u0442\u0440\u0430\u0438 \u0431\u0435\u0437 \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0432\u043e\u043a\u0440\u0443\u0433 \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u0432 \u043e\u043f\u0430\u0441\u043d\u044b.<\/p>\n<h4>\u041c\u0435\u0442\u0440\u0438\u043a\u0438 \u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u043e\u0441\u0442\u044c<\/h4>\n<p>\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u0432\u0438\u0434\u0435\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0436\u0438\u0432\u043e\u0439:<\/p>\n<ul>\n<li>\n<p>\u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u043d\u0438\u043c\u043e\u043a, \u043d\u0435 \u0431\u0430\u0440\u044c\u0435\u0440 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0427\u0438\u0441\u043b\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0451\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u043b\u0438 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u0432 \u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0427\u0438\u0441\u043b\u043e \u043e\u0442\u043c\u0435\u043d\u0451\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<\/li>\n<\/ul>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 <code>System.Diagnostics.Metrics<\/code>:<\/p>\n<pre><code class=\"cs\">using System.Diagnostics.Metrics;  public sealed class BcMetrics : IDisposable {     private readonly Meter _meter = new(\"MyService.BlockingCollection\");     private int _queueLength;      public Counter&lt;long&gt; Added { get; }     public Counter&lt;long&gt; Taken { get; }     public Counter&lt;long&gt; Timeouts { get; }      public BcMetrics()     {         Added = _meter.CreateCounter&lt;long&gt;(\"added_total\");         Taken = _meter.CreateCounter&lt;long&gt;(\"taken_total\");         Timeouts = _meter.CreateCounter&lt;long&gt;(\"timeouts_total\");         _meter.CreateObservableGauge(\"queue_length\", () =&gt; new Measurement&lt;int&gt;(_queueLength));     }      public void SetLength(int value) =&gt; _queueLength = value;      public void Dispose() =&gt; _meter.Dispose(); }<\/code><\/pre>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0439\u0442\u0435 <code>SetLength(bc.Count)<\/code> \u0442\u0430\u043c, \u0433\u0434\u0435 \u0438 \u0442\u0430\u043a \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e. \u041d\u0435 \u043f\u043b\u043e\u0434\u0438\u0442\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0430\u0439\u043c\u0435\u0440\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u0438\u043d\u044b \u043d\u0430 \u0432\u044b\u0441\u043e\u043a\u0438\u0445 \u0447\u0430\u0441\u0442\u043e\u0442\u0430\u0445 \u0431\u0435\u0437 \u0441\u043c\u044b\u0441\u043b\u0430.<\/p>\n<h4>\u0420\u0430\u0437\u043c\u0435\u0440 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c: \u043a\u0430\u043a \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430<\/h4>\n<p>\u0416\u0451\u0441\u0442\u043a\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0440\u0435\u0434\u043a\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0420\u0430\u0437\u0443\u043c\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434:<\/p>\n<ul>\n<li>\n<p>\u0412\u0435\u0440\u0445\u043d\u044f\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 \u2014 \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0431\u044e\u0434\u0436\u0435\u0442\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0445\u0432\u043e\u0441\u0442\u0430 \u0432 \u043f\u0438\u043a. \u0415\u0441\u043b\u0438 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u2014 2 \u041a\u0411, \u0438 \u0433\u043e\u0442\u043e\u0432\u044b \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\u0435 \u0434\u043e 50 \u0442\u044b\u0441\u044f\u0447, \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c 50 \u0442\u044b\u0441\u044f\u0447 \u2014 \u044d\u0442\u043e \u043f\u043b\u044e\u0441-\u043c\u0438\u043d\u0443\u0441 100 \u041c\u0411 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0435 \u0441\u0447\u0438\u0442\u0430\u044f \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0440\u0430\u0441\u0445\u043e\u0434\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c \u2014 \u0438\u0437 \u043f\u0440\u043e\u0444\u0438\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430. \u0415\u0441\u043b\u0438 \u043e\u043d CPU-bound, \u0441\u0442\u0430\u0440\u0442\u0443\u0439\u0442\u0435 \u0441 \u0447\u0438\u0441\u043b\u0430 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u044f\u0434\u0435\u0440 \u043c\u0438\u043d\u0443\u0441 \u043e\u0434\u0438\u043d. \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0447\u0430\u0441\u0442\u043e \u0436\u0434\u0451\u0442 IO, \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c, \u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u0435 \u0437\u0430 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0438 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0446\u0438\u0435\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0438\u0434\u0438\u0442\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u2014 \u043b\u0438\u0431\u043e \u043c\u0430\u043b\u043e \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043b\u0438\u0431\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0430\u044f. \u0423\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0435 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u0435\u0437 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u2014 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0440\u0430\u0449\u0438\u0432\u0430\u0435\u0442 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443, \u0430 \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c.<\/p>\n<\/li>\n<\/ul>\n<h4>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 Generic Host: \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443<\/h4>\n<p>\u0412 \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u0445 \u043d\u0430 <code>IHost<\/code> \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e <code>ApplicationStopping<\/code> \u0432\u0441\u0451 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u043b\u043e\u0441\u044c \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u043e. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u0430: \u043d\u0430 \u0441\u0438\u0433\u043d\u0430\u043b\u0435 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>CompleteAdding<\/code>, \u0436\u0434\u0451\u043c \u0432\u044b\u0445\u043e\u0434 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0430, \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0445\u043e\u0434\u0438\u043c.<\/p>\n<pre><code class=\"cs\">public sealed class QueueService : IHostedService {     private readonly WorkerPool&lt;MyItem&gt; _pool;     private readonly IHostApplicationLifetime _lifetime;      public QueueService(IHostApplicationLifetime lifetime)     {         _lifetime = lifetime;         _pool = new WorkerPool&lt;MyItem&gt;(             boundedCapacity: 10_000,             degreeOfParallelism: Environment.ProcessorCount,             handler: ProcessItem);     }      public Task StartAsync(CancellationToken cancellationToken)     {         _lifetime.ApplicationStopping.Register(() =&gt;         {             _pool.Complete();             _pool.Dispose();         });         return Task.CompletedTask;     }      public Task StopAsync(CancellationToken cancellationToken) =&gt; Task.CompletedTask;      private static void ProcessItem(MyItem item, CancellationToken ct)     {         \/\/ \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430     } }<\/code><\/pre>\n<p>\u0413\u043b\u0430\u0432\u043d\u0430\u044f \u043c\u044b\u0441\u043b\u044c \u043d\u0435 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0434\u0438\u0441\u043f\u043e\u0437\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e, \u043f\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u0432\u0438\u0441\u044f\u0449\u0438\u0435 <code>Take<\/code>. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u0432\u0445\u043e\u0434, \u0437\u0430\u0442\u0435\u043c \u0436\u0434\u0451\u043c.<\/p>\n<h4>\u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0440\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0430 Channel<\/h4>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435, \u0432\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u0442\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435\u043c, \u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043c\u044b\u0441\u043b \u0432 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u0439, \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0438 \u0441 <code>async\/await<\/code> \u2014 \u043a\u0430\u043d\u0430\u043b\u044b \u043e\u0449\u0443\u0442\u0438\u043c\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435. \u0422\u0430\u043c \u0435\u0441\u0442\u044c bounded-\u0440\u0435\u0436\u0438\u043c\u044b, backpressure, \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, <code>ReadAllAsync<\/code>, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u043d\u043a\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0436\u0438\u043c \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430. \u0414\u043b\u044f CPU-bound \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432\u044b\u0438\u0433\u0440\u044b\u0448 \u043d\u0430 \u043a\u0430\u043d\u0430\u043b\u0430\u0445 \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u0442\u0435\u043d \u0437\u0430 \u0441\u0447\u0451\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0442\u043e\u043a\u0430. \u0414\u043b\u044f \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u2014 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u2014 \u043f\u0440\u043e\u0444\u0438\u043b\u044c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c. \u041d\u0435 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0440\u0430\u0434\u0438 \u043c\u043e\u0434\u044b.<\/p>\n<h4>\u0418\u0442\u043e\u0433\u043e<\/h4>\n<p>BlockingCollection \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438 \u043d\u0430\u0434\u0451\u0436\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u044b\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c. \u0414\u0435\u043b\u0438\u0442\u0435\u0441\u044c \u043a\u0435\u0439\u0441\u0430\u043c\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<hr\/>\n<p>\u0414\u0430\u0436\u0435 \u0441\u0430\u043c\u0430\u044f \u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u0433\u0440\u0443\u0431\u043e\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e: API \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0437\u043e\u043e\u043f\u0430\u0440\u043a \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u0439, \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442 \u0441\u0431\u043e\u0438\u0442\u044c \u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439, \u0430 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043f\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u0430\u043c \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u2014 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c \u0438\u0445 \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 \u0438 \u043d\u0430\u0439\u0442\u0438 \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0445 \u0443\u0440\u043e\u043a\u0430\u0445:<\/p>\n<ul>\n<li>\n<p>11 \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044f \u0432 20:00 \u2014 API Gateway \u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e: \u0448\u0430\u0433\u0438 \u043a \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 API. <a href=\"https:\/\/otus.pw\/ndjC1\/\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<li>\n<p>17 \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044f \u0432 20:00 \u2014 \u041f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u043e\u0442\u043a\u0430\u0437\u043e\u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u0438 \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u043d\u043e\u0439 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b. <a href=\"https:\/\/otus.pw\/i0RM\/\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f<\/a><\/p>\n<\/li>\n<\/ul>\n<p><a href=\"https:\/\/otus.pw\/k0XKp\/\" rel=\"noopener noreferrer nofollow\"><strong>\u041f\u0440\u043e\u0439\u0434\u0438\u0442\u0435 \u0432\u0441\u0442\u0443\u043f\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/strong><\/a>, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u044b \u0433\u043e\u0442\u043e\u0432\u044b \u043a \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e \u0438 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442 \u043b\u0438 \u043a\u0443\u0440\u0441 <em>&#171;<\/em>Software Architect<em>&#171;<\/em> \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0430\u043c.<\/p>\n<p><em>\u0410 \u0447\u0442\u043e\u0431\u044b \u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043a\u0443\u0440\u0441\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u0438 \u0442\u0440\u0435\u043d\u0434\u043e\u0432, \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0439\u0442\u0435\u0441\u044c \u043d\u0430 <\/em><a href=\"https:\/\/t.me\/+dxHyB6AgI99kNDUy\" rel=\"noopener noreferrer nofollow\"><em>Telegram-\u043a\u0430\u043d\u0430\u043b OTUS.<\/em><\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/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\/articles\/943164\/\"> https:\/\/habr.com\/ru\/articles\/943164\/<\/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!<\/p>\n<p>\u0412 .NET \u0436\u0438\u0432\u0451\u0442 \u0441\u0442\u0430\u0440\u044b\u0439, \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u2014 <code>BlockingCollection&lt;T&gt;<\/code>. \u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c producer\u2013consumer, \u0434\u0430\u0451\u0442 \u0441\u0442\u0440\u043e\u0433\u0443\u044e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0443\u0435\u043c\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u0442\u0435 \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438. \u0414\u0430, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>Channel&lt;T&gt;<\/code>, \u0435\u0441\u0442\u044c TPL Dataflow, \u0435\u0441\u0442\u044c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0441\u0430\u043c\u043e\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u043d\u0430 <code>SemaphoreSlim<\/code>. \u041d\u043e \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u044b \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0438 \u0436\u0435\u043b\u0435\u0437\u043d\u044b\u0439 backpressure \u0431\u0435\u0437 <code>BlockingCollection<\/code> \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0445\u043e\u0440\u043e\u0448\u043e. <\/p>\n<h4>\u0427\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u0430\u0451\u0442 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f<\/h4>\n<ol>\n<li>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u0415\u0451 \u0437\u0430\u0434\u0430\u044e\u0442 \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u0431\u044e\u0434\u0436\u0435\u0442 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0442\u043e\u0440\u043c\u0430\u0436\u0438\u0432\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0411\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. <code>Add<\/code> \u0438 <code>Take<\/code> \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0442\u043e\u043a \u0434\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u0441\u0442\u0430 \u0438\u043b\u0438 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u0415\u0441\u0442\u044c \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438 <code>TryAdd<\/code> \u0438 <code>TryTake<\/code> \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0430\u043c\u0438 \u0438 <code>CancellationToken<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 <code>CompleteAdding<\/code>, \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043f\u043e <code>IsCompleted<\/code> \u0438 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0431\u0435\u0437 \u0432\u043e\u0440\u043d\u0438\u043d\u0433\u043e\u0432 \u0438 \u043f\u043e\u0434\u0432\u0438\u0441\u0430\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441 \u0438\u0437\u044a\u044f\u0442\u0438\u0435\u043c. <code>GetConsumingEnumerable<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f \u0447\u0435\u0440\u0435\u0437 <code>foreach<\/code>, \u0431\u0435\u0437 \u0440\u0443\u0447\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439. \u0415\u0441\u0442\u044c <code>TakeFromAny<\/code> \u0438 <code>AddToAny<\/code>, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043a\u0430\u043a \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e.<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c \u043f\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>ConcurrentQueue&lt;T&gt;<\/code> (FIFO), \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u0438 <code>ConcurrentStack&lt;T&gt;<\/code> (LIFO), \u0438 <code>ConcurrentBag&lt;T&gt;<\/code> \u2014 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0441\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0438.<\/p>\n<h4>\u041a\u0430\u0440\u043a\u0430\u0441 \u043f\u0443\u043b\u0430 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432 \u0431\u0435\u0437 \u043f\u043e\u0434\u0432\u0438\u0441\u0430\u043d\u0438\u0439<\/h4>\n<p>\u0417\u0430\u0434\u0430\u0447\u0430: \u0434\u0430\u0442\u044c backpressure, \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e \u0441\u0438\u0433\u043d\u0430\u043b\u0443, \u043d\u0435 \u043b\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u0432\u0438\u0441\u0448\u0438\u0435 <code>Take<\/code>, \u043d\u0435 \u0441\u0436\u0435\u0447\u044c CPU \u0441\u043f\u0438\u043d\u043e\u043c.<\/p>\n<pre><code class=\"cs\">using System; using System.Collections.Concurrent; using System.Threading; using System.Threading.Tasks;  public sealed class WorkerPool&lt;T&gt; : IDisposable {     private readonly BlockingCollection&lt;T&gt; _queue;     private readonly Task[] _workers;     private readonly CancellationTokenSource _cts = new();      public WorkerPool(         int boundedCapacity,         int degreeOfParallelism,         Action&lt;T, CancellationToken&gt; handler)     {         if (boundedCapacity &lt;= 0) throw new ArgumentOutOfRangeException(nameof(boundedCapacity));         if (degreeOfParallelism &lt;= 0) throw new ArgumentOutOfRangeException(nameof(degreeOfParallelism));         if (handler is null) throw new ArgumentNullException(nameof(handler));          _queue = new BlockingCollection&lt;T&gt;(new ConcurrentQueue&lt;T&gt;(), boundedCapacity);          _workers = new Task[degreeOfParallelism];         for (int i = 0; i &lt; degreeOfParallelism; i++)         {             _workers[i] = Task.Factory.StartNew(() =&gt;             {                 var ct = _cts.Token;                 while (!_queue.IsCompleted)                 {                     try                     {                         var item = _queue.Take(ct);                         handler(item, ct);                     }                     catch (OperationCanceledException)                     {                         break;                     }                     catch (InvalidOperationException)                     {                         break; \/\/ \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430 \u0438 \u043f\u0443\u0441\u0442\u0430                     }                 }             }, TaskCreationOptions.LongRunning);         }     }      public bool TryPost(T item, TimeSpan timeout, CancellationToken ct = default)         =&gt; _queue.TryAdd(item, timeout, ct);      public void Complete() =&gt; _queue.CompleteAdding();      public void Dispose()     {         try         {             _cts.Cancel();             _queue.CompleteAdding();             Task.WaitAll(_workers, TimeSpan.FromSeconds(5));         }         finally         {             _queue.Dispose();             _cts.Dispose();         }     } }<\/code><\/pre>\n<p><code>TaskCreationOptions.LongRunning<\/code> \u043f\u0440\u043e\u0441\u0438\u0442 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u0434 \u0432\u043e\u0440\u043a\u0435\u0440, \u044d\u0442\u043e \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0446\u0438\u044e \u0437\u0430 \u043f\u0443\u043b, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043d\u0435 \u043c\u0438\u043a\u0440\u043e\u0441\u043a\u043e\u043f\u0438\u0447\u0435\u0441\u043a\u0430\u044f. \u041d\u0435 \u0441\u0442\u043e\u0438\u0442 \u0441\u0442\u0430\u0432\u0438\u0442\u044c LongRunning \u0432\u0435\u0437\u0434\u0435 \u043f\u043e \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0435. \u0415\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f, \u043e\u0431\u044b\u0447\u043d\u044b\u0435 <code>Task.Run<\/code> \u0431\u0435\u0437 LongRunning \u0434\u0430\u044e\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u0443\u044e \u0443\u0442\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u044e.<\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043b\u0438 <code>CompleteAdding<\/code> \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430, <code>Take<\/code> \u043a\u0438\u0434\u0430\u0435\u0442 <code>InvalidOperationException<\/code>. <\/p>\n<h4>\u0422\u0430\u0439\u043c\u0430\u0443\u0442\u044b, \u043e\u0442\u043c\u0435\u043d\u0430 \u0438 \u043e\u0442\u043a\u0430\u0437 \u043e\u0442 \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043f\u0438\u043d\u043e\u0432<\/h4>\n<p>\u0421\u043f\u0438\u043d \u0441 <code>TryTake<\/code> \u0432 \u0432\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u0433\u0434\u0435 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 <code>Thread.Sleep(1)<\/code> \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f. \u042d\u0442\u043e \u043d\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 CPU \u0431\u0435\u0437 \u043f\u043e\u043b\u044c\u0437\u044b \u0438 \u043f\u043e\u0440\u0442\u0438\u0442 \u044d\u043d\u0435\u0440\u0433\u043e\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435. \u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u0430\u043c\u0438 \u0438 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u043e\u0442\u043c\u0435\u043d\u043e\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u0434\u0435\u043a\u0432\u0430\u0442\u043d\u0435\u0435:<\/p>\n<pre><code class=\"cs\">public static void DrainWithTimeout&lt;T&gt;(     BlockingCollection&lt;T&gt; bc,     Action&lt;T&gt; handle,     TimeSpan wait,     CancellationToken ct) {     while (!bc.IsCompleted)     {         try         {             if (bc.TryTake(out var item, (int)wait.TotalMilliseconds, ct))             {                 handle(item);             }             \/\/ \u0438\u043d\u0430\u0447\u0435 \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0434\u0451\u043c \u0432 \u043d\u043e\u0432\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e         }         catch (OperationCanceledException)         {             break;         }         catch (InvalidOperationException)         {             break;         }     } }<\/code><\/pre>\n<p>\u0422\u0430\u0439\u043c\u0430\u0443\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0439\u0442\u0435 \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 SLA \u043d\u0430 \u043b\u0430\u0442\u0435\u043d\u0442\u043d\u043e\u0441\u0442\u044c. \u0415\u0441\u043b\u0438 \u0432\u0430\u0436\u043d\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u043d\u043e \u0432\u044b\u0448\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u044f CPU, \u0442\u0430\u0439\u043c\u0430\u0443\u0442 \u0431\u043e\u043b\u044c\u0448\u0435. \u0423\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043d\u0435\u0442.<\/p>\n<h4>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044f\u0445<\/h4>\n<p>\u0422\u0438\u043f\u0438\u0447\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>CompleteAdding<\/code> \u0438\u0437 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0432\u0441\u0451. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u044d\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0443\u0442\u044c \u043a \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c. \u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u2014 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0443\u0440\u043e\u0432\u043d\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u043e\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u0442. \u041f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044f\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e <code>IsCompleted<\/code>.<\/p>\n<p>\u0411\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c <code>CompleteAdding<\/code> \u0432 <code>finally<\/code> \u0440\u044f\u0434\u043e\u043c \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u043c, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0434 \u043d\u0435 \u0434\u043e\u0431\u0435\u0436\u0430\u043b \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0446\u0438\u043a\u043b\u0430 \u0438\u0437-\u0437\u0430 \u043e\u0442\u043c\u0435\u043d\u044b \u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f. <\/p>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u0431\u044b\u0432\u0430\u044e\u0442. <code>Dispose<\/code> \u043d\u0430 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 <code>Take<\/code>. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a: <code>CompleteAdding<\/code>, \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0432\u043e\u0440\u043a\u0435\u0440\u043e\u0432, \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e <code>Dispose<\/code>. \u041f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u0443\u0431\u0438\u0442\u044c \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0434\u0438\u0441\u043f\u043e\u0437\u043e\u043c \u0442\u0430\u043a \u0441\u0435\u0431\u0435 \u0437\u0430\u0442\u0435\u044f.<\/p>\n<h4>\u0412\u044b\u0431\u043e\u0440 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c<\/h4>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441\u0442\u043e\u0438\u0442 <code>ConcurrentQueue&lt;T&gt;<\/code> \u2014 \u044d\u0442\u043e FIFO \u0438 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u0438\u0441\u0442\u0435\u043c \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u0430\u044f:<\/p>\n<ul>\n<li>\n<p>\u041d\u0443\u0436\u0435\u043d LIFO, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043f\u0440\u0438\u0448\u0435\u0434\u0448\u0438\u0439 \u0432\u0430\u0436\u043d\u0435\u0435 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435 <code>ConcurrentStack&lt;T&gt;<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u043b \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441 \u0431\u0435\u0437\u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435\u043c \u043a \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u2014 <code>ConcurrentBag&lt;T&gt;<\/code>.<\/p>\n<\/li>\n<\/ul>\n<p><code>PriorityQueue&lt;TElement,TPriority&gt;<\/code> \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 <code>IProducerConsumerCollection&lt;T&gt;<\/code>, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e\u0434\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u0430 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0437\u0430\u0446\u0438\u044f, \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u0432\u043e\u044e \u043e\u0431\u043e\u043b\u043e\u0447\u043a\u0443, \u043b\u0438\u0431\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a <code>Channel&lt;T&gt;<\/code> \u0438 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438.<\/p>\n<h4>GetConsumingEnumerable: \u0447\u0438\u0441\u0442\u044b\u0439 \u043f\u043e\u0442\u0440\u0435\u0431\u0438\u0442\u0435\u043b\u044c \u0431\u0435\u0437 \u0440\u0443\u0447\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432<\/h4>\n<p>\u041d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u0438\u043c \u0438\u0437-\u0437\u0430 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438. \u041f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043a\u0440\u044b\u0442\u0430 \u0438 \u043e\u043f\u0443\u0441\u0442\u0435\u043b\u0430.<\/p>\n<pre><code class=\"cs\">public static Task StartConsumer&lt;T&gt;(     BlockingCollection&lt;T&gt; bc,     Action&lt;T&gt; handler,     CancellationToken ct) {     return Task.Factory.StartNew(() =&gt;     {         foreach (var item in bc.GetConsumingEnumerable(ct))             handler(item);     }, TaskCreationOptions.LongRunning); }<\/code><\/pre>\n<p>\u0423 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043e\u0434\u0438\u043d \u043d\u044e\u0430\u043d\u0441: \u043d\u0435 \u043a\u043b\u0430\u0434\u0438\u0442\u0435 \u0442\u044f\u0436\u0451\u043b\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u043f\u0440\u044f\u043c\u043e \u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c, \u0442\u044f\u043d\u0438\u0442\u0435 \u043d\u0430\u0440\u0443\u0436\u0443. \u0422\u043e\u0433\u0434\u0430 \u0442\u0435\u0441\u0442\u044b \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0430 \u043f\u0440\u043e\u0449\u0435.<\/p>\n<h4>\u041a\u043e\u043d\u0432\u0435\u0439\u0435\u0440 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0442\u0430\u0434\u0438\u0439 \u0441 \u0447\u0451\u0442\u043a\u0438\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c<\/h4>\n<p>\u0425\u043e\u0440\u043e\u0448\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430 \u2014 \u0441\u0442\u0430\u0434\u0438\u044f \u043d\u0430 \u0432\u0445\u043e\u0434, \u0441\u0442\u0430\u0434\u0438\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434. \u041a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0445\u043e\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 <code>CompleteAdding<\/code> \u043d\u0430 \u0441\u0432\u043e\u0451\u043c \u0432\u044b\u0445\u043e\u0434\u0435. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/p>\n<pre><code class=\"cs\">public static class Pipeline {     public static void Run(CancellationToken ct)     {         using var stage1 = new BlockingCollection&lt;int&gt;(boundedCapacity: 256);         using var stage2 = new BlockingCollection&lt;string&gt;(boundedCapacity: 256);          var producer = Task.Run(() =&gt;         {             try             {                 for (var i = 0; i &lt; 10_000; i++)                 {                     ct.ThrowIfCancellationRequested();                     stage1.Add(i, ct);                 }             }             finally             {                 stage1.CompleteAdding();             }         }, ct);          var middle = Task.Run(() =&gt;         {             try             {                 foreach (var n in stage1.GetConsumingEnumerable(ct))                 {                     var line = $\"value={n}\";                     stage2.Add(line, ct);                 }             }             finally             {                 stage2.CompleteAdding();             }         }, ct);          var consumer = Task.Run(() =&gt;         {             foreach (var s in stage2.GetConsumingEnumerable(ct))             {                 \/\/ \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430             }         }, ct);          Task.WaitAll(producer, middle, consumer);     } }<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u0430\u044f. \u041d\u0435\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u0432. \u041d\u0435\u0442 \u0441\u043a\u0440\u044b\u0442\u044b\u0445  \u0444\u043b\u0430\u0433\u043e\u0432. \u0412\u0441\u0451 \u0432\u0438\u0434\u043d\u043e \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438 \u0432 <code>finally<\/code>.<\/p>\n<h4>\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0445\u043e\u0434\u043e\u0432: TakeFromAny \u0438 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u044b\u0439 \u0432\u044b\u0445\u043e\u0434<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e, \u043c\u043e\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0433\u043e\u0442\u043e\u0432\u0430. \u0412\u0430\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u0432\u044b\u0445\u043e\u0434\u0430, \u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u043d\u0443\u0442\u044c \u0432 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438.<\/p>\n<pre><code class=\"cs\">public static void DrainAny(     BlockingCollection&lt;byte[]&gt;[] inputs,     Action&lt;byte[]&gt; handle,     CancellationToken ct) {     while (true)     {         if (AllCompleted(inputs)) break;          try         {             int idx = BlockingCollection&lt;byte[]&gt;.TakeFromAny(inputs, out var item, ct);             handle(item);         }         catch (OperationCanceledException)         {             break;         }         catch (InvalidOperationException)         {             \/\/ \u043e\u0434\u043d\u0430 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0430\u0441\u044c \u0438 \u043f\u0443\u0441\u0442\u0430 \u2014 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043e\u0431\u0449\u0443\u044e \u0433\u043e\u0442\u043e\u0432\u043d\u043e\u0441\u0442\u044c \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c         }     }      static bool AllCompleted(BlockingCollection&lt;byte[]&gt;[] arr)     {         foreach (var bc in arr)             if (!bc.IsCompleted) return false;         return true;     } }<\/code><\/pre>\n<p>\u0421\u043f\u0440\u0430\u0432\u0435\u0434\u043b\u0438\u0432\u043e\u0441\u0442\u0438 \u043f\u043e \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430\u043c \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043d\u0435 \u043e\u0431\u0435\u0449\u0430\u0435\u0442. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0439 \u0447\u0435\u0440\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0442. \u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442, \u0434\u0435\u043b\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0441\u0430\u043c\u0438: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 <code>TryTake<\/code> \u0441 \u043d\u0443\u043b\u0435\u0432\u044b\u043c \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c \u0438\u0437 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u043e\u0439, \u043f\u043e\u0442\u043e\u043c <code>TryTakeFromAny<\/code> \u0438\u0437 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441 \u0442\u0430\u0439\u043c\u0430\u0443\u0442\u043e\u043c.<\/p>\n<h4>\u041e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0432\u0440\u0435\u0434\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b<\/h4>\n<p>\u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0441\u0430\u043c\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u044d\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u044f\u0432\u043d\u044b\u043c. \u0423\u0434\u043e\u0431\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438\u043d\u044a\u0435\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 dead letter \u0432\u044b\u0445\u043e\u0434.<\/p>\n<pre><code class=\"cs\">public sealed class SafeWorkerPool&lt;T&gt; : IDisposable {     \/\/ ... \u0442\u0435 \u0436\u0435 \u043f\u043e\u043b\u044f,<\/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-473854","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473854","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=473854"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/473854\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=473854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=473854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=473854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}