Async with queue size

Scenario:

Process records asynchronously with batch 

Solution:

  • Post async all
    1.  1
       2
       3
       4
       5
       6
       7
       8
       9
      10
              public async Task PostAsync<T>(params T[] infos) where T : IData
              {
                  await Task.WhenAll(infos.Select(async d =>
                  {
                      var stopwatch = Stopwatch.StartNew();
                      await InternalPostAsync(d);
                      stopwatch.Stop();
                      log = $("{stopwatch.ElapsedMilliseconds} ms");
                  }));
              }
  • Post async to queue
    1. 1
      2
      3
      4
              public async Task PostAsync<T>(params T[] infos) where T : IData
              {
                  await Task.Run(() => _queue.AddRecord(infos as IData[]));
              }

  • Process batch in queue
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
            public void AddRecord(IData[] record)
            {
                if (record.Length > 0)
                {
                    var size = ConfigConRequests;
    
                    for (var i = 0; i < record.Length; i += size)
                    {
                      ...
                    }
                }
            }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...