Exemplo n.º 1
0
 public GitHubRateLimiter(IGlobalConfigurationProvider globalConfigurationProvider)
 {
     limiter = TimeLimiter.GetFromMaxCountByInterval(
         globalConfigurationProvider.GetMaxRequestsPerPeriod(),
         TimeSpan.FromSeconds(globalConfigurationProvider.GetPeriodDurationInSeconds())
         );
 }
Exemplo n.º 2
0
        public async Task UsageWithFactory()
        {
            var wrapped        = new TimeLimited(_Output);
            var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromMilliseconds(100));
            var timeLimited    = timeConstraint.Proxify <ITimeLimited>(wrapped);

            var watch = Stopwatch.StartNew();

            for (var i = 0; i < 50; i++)
            {
                await timeLimited.GetValue();
            }

            watch.Stop();
            watch.Elapsed.Should().BeGreaterThan(TimeSpan.FromMilliseconds(900));
            _Output.WriteLine($"Elapsed: {watch.Elapsed}");

            var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(110));

            Func <Task> cancellable = async() =>
            {
                while (true)
                {
                    await timeLimited.GetValue(cts.Token);
                }
            };

            await cancellable.Should().ThrowAsync <OperationCanceledException>();

            var res = await timeLimited.GetValue();

            res.Should().Be(56);
        }
        /// <summary>
        /// Register all the services that are specific to an external eve xml api web service
        /// </summary>
        /// <param name="builder"></param>
        private void registerEVEAPIServices(ContainerBuilder builder)
        {
            builder.Register <EveXmlWebClient>(c => new EveXmlWebClient(
                                                   c.ResolveKeyed <IHttpClientWrapper>(WebServices.EVEXMLAPI),
                                                   c.Resolve <IConfigurationProvider>()))
            .As <IEveXmlWebClient>()
            .InstancePerLifetimeScope();

            builder.Register <HttpClient>(c => HttpClientFactory.Create(
                                              new HttpClientHandler(),
                                              new RateLimitHandler(c.ResolveKeyed <IRateLimiter>(WebServices.EVEXMLAPI)),
                                              new LoggingHandler(c.Resolve <ILogger>())))
            .Keyed <HttpClient>(WebServices.EVEXMLAPI);

            builder.Register(c =>
                             new HttpClientWrapper(
                                 c.ResolveKeyed <HttpClient>(WebServices.EVEXMLAPI),
                                 c.Resolve <MediaTypeFormatterCollection>()
                                 )).Keyed <IHttpClientWrapper>(WebServices.EVEXMLAPI)
            .SingleInstance();

            builder.Register <TimeLimiter>(c => TimeLimiter.GetFromMaxCountByInterval(c.Resolve <IConfigurationProvider>().EveAPIRateLimit, TimeSpan.FromSeconds(1)))
            .Keyed <IRateLimiter>(WebServices.EVEXMLAPI)
            .SingleInstance();
        }
Exemplo n.º 4
0
 public CryptoService(IDataCache cache, IHttpClientFactory httpFactory)
 {
     _log         = LogManager.GetCurrentClassLogger();
     _cache       = cache;
     _httpFactory = httpFactory;
     ratelimit    = TimeLimiter.GetFromMaxCountByInterval(1, TimeSpan.FromSeconds(2));
 }
Exemplo n.º 5
0
        public IgdbApi(UpdatableAppSettings settings)
        {
            this.settings         = settings;
            requestLimiterHandler = TimeLimiter
                                    .GetFromMaxCountByInterval(4, TimeSpan.FromSeconds(1))
                                    .AsDelegatingHandler();
            HttpClient = new HttpClient(requestLimiterHandler);
            HttpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            CacheRoot = settings.Settings.IGDB.CacheDirectory;
            if (!Path.IsPathRooted(CacheRoot))
            {
                CacheRoot = Path.Combine(Paths.ExecutingDirectory, CacheRoot);
            }

            Games              = new Games(this);
            AlternativeNames   = new AlternativeNames(this);
            InvolvedCompanies  = new InvolvedCompanies(this);
            Genres             = new Genres(this);
            Websites           = new Websites(this);
            GameModes          = new GameModes(this);
            PlayerPerspectives = new PlayerPerspectives(this);
            Covers             = new Covers(this);
            Artworks           = new Artworks(this);
            Screenshots        = new Screenshots(this);
            AgeRatings         = new AgeRatings(this);
            Collections        = new Collections(this);
        }
