2021年3月6日星期六

Only one async task operation at a time per a symbol

SubscribeToKlineUpdatesAsync is a web socket stream event handler receiving data on each second from Binance. I have a long-running operation for 10 seconds inside it. It's separated into a task, because I don't want it to block any incoming price data.

The problem is that it spawns thousand of tasks at a time and I want it to spawn only one task at a time per a symbol. The symbol is identified by data.Symbol. Below the code, you'll find the logs. The second log is what I want it to be like.

I would like to use something like TPL Dataflow or Rx.NET. I just don't know how to do it. I'm aware of the lock way, BlockingCollection and SemaphoreSlim.

There is a log below of what's current and what I want to accomplish.

Code

using System;  using System.Threading;  using System.Threading.Tasks;  using Binance.Net;  using Binance.Net.Enums;  using Binance.Net.Interfaces;  using Binance.Net.Objects.Spot;  using CryptoExchange.Net.Authentication;  using CryptoExchange.Net.Logging;    namespace SubscribeToCandlesEventFixTest  {      public class BinanceSocketHandler      {          private readonly IBinanceClient _client;          private readonly IBinanceSocketClient _socketClient;            public BinanceSocketHandler()          {              _client = new BinanceClient(new BinanceClientOptions              {                  ApiCredentials = new ApiCredentials("not required", "not required"),                  AutoTimestamp = true,                  AutoTimestampRecalculationInterval = TimeSpan.FromMinutes(30),  #if DEBUG                  LogVerbosity = LogVerbosity.Debug  

没有评论:

发表评论