<commit_before>﻿using Hudl.Mjolnir.Breaker;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Hudl.Mjolnir.Command
{
    internal interface IBreakerInvoker
    {
        Task<TResult> ExecuteWithBreakerAsync<TResult>(AsyncCommand<TResult> command, CancellationToken ct);
        TResult ExecuteWithBreaker<TResult>(SyncCommand<TResult> command, CancellationToken ct);
    }

    internal class BreakerInvoker : IBreakerInvoker
    {
        private readonly ICommandContext _context;

        public BreakerInvoker(ICommandContext context)
        {
            _context = context ?? CommandContext.Current;
        }

        public async Task<TResult> ExecuteWithBreakerAsync<TResult>(AsyncCommand<TResult> command, CancellationToken ct)
        {
            var breaker = _context.GetCircuitBreaker(command.BreakerKey);

            if (!breaker.IsAllowing())
            {
                throw new CircuitBreakerRejectedException();
            }

            TResult result;

            var stopwatch = Stopwatch.StartNew();
            try
            {
                // Await here so we can catch the Exception and track the state.
                // I suppose we could do this with a continuation, too. Await's easier.
                result = await command.ExecuteAsync(ct).ConfigureAwait(false);

                breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                breaker.Metrics.MarkCommandSuccess();
            }
            catch (Exception e)
            {
                if (_context.IsExceptionIgnored(e.GetType()))
                {
                    breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                    breaker.Metrics.MarkCommandSuccess();
                }
                else
                {
                    breaker.Metrics.MarkCommandFailure();
                }

                throw;
            }

            return result;
        }

        public TResult ExecuteWithBreaker<TResult>(SyncCommand<TResult> command, CancellationToken ct)
        {
            var breaker = _context.GetCircuitBreaker(command.BreakerKey);

            if (!breaker.IsAllowing())
            {
                throw new CircuitBreakerRejectedException();
            }

            TResult result;

            try
            {
                var stopwatch = Stopwatch.StartNew();

                result = command.Execute(ct);

                breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                breaker.Metrics.MarkCommandSuccess();
            }
            catch (Exception e)
            {
                if (_context.IsExceptionIgnored(e.GetType()))
                {
                    breaker.Metrics.MarkCommandSuccess();
                }
                else
                {
                    breaker.Metrics.MarkCommandFailure();
                }<commit_msg>Fix inconsistent success marking in breaker invoker<commit_after>﻿using Hudl.Mjolnir.Breaker;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Hudl.Mjolnir.Command
{
    internal interface IBreakerInvoker
    {
        Task<TResult> ExecuteWithBreakerAsync<TResult>(AsyncCommand<TResult> command, CancellationToken ct);
        TResult ExecuteWithBreaker<TResult>(SyncCommand<TResult> command, CancellationToken ct);
    }

    internal class BreakerInvoker : IBreakerInvoker
    {
        private readonly ICommandContext _context;

        public BreakerInvoker(ICommandContext context)
        {
            _context = context ?? CommandContext.Current;
        }

        public async Task<TResult> ExecuteWithBreakerAsync<TResult>(AsyncCommand<TResult> command, CancellationToken ct)
        {
            var breaker = _context.GetCircuitBreaker(command.BreakerKey);

            if (!breaker.IsAllowing())
            {
                throw new CircuitBreakerRejectedException();
            }

            TResult result;

            var stopwatch = Stopwatch.StartNew();
            try
            {
                // Await here so we can catch the Exception and track the state.
                // I suppose we could do this with a continuation, too. Await's easier.
                result = await command.ExecuteAsync(ct).ConfigureAwait(false);

                breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                breaker.Metrics.MarkCommandSuccess();
            }
            catch (Exception e)
            {
                if (_context.IsExceptionIgnored(e.GetType()))
                {
                    breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                    breaker.Metrics.MarkCommandSuccess();
                }
                else
                {
                    breaker.Metrics.MarkCommandFailure();
                }

                throw;
            }

            return result;
        }

        public TResult ExecuteWithBreaker<TResult>(SyncCommand<TResult> command, CancellationToken ct)
        {
            var breaker = _context.GetCircuitBreaker(command.BreakerKey);

            if (!breaker.IsAllowing())
            {
                throw new CircuitBreakerRejectedException();
            }

            TResult result;

            var stopwatch = Stopwatch.StartNew();
            try
            {
                result = command.Execute(ct);

                breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                breaker.Metrics.MarkCommandSuccess();
            }
            catch (Exception e)
            {
                if (_context.IsExceptionIgnored(e.GetType()))
                {
                    breaker.MarkSuccess(stopwatch.ElapsedMilliseconds);
                    breaker.Metrics.MarkCommandSuccess();
                }
                else
                {
                    breaker.Metrics.MarkCommandFailure();
                }
{'fraction_non_alphanumeric': 0.07474270288510207, 'fraction_numerical': 0.0, 'mean_word_length': 1.6264953478068231, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '234422', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1384, 'n_words': 273}