Exemplo n.º 6
0
        public async Task <WorkloadResult> Execute(IBucket bucket, int workloadIndex)
        {
            Ensure.That(bucket, "bucket").IsNotNull();

            await OnPreExecute(bucket, workloadIndex, 0);

            var workloadResult = CreateWorkloadResult();

            var workloadTimer = Stopwatch.StartNew();

            if (_rateLimit == 0)
            {
                for (var i = 0; i < WorkloadSize; i++)
                {
                    await CreateWorkloadTask(bucket, workloadIndex, 0, workloadResult);
                }
            }
            else
            {
                var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(_rateLimit, TimeSpan.FromSeconds(1));
                for (var i = 0; i < WorkloadSize; i++)
                {
                    await timeConstraint;
                    await CreateWorkloadTask(bucket, workloadIndex, 0, workloadResult);
                }
            }

            workloadResult.TimeTaken = workloadTimer.Elapsed;

            await OnPostExecute(bucket);

            return(workloadResult);
        }
 public NexusModsIssueMonitor(ILogger <NexusModsIssueMonitor> logger, IClock clock, IServiceScopeFactory scopeFactory, IApplicationEnder applicationEnder) : base(applicationEnder)
 {
     _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
     _clock        = clock ?? throw new ArgumentNullException(nameof(clock));
     _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
     _timeLimiter  = TimeLimiter.GetFromMaxCountByInterval(1, TimeSpan.FromSeconds(90));
 }
 static ViewModelLocator()
 {
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
     SimpleIoc.Default.Register <IRateLimiter>(() => TimeLimiter.GetFromMaxCountByInterval(2, TimeSpan.FromSeconds(1)));
     SimpleIoc.Default.Register <ITwitchAPI>(() => new TwitchAPI(null, null, null));
     SimpleIoc.Default.Register <MainViewModel>();
     SimpleIoc.Default.Register <ApiExamplesViewModel>();
 }
Exemplo n.º 9
0
 //private List<Task> tasks;
 public SendMessage(ITelegramBotClient botClient)
 {
     //queue = new ConcurrentQueue<SendModel>();
     GroupLimit     = TimeLimiter.GetFromMaxCountByInterval(20, TimeSpan.FromMinutes(1));
     GlobalLimit    = TimeLimiter.GetFromMaxCountByInterval(30, TimeSpan.FromSeconds(1));
     tasks          = new ConcurrentQueue <Task>();
     this.botClient = botClient;
 }
Exemplo n.º 10
0
        public FaceIdentifierBase(string apiKey, string apiEndpoint)
        {
            Client = new FaceClient(new ApiKeyServiceClientCredentials(apiKey))
            {
                Endpoint = apiEndpoint
            };

            RateLimit = TimeLimiter.GetFromMaxCountByInterval(RateLimitRequests, RateLimitInterval);
        }
Exemplo n.º 11
0
 public JsApi(HttpMessageHandler httpClientHandler, HistorySettings historySettings)
 {
     _historySettings = historySettings;
     _httpClient      = new HttpClient(httpClientHandler)
     {
         Timeout = TimeSpan.FromSeconds(4)
     };
     _rateLimiter = TimeLimiter.GetFromMaxCountByInterval(3, TimeSpan.FromSeconds(1.00f));
 }
Exemplo n.º 12
0
        public async Task SimpleUsage()
        {
            var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));

            for (int i = 0; i < 1000; i++)
            {
                await timeConstraint.Enqueue(() => ConsoleIt());
            }
        }
Exemplo n.º 13
0
 public JsonServerHistoryPublisher(Settings settings, HttpMessageHandler httpMessageHandler)
 {
     _settings    = settings;
     _rateLimiter = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
     _httpClient  = new HttpClient(httpMessageHandler)
     {
         Timeout = TimeSpan.FromSeconds(4)
     };
 }
Exemplo n.º 14
0
        protected override HttpMessageHandler CreateHandler(CreateHttpClientArgs args)
        {
            var handler = TimeLimiter
                          .GetFromMaxCountByInterval(1000, TimeSpan.FromSeconds(100))
                          .AsDelegatingHandler();

            handler.InnerHandler = base.CreateHandler(args);

            return(handler);
        }
Exemplo n.º 15
0
        public async Task SimpleUsageAwaitable()
        {
            var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));

            for (var i = 0; i < 50; i++)
            {
                await timeConstraint;
                ConsoleIt();
            }
        }
Exemplo n.º 16
0
        public Twitch(IConfigurationRoot config)
        {
            // Build handler with RateLimit
            DelegatingHandler handler = TimeLimiter.GetFromMaxCountByInterval(750, TimeSpan.FromMinutes(1)).AsDelegatingHandler();

            twitch = new HttpClient(handler);
            // Set headers for the twitch api request
            twitch.DefaultRequestHeaders.Add("Client-ID", config.GetApiKey("Twitch"));
            twitch.DefaultRequestHeaders.Add("Authorization", config.GetApiKey("Twitch_OAuth"));
            configuration = config;
        }
