<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace Tweek.ApiService.Utils
{
    public static class NancyHelpers
    {
        public static Func<dynamic, CancellationToken, Task<dynamic>> AsyncTimeout(
            int timeout,
            Func<dynamic, CancellationToken, Task<dynamic>> fn) =>
                async (context, token) =>
                {
                    var newToken = new CancellationTokenSource();
                    token.Register(() => newToken.Cancel());
                    var delay = Task.Delay(timeout, newToken.Token);
                    try
                    {
                        var result = await Task.WhenAny(delay, fn(context, newToken.Token));
                        token.ThrowIfCancellationRequested();
                        if (result == delay) throw new TimeoutException();
                        return await result;
                    }
                    finally
                    {
                        newToken.Cancel();<commit_msg>changed task cancellation exception<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace Tweek.ApiService.Utils
{
    public static class NancyHelpers
    {
        public static Func<dynamic, CancellationToken, Task<dynamic>> AsyncTimeout(
            int timeout,
            Func<dynamic, CancellationToken, Task<dynamic>> fn) =>
                async (context, token) =>
                {
                    var newToken = new CancellationTokenSource();
                    token.Register(() => newToken.Cancel());
                    var delay = Task.Delay(timeout, newToken.Token);
                    try
                    {
                        var result = await Task.WhenAny(delay, fn(context, newToken.Token));
                        if (token.IsCancellationRequested) throw new TaskCanceledException();
                        if (result == delay) throw new TimeoutException();
                        return await result;
                    }
                    finally
                    {
                        newToken.Cancel();
{'fraction_non_alphanumeric': 0.08421052631578947, 'fraction_numerical': 0.0, 'mean_word_length': 1.5457589285714286, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '2749571', 'n_tokens_mistral': 514, 'n_tokens_neox': 504, 'n_words': 130}