<commit_before>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Jasper.Messaging.Transports;
using Microsoft.Extensions.Hosting;

namespace Jasper.Messaging
{
    public class BackPressureAgent : IHostedService
    {
        private readonly IMessagingRoot _root;
        private Timer _timer;

        public BackPressureAgent(IMessagingRoot root)
        {
            _root = root;
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            _timer = new Timer(s => ApplyBackPressure(), null, _root.Settings.BackPressurePollingInterval, _root.Settings.BackPressurePollingInterval);
            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            _timer?.Dispose();
            return Task.CompletedTask;
        }

        public void ApplyBackPressure()
        {
            double ratio = (double)_root.Workers.QueuedCount / (double)_root.Settings.MaximumLocalEnqueuedBackPressureThreshold;

            if (_root.ListeningStatus == ListeningStatus.Accepting && ratio > 1.0)
            {
                _root.ListeningStatus = ListeningStatus.TooBusy;
            }
            else if (_root.ListeningStatus == ListeningStatus.TooBusy && ratio < 0.9)
            {
                _root.ListeningStatus = ListeningStatus.Accepting;
            }
        }
    }
}<commit_msg>Switched BackPressureAgent to inherit from BackgroundService<commit_after>﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Jasper.Messaging.Transports;
using Microsoft.Extensions.Hosting;

namespace Jasper.Messaging
{
    public class BackPressureAgent : BackgroundService
    {
        private readonly IMessagingRoot _root;

        public BackPressureAgent(IMessagingRoot root)
        {
            _root = root;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(_root.Settings.BackPressurePollingInterval, stoppingToken);
                try
                {
                    ApplyBackPressure();
                }
                catch (Exception e)
                {
                    _root.Logger.LogException(e);
                }
            }
        }


        public void ApplyBackPressure()
        {
            double ratio = (double)_root.Workers.QueuedCount / (double)_root.Settings.MaximumLocalEnqueuedBackPressureThreshold;

            if (_root.ListeningStatus == ListeningStatus.Accepting && ratio > 1.0)
            {
                _root.ListeningStatus = ListeningStatus.TooBusy;
            }
            else if (_root.ListeningStatus == ListeningStatus.TooBusy && ratio < 0.9)
            {
                _root.ListeningStatus = ListeningStatus.Accepting;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07349438584552569, 'fraction_numerical': 0.0027220142905750254, 'mean_word_length': 2.003064351378958, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '515107', 'n_tokens_mistral': 773, 'n_tokens_neox': 744, 'n_words': 139}