Exemplo n.º 17
0
        public OpenWeatherMapClient(string apiKey)
        {
            _ApiKey = apiKey;
            var handler = TimeLimiter
                          .GetFromMaxCountByInterval(60, TimeSpan.FromMinutes(1))
                          .AsDelegatingHandler();

            _Client = new HttpClient(handler)
            {
                BaseAddress = new Uri("https://api.openweathermap.org/data/2.5/")
            };
        }
Exemplo n.º 18
0
        public VkApi(AccessToken token, HttpMessageHandler httpClientHandler)
        {
            _token      = token;
            _httpClient = new HttpClient(httpClientHandler)
            {
                Timeout = TimeSpan.FromSeconds(4)
            };
            _responseCache = new ConcurrentDictionary <string, VkApiResponseInfo>();
            _requestLog    = new ConcurrentDictionary <string, DateTimeOffset>();

            _rateLimiter = TimeLimiter.GetFromMaxCountByInterval(3, TimeSpan.FromSeconds(1.00f));
        }
Exemplo n.º 19
0
        /// <summary>Initializes a new instance of the <see cref="CoinMarketCapClient"/> class.</summary>
        /// <param name="httpClient">The HTTP client.</param>
        /// <param name="requestsPerMinute">The requests per minute.</param>
        /// <exception cref="System.ArgumentNullException">httpClient</exception>
        public CoinMarketCapClient(IRestClient httpClient, int requestsPerMinute)
        {
            this.gate       = TimeLimiter.GetFromMaxCountByInterval(requestsPerMinute, new TimeSpan(0, 1, 0));
            this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));

            var serializer = new CoinMarketCapSerializer();

            this.HttpClient.UserAgent = "Inixe CoinManager";
            this.HttpClient.AddHandler("application/json", serializer);

            this.crytocurrencies = new Lazy <IList <string> >(this.LoadCrytoCurrencies);
        }
Exemplo n.º 20
0
        public PipeDriveClient(string apiToken, int apiRate = DefaultApiRate)
        {
            _Client = new RestClient("https://api.pipedrive.com/v1");
            _Client.AddHandler("application/json", PipeDriveJsonSerializer.Default);
            _Client.AddHandler("text/json", PipeDriveJsonSerializer.Default);
            _Client.AddHandler("text/x-json", PipeDriveJsonSerializer.Default);
            _Client.AddHandler("text/javascript", PipeDriveJsonSerializer.Default);
            _Client.AddHandler("*+json", PipeDriveJsonSerializer.Default);

            _ApiToken = apiToken;

            // Fly under the radar by doing only 99 requests per 10 seconds
            _TimeContraint = TimeLimiter.GetFromMaxCountByInterval(apiRate, TimeSpan.FromSeconds(10));
        }
Exemplo n.º 21
0
        /// <summary>Initializes a new instance of the <see cref="BitsoClientBase"/> class.</summary>
        /// <param name="restClient">The rest client to use. This is often usefull for unit testing, If <c>null</c> a default RestClient will be used</param>
        /// <param name="serverUrl">The server URL.</param>
        /// <param name="apiKey">The Api key value</param>
        /// <param name="apiSecret">The Api secret value</param>
        /// <exception cref="ArgumentException">Invalid URL - serverUrl</exception>
        /// <remarks>None</remarks>
        protected BitsoClientBase(IRestClient restClient, string serverUrl, string apiKey, SecureString apiSecret)
        {
            if (!System.Uri.IsWellFormedUriString(serverUrl, System.UriKind.Absolute))
            {
                throw new ArgumentException("Invalid URL", nameof(serverUrl));
            }

            this.secureApiSecret = apiSecret;

            this.apiKey        = apiKey;
            this.targetUrl     = new Uri(serverUrl);
            this.client        = restClient ?? new RestClient(serverUrl);
            this.disposedValue = false;
            this.rateLimiter   = TimeLimiter.GetFromMaxCountByInterval(300, new TimeSpan(0, 5, 0));
        }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            Log("Starting tasks ...");
            var constraint = TimeLimiter.GetFromMaxCountByInterval(1, TimeSpan.FromSeconds(0.5));
            var tasks      = new[]
            {
                DoWorkAsync("Task1", constraint),
                DoWorkAsync("Task2", constraint),
                DoWorkAsync("Task3", constraint),
                DoWorkAsync("Task4", constraint)
            };

            Task.WaitAll(tasks);
            Log("All tasks finished.");
            Console.ReadLine();
        }
