Created
April 6, 2017 08:33
-
-
Save LindaLawton/55c061ed44c2ef8e00336be5c8655e38 to your computer and use it in GitHub Desktop.
Flood protection class for Google APIs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace GoogleDevTest | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// 10 requests within 1 second | |
var que = new GoogleDevTest.GoogleFloodBuster(new TimeSpan(0, 0, 1), 10); | |
// 100 requets within 100 seconds (diffrent api shave diffrent flood limits) | |
//var que = new GoogleDevTest.GoogleFloodBuster(new TimeSpan(0, 0, 100), 100); | |
// This loop is just for testing | |
for (int i = 0; i < 200; i++) | |
{ | |
que.Enque(); | |
// Simulates a request being sent to Google. They normaly return quite quickly | |
System.Threading.Thread.Sleep(500); | |
} | |
} | |
} | |
internal static class GoogleFloodBusterExtensions | |
{ | |
public static long ToUnixTime(this DateTime date) | |
{ | |
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | |
return Convert.ToInt64((date - epoch).TotalMilliseconds); | |
} | |
} | |
internal class GoogleFloodBuster | |
{ | |
private int pos = 0; | |
private long[] que; | |
private TimeSpan amountOfTimeToMonitor = new TimeSpan(); | |
/// <summary> | |
/// Constructor for Flood protecthion. | |
/// </summary> | |
/// <param name="amountOfTimeToMonitor">How much time can go between x number of requests</param> | |
/// <param name="numberOfRequestsToMonintor">How many requets we are checking.</param> | |
public GoogleFloodBuster( TimeSpan amountOfTimeToMonitor, int numberOfRequestsToMonintor) | |
{ | |
que = new long[numberOfRequestsToMonintor]; | |
this.amountOfTimeToMonitor = amountOfTimeToMonitor; | |
} | |
/// <summary> | |
/// Adds a new item to the que | |
/// </summary> | |
/// <param name="numberOfRequests"></param> | |
public void Enque() | |
{ | |
if (pos == que.Length) | |
deque(); | |
que[pos++] = DateTime.UtcNow.ToUnixTime(); | |
var timeDiff = que[que.Length-1] - que[0]; | |
if (timeDiff > 0 && timeDiff < amountOfTimeToMonitor.TotalMilliseconds) | |
{ | |
Console.WriteLine("Sleeping: " + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + timeDiff + " " + amountOfTimeToMonitor.TotalMilliseconds); | |
System.Threading.Thread.Sleep(Convert.ToInt32(amountOfTimeToMonitor.TotalMilliseconds) - (int)timeDiff); | |
} | |
else { | |
Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff") + " "+ timeDiff + " " + amountOfTimeToMonitor.TotalMilliseconds); | |
} | |
} | |
// Remove the oldest que item. Each item is moved up one position. | |
private void deque() | |
{ | |
for (int i = 1; i < pos; i++) | |
{ | |
que[i - 1] = que[i]; | |
} | |
pos--; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment