Search code examples
c#multithreadingentity-frameworkwhile-loopconcurrent-queue

Thread + While(true) + Entity


I'm building a candle recorder (Binance Crypto), interesting in 1 minute candles, including intra candle data for market study purpose (But eventually I could use this same code to actually be my eyes on what's happening in the market)

To avoid eventual lag / EF / SQL performance etc. I decided do accomplish this using two threads.

One receives the subscribed (Async) tokens from Binance and put them in a ConcurrentQueue, while another keeps trying to dequeue and save the data in MSSQL

My question goes for the second Thread, a while(true) loop. Whats the best approach to save like 200 + info/sec to SQL while these info come in individually (sometimes 300 info in a matter of 300ms, sometime less) using EF:

Should I open the SQL con each time I want to save? (Performance). Whats the best approach to accomplish this?

-- EDITED -- At one point I got 600k+ in the Queue so I'm facing problems inserting to SQL Changed from Linq to SQL to EF

Here's my actual code:

//Initialize 
        public void getCoinsMoves()
        {
            Thread THTransferDatatoSQL = new Thread(TransferDatatoSQL);
            THTransferDatatoSQL.Name = "THTransferDatatoSQL";
            THTransferDatatoSQL.SetApartmentState(ApartmentState.STA);
            THTransferDatatoSQL.IsBackground = true;
            THTransferDatatoSQL.Start();

            List<string> SymbolsMap;
            using(DBBINANCEEntities lSQLBINANCE = new DBBINANCEEntities())
            {
                SymbolsMap = lSQLBINANCE.TB_SYMBOLS_MAP.Select(h => h.SYMBOL).ToList();
            }

            socketClient.Spot.SubscribeToKlineUpdatesAsync(SymbolsMap, Binance.Net.Enums.KlineInterval.OneMinute, h =>
            {
                RecordCandles(h);
            });
        }

//Enqueue Data
        public void RecordCandles(Binance.Net.Interfaces.IBinanceStreamKlineData Candle)
        {
            FRACTIONED_CANDLES.Enqueue(new TB_FRACTIONED_CANDLES_DATA()
            {
                BASE_VOLUME = Candle.Data.BaseVolume,
                CLOSE_TIME = Candle.Data.CloseTime.AddHours(-3),
                MONEY_VOLUME = Candle.Data.QuoteVolume,
                PCLOSE = Candle.Data.Close,
                PHIGH = Candle.Data.High,
                PLOW = Candle.Data.Low,
                POPEN = Candle.Data.Open,
                SYMBOL = Candle.Symbol,
                TAKER_BUY_BASE_VOLUME = Candle.Data.TakerBuyBaseVolume,
                TAKER_BUY_MONEY_VOLUME = Candle.Data.TakerBuyQuoteVolume,
                TRADES = Candle.Data.TradeCount,
                IS_LAST_CANDLE = Candle.Data.Final
            });
        }

//Transfer Data to SQL
        public void TransferDatatoSQL()
        {
            while (true)
            {
                TB_FRACTIONED_CANDLES_DATA NewData;
                if (FRACTIONED_CANDLES.TryDequeue(out NewData))
                {
                    using (DBBINANCEEntities LSQLBINANCE = new DBBINANCEEntities())
                    {
                        LSQLBINANCE.TB_FRACTIONED_CANDLES_DATA.Add(NewData);
                        if (NewData.IS_LAST_CANDLE)
                            LSQLBINANCE.TB_CANDLES_DATA.Add(new TB_CANDLES_DATA()
                            {
                                BASE_VOLUME = NewData.BASE_VOLUME,
                                CLOSE_TIME = NewData.CLOSE_TIME,
                                IS_LAST_CANDLE = NewData.IS_LAST_CANDLE,
                                MONEY_VOLUME = NewData.MONEY_VOLUME,
                                PCLOSE = NewData.PCLOSE,
                                PHIGH = NewData.PHIGH,
                                PLOW = NewData.PLOW,
                                POPEN = NewData.POPEN,
                                SYMBOL = NewData.SYMBOL,
                                TAKER_BUY_BASE_VOLUME = NewData.TAKER_BUY_BASE_VOLUME,
                                TAKER_BUY_MONEY_VOLUME = NewData.TAKER_BUY_MONEY_VOLUME,
                                TRADES = NewData.TRADES
                            });
                        LSQLBINANCE.SaveChanges();
                    }
                }
                Thread.Sleep(1);
            }            
        }

Thx in Adv

Rafael


Solution

  • I see one error in your code, you're sleeping a background thread after every insert, don't sleep if there's more data. Instead of:

    if (FRACTIONED_CANDLES.TryDequeue(out NewData))
    {
        using (DBBINANCEEntities LSQLBINANCE = new DBBINANCEEntities())
        {
            LSQLBINANCE.TB_FRACTIONED_CANDLES_DATA.Add(NewData);
            if (NewData.IS_LAST_CANDLE)
                LSQLBINANCE.TB_CANDLES_DATA.Add(new TB_CANDLES_DATA()
                {
                    BASE_VOLUME = NewData.BASE_VOLUME,
                    CLOSE_TIME = NewData.CLOSE_TIME,
                    IS_LAST_CANDLE = NewData.IS_LAST_CANDLE,
                    MONEY_VOLUME = NewData.MONEY_VOLUME,
                    PCLOSE = NewData.PCLOSE,
                    PHIGH = NewData.PHIGH,
                    PLOW = NewData.PLOW,
                    POPEN = NewData.POPEN,
                    SYMBOL = NewData.SYMBOL,
                    TAKER_BUY_BASE_VOLUME = NewData.TAKER_BUY_BASE_VOLUME,
                    TAKER_BUY_MONEY_VOLUME = NewData.TAKER_BUY_MONEY_VOLUME,
                    TRADES = NewData.TRADES
                });
            LSQLBINANCE.SaveChanges();
        }
    }
    Thread.Sleep(1);
    

    Change the last line to:

    else
        Thread.Sleep(1);
    

    This may resolve your problem.