Exemplo n.º 23
0
        public async Task SimpleUsageWithcancellation()
        {
            var timeconstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
            var cts            = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(10));

            for (int i = 0; i < 1000; i++)
            {
                try {
                    await timeconstraint.Perform(() => ConsoleIt(), cts.Token);
                }
                catch (Exception) {
                }
            }
        }
Exemplo n.º 24
0
        public async Task SimpleUsageWithCancellation()
        {
            var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(3, TimeSpan.FromSeconds(1));
            var cts            = new CancellationTokenSource(1100);

            for (var i = 0; i < 1000; i++)
            {
                try
                {
                    await timeConstraint.Enqueue(() => ConsoleIt(), cts.Token);
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }
Exemplo n.º 25
0
        public async Task ParallelSlowActionPerformance()
        {
            var         stopWatch = Stopwatch.StartNew();
            var         limiter   = TimeLimiter.GetFromMaxCountByInterval(100, TimeSpan.FromMinutes(1));
            Func <Task> func      = async() => await Task.Delay(300);

            await Task.WhenAll(
                limiter.Enqueue(func),
                limiter.Enqueue(func),
                limiter.Enqueue(func),
                limiter.Enqueue(func),
                limiter.Enqueue(func),
                limiter.Enqueue(func));

            stopWatch.Stop();

            stopWatch.Elapsed.TotalSeconds.Should().BeLessThan(1);
        }
Exemplo n.º 26
0
        public static async Task <IEnumerable <Models.Objects.Issue> > ListIssuesAsync(this GitHubClient s_gitHub, RepositoryInfo repo, SearchIssuesRequest issueQuery)
        {
            TimeLimiter rateLimiter = TimeLimiter.GetFromMaxCountByInterval(1, TimeSpan.FromSeconds(1));

            List <Models.Objects.Issue> issuesFound = new List <Models.Objects.Issue>();

            int totalPages = -1, currentPage = 0;

            issueQuery.Repos.Add(repo.Owner, repo.Name);

            do
            {
                currentPage++;
                issueQuery.Page = currentPage;

                SearchIssuesResult searchresults = null;

                // make sure the rate limit is met
                await rateLimiter;
                searchresults = await s_gitHub.Search.SearchIssues(issueQuery);

                Colorizer.WriteLine("Found [Yellow!{0}] issues in [Magenta!{1}].", searchresults.TotalCount, $"{repo.Owner}\\{repo.Name}");

                foreach (Issue item in searchresults.Items)
                {
                    Models.Objects.Issue issueFound = new Models.Objects.Issue(item)
                    {
                        // set the repo on the item
                        RepositoryName   = repo.Name,
                        OrganizationName = repo.Owner
                    };
                    issuesFound.Add(issueFound);
                }

                // if this is the first call, setup the totalpages stuff
                if (totalPages == -1)
                {
                    totalPages = (searchresults.TotalCount / 100) + 1;
                }
                Colorizer.WriteLine("Retrieved [Yellow!{0}]/[Yellow!{1}] issues.", issuesFound.Count, searchresults.TotalCount);
            } while (totalPages > currentPage);

            return(issuesFound);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Creates an Instance of the TwitchAPI Class.
        /// </summary>
        /// <param name="clientId">Twitch Client Id.</param>
        /// <param name="accessToken">Twitch Access Token.</param>
        /// <param name="rateLimit">Should RateLimit Requests?</param>
        /// <param name="rateLimiter">Instance Of RateLimiter. Useful if using multiple API instances on one connection and you wish to share the requests ratelimiter.</param>
        /// <param name="callsPerPeriod">Number of Requests per Period to rate limit to</param>
        /// <param name="ratePeriod">Period for Rate Limit (In Seconds)</param>
        public TwitchAPI(string clientId = null, string accessToken = null, bool rateLimit = true, IRateLimiter rateLimiter = null, int callsPerPeriod = 1, int ratePeriod = 1)
        {
            _rateLimiter = rateLimit ?
                           (rateLimiter ?? TimeLimiter.GetFromMaxCountByInterval(callsPerPeriod, TimeSpan.FromSeconds(ratePeriod)))
                : BypassLimiter.CreateLimiterBypassInstance();

            Auth            = new Auth(this);
            Blocks          = new Blocks(this);
            Badges          = new Badges(this);
            Bits            = new Bits(this);
            ChannelFeeds    = new ChannelFeeds(this);
            Channels        = new Channels(this);
            Chat            = new Chat(this);
            Clips           = new Clips(this);
            Collections     = new Collections(this);
            Communities     = new Communities(this);
            Follows         = new Follows(this);
            Games           = new Games(this);
            Ingests         = new Ingests(this);
            Root            = new Root(this);
            Search          = new Search(this);
            Streams         = new Streams(this);
            Subscriptions   = new Subscriptions(this);
            Teams           = new Teams(this);
            ThirdParty      = new ThirdParty(this);
            Undocumented    = new Undocumented(this);
            Users           = new Users(this);
            Videos          = new Videos(this);
            Webhooks        = new Webhooks(this);
            Debugging       = new Debugging();
            Settings        = new ApiSettings(this);
            _jsonSerializer = new TwitchLibJsonSerializer();

            if (!string.IsNullOrWhiteSpace(clientId))
            {
                Settings.ClientId = clientId;
            }
            if (!string.IsNullOrWhiteSpace(accessToken))
            {
                Settings.AccessToken = accessToken;
            }
        }
        private void SetThrottler()
        {
            if (this.targetThroughput > 0)
            {
                // 25% extra for overhead - maybe, need a more scientific way to calc, 0% for now
                double overheadCompensator = this.dryRun ? 1.0 : 1.0;

                double controlInterval = 1.0; // seconds

                // how many invokations per second are allowed
                double invocationsPerControlInterval = (double)(this.targetThroughput * overheadCompensator) / (double)this.batchSize * controlInterval;

                double clampBelowInterval = controlInterval / invocationsPerControlInterval * 1000.0;

                // renormalise to avoid fractional invocations
                if (invocationsPerControlInterval < 100)
                {
                    double normaliser = 100.0 / invocationsPerControlInterval;
                    invocationsPerControlInterval = normaliser * invocationsPerControlInterval;
                    controlInterval = normaliser * controlInterval;
                }

                // does not make sence to clamp from below at < 15ms - timers aren't that precise
                if (clampBelowInterval < 15.0)
                {
                    this.throttler = TimeLimiter.GetFromMaxCountByInterval(Convert.ToInt32(invocationsPerControlInterval), TimeSpan.FromSeconds(controlInterval));
                }
                else
                {
                    var clampAbove = new CountByIntervalAwaitableConstraint(Convert.ToInt32(invocationsPerControlInterval), TimeSpan.FromSeconds(controlInterval));
                    // Clamp from below: e.g. one invocation every 100 ms
                    var clampBelow = new CountByIntervalAwaitableConstraint(1, TimeSpan.FromMilliseconds(clampBelowInterval));
                    //Compose the two constraints
                    this.throttler = TimeLimiter.Compose(clampAbove, clampBelow);
                }
            }
            else // no throttling
            {
                this.throttler = TimeLimiter.GetFromMaxCountByInterval(Int32.MaxValue, TimeSpan.FromSeconds(1));
            }
        }
Exemplo n.º 29
0
        public async Task SimpleUsageAwaitableCancellable()
        {
            var timeConstraint = TimeLimiter.GetFromMaxCountByInterval(5, TimeSpan.FromSeconds(1));
            var cts            = new CancellationTokenSource(TimeSpan.FromSeconds(1.1));
            var token          = cts.Token;
            var count          = 0;

            Func <Task> cancellable = async() =>
            {
                while (true)
                {
                    await timeConstraint;
                    token.ThrowIfCancellationRequested();
                    ConsoleIt();
                    count++;
                }
            };

            await cancellable.Should().ThrowAsync <OperationCanceledException>();

            count.Should().Be(10);
        }
Exemplo n.º 30
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            // Load up the image content we will use for the tests
            ByteArrayContent content;
            using (var fs = File.OpenRead(@".\Documents\Page11_Image1.jpg"))
            {
                byte[] byteData;
                fs.Position = 0;
                using (var binaryReader = new BinaryReader(fs))
                {
                    byteData = binaryReader.ReadBytes((int)fs.Length);
                }
                content = new ByteArrayContent(byteData);
                content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/octet-stream");
            }

            // Set up a shared Time based RateLimiter (for all calls to service)
            var callsPerSecond = 5;
            Console.WriteLine($"TimeLimiter Max Calls/Sec: {callsPerSecond}");
            var timeLimiter = TimeLimiter.GetFromMaxCountByInterval(callsPerSecond, TimeSpan.FromSeconds(1));

            // Add 'n' Workers
            var maxDOP = 20;
            for (int i = 0; i < maxDOP; i++)
            {
                int workerId = i;
                services.AddSingleton <IHostedService>(
                    sp => new TestWorker(
                        serviceProvider: sp,
                        workerId: workerId,
                        timeLimiter: timeLimiter,
                        imageContent: content
                        )
                    );
            }
        });