-
Notifications
You must be signed in to change notification settings - Fork 5
/
EnumerableExtensions.cs
28 lines (27 loc) · 1.14 KB
/
EnumerableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
namespace PennedObjects.RateLimiting
{
public static class EnumerableExtensions
{
/// <summary>
/// Limits the rate at which the sequence is enumerated.
/// </summary>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}"/> whose enumeration is to be rate limited.</param>
/// <param name="count">The number of items in the sequence that are allowed to be processed per time unit.</param>
/// <param name="timeUnit">Length of the time unit.</param>
/// <returns>An <see cref="IEnumerable{T}"/> containing the elements of the source sequence.</returns>
public static IEnumerable<T> LimitRate<T>(this IEnumerable<T> source, int count, TimeSpan timeUnit)
{
using (var rateGate = new RateGate(count, timeUnit))
{
foreach (var item in source)
{
rateGate.WaitToProceed();
yield return item;
}
}
}
